This repository has been archived by the owner on May 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added views for creating and viewing a list of forums
- Loading branch information
Showing
7 changed files
with
127 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,6 @@ | ||
"""fora URL Configuration | ||
The `urlpatterns` list routes URLs to views. For more information please see: | ||
https://docs.djangoproject.com/en/1.8/topics/http/urls/ | ||
Examples: | ||
Function views | ||
1. Add an import: from my_app import views | ||
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home') | ||
Class-based views | ||
1. Add an import: from other_app.views import Home | ||
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home') | ||
Including another URLconf | ||
1. Add an import: from blog import urls as blog_urls | ||
2. Add a URL to urlpatterns: url(r'^blog/', include(blog_urls)) | ||
""" | ||
from django.conf.urls import include, url | ||
from django.contrib import admin | ||
|
||
|
||
urlpatterns = [ | ||
url(r'^admin/', include(admin.site.urls)), | ||
url(r'^forums/', include('forums.urls', namespace='forums')), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<form action="" method="post"> | ||
{% csrf_token %} | ||
{{ form.as_p }} | ||
<input type="submit" value="Create Forum" /> | ||
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<a href="{% url 'forums:new' %}">New Forum</a> | ||
|
||
{% for forum in object_list %} | ||
<div> | ||
<h1>{{ forum.name }}</h1> | ||
<p>{{ forum.description }}</p> | ||
</div> | ||
{% empty %} | ||
<p>No forums found</p> | ||
{% endfor %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from django.core.urlresolvers import reverse | ||
from django.test import TestCase, Client | ||
|
||
|
||
class ForumUrlsTestCase(TestCase): | ||
|
||
def setUp(self): | ||
super(ForumUrlsTestCase, self).setUp() | ||
self.client = Client() | ||
|
||
def test_index_url(self): | ||
response = self.client.get(reverse('forums:index')) | ||
self.assertEqual(response.status_code, 200) | ||
|
||
def test_new_url(self): | ||
response = self.client.get(reverse('forums:new')) | ||
self.assertEqual(response.status_code, 200) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
from django.core.urlresolvers import reverse | ||
from django.test import TestCase, RequestFactory | ||
from forums.views import ForumIndexView, NewForumView | ||
from forums.models import Forum | ||
|
||
|
||
class ForumIndexViewTestCase(TestCase): | ||
|
||
def setUp(self): | ||
self.factory = RequestFactory() | ||
super(ForumIndexViewTestCase, self).setUp() | ||
|
||
def test_index_with_no_forums(self): | ||
request = self.factory.get('/') | ||
response = ForumIndexView.as_view()(request) | ||
response.render() | ||
|
||
self.assertEqual(response.status_code, 200) | ||
self.assertIn('No forums found', response.content) | ||
|
||
def test_index_with_forums(self): | ||
f = Forum.objects.create(name='First Forum', description='A forum') | ||
|
||
request = self.factory.get('/') | ||
response = ForumIndexView.as_view()(request) | ||
response.render() | ||
|
||
self.assertEqual(response.status_code, 200) | ||
self.assertIn(f.name, response.content) | ||
self.assertIn(f.description, response.content) | ||
|
||
|
||
class NewForumViewTestCase(TestCase): | ||
|
||
def setUp(self): | ||
self.factory = RequestFactory() | ||
super(NewForumViewTestCase, self).setUp() | ||
|
||
def test_get_new_view(self): | ||
request = self.factory.get('/') | ||
response = NewForumView.as_view()(request) | ||
response.render() | ||
|
||
self.assertEqual(response.status_code, 200) | ||
self.assertIn('<label for="id_name">Name:</label>', response.content) | ||
self.assertIn( | ||
'<label for="id_description">Description:</label>', | ||
response.content | ||
) | ||
|
||
def test_post_form(self): | ||
request = self.factory.post(reverse('forums:new'), data={ | ||
'name': 'My Forum', | ||
'description': 'My forum containing my threads' | ||
}) | ||
response = NewForumView.as_view()(request) | ||
|
||
self.assertEqual(response.status_code, 302) | ||
self.assertEqual(response.url, reverse('forums:index')) | ||
|
||
def test_post_form_invalid(self): | ||
request = self.factory.post(reverse('forums:new'), data={}) | ||
response = NewForumView.as_view()(request) | ||
response.render() | ||
|
||
self.assertIn( | ||
'<ul class="errorlist"><li>This field is required.</li></ul>', | ||
response.content | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from django.conf.urls import url | ||
from forums.views import NewForumView, ForumIndexView | ||
|
||
|
||
urlpatterns = [ | ||
url(r'^$', ForumIndexView.as_view(), name='index'), | ||
url(r'^new/$', NewForumView.as_view(), name='new'), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,16 @@ | ||
# Create your views here. | ||
from django.core.urlresolvers import reverse | ||
from django.views.generic.edit import CreateView | ||
from django.views.generic.list import ListView | ||
from forums.models import Forum | ||
|
||
|
||
class ForumIndexView(ListView): | ||
model = Forum | ||
|
||
|
||
class NewForumView(CreateView): | ||
model = Forum | ||
fields = ['name', 'description'] | ||
|
||
def get_success_url(self): | ||
return reverse('forums:index') |