Permalink
Browse files

news and evetns tests, nicer urls, improvements in views

  • Loading branch information...
1 parent e9dd276 commit cae56e4f9c0e80df4293bff24fc6d76acfe140fd @evildmp committed Nov 8, 2012
Showing with 149 additions and 20 deletions.
  1. +127 −0 news_and_events/tests.py
  2. +8 −14 news_and_events/urls.py
  3. +14 −6 news_and_events/views.py
View
@@ -0,0 +1,127 @@
+from django.test import TestCase
+from django.test.client import Client
+from django.test.utils import override_settings
+
+from django.conf import settings
+from django.contrib.auth.models import User
+
+# we're testing the behaviour of a method that uses date-related functions
+import datetime
+from django.utils import timezone
+
+from models import NewsArticle
+from contacts_and_people.models import Entity
+
+class NewsTests(TestCase):
+ def setUp(self):
+ # Every test needs a client.
+ self.client = Client()
+
+ # self.school = Entity(
+ # name="School of Medicine",
+ # slug="medicine",
+ # )
+ # self.school.save()
+
+ # create a news item
+ self.tootharticle = NewsArticle(
+ title = "All about teeth",
+ slug = "all-about-teeth",
+ date = timezone.now() + datetime.timedelta(days=30),
+ )
+ print self.tootharticle.date
+ def test_generic_attributes(self):
+ self.tootharticle.save()
+ # the item has no informative content
+ self.assertEqual(self.tootharticle.is_uninformative, True)
+
+ # there are no Entities in the database, so this can't be hosted_by anything
+ self.assertEqual(self.tootharticle.hosted_by, None)
+
+ # since there are no Entities in the database, default to settings's template
+ self.assertEqual(self.tootharticle.get_template, settings.CMS_TEMPLATES[0][0])
+
+class NewsEventsViewsTests(TestCase):
+ def setUp(self):
+ # Every test needs a client.
+ self.client = Client()
+
+ self.school = Entity(
+ name="School of Medicine",
+ slug="medicine",
+ )
+
+ # create a news item
+ self.tootharticle = NewsArticle(
+ title = "All about teeth",
+ slug = "all-about-teeth"
+ )
+
+ self.adminuser = User.objects.create_user('arkestra', 'arkestra@example.com', 'arkestra')
+ self.adminuser.is_staff=True
+ self.adminuser.save()
+
+ def test_newsarticle_views(self):
+ self.tootharticle.save()
+
+ # Issue a GET request.
+ response = self.client.get('/news/all-about-teeth/')
+
+ # Check that the response is 404 because it's not published
+ self.assertEqual(response.status_code, 404)
+
+ # log in a staff user
+ self.client.login(username='arkestra', password='arkestra')
+ response = self.client.get('/news/all-about-teeth/')
+ self.assertEqual(response.status_code, 200)
+
+ # log out the staff user
+ self.client.logout()
+ self.tootharticle.published = True
+ self.tootharticle.save()
+
+ # Check that the response is 200 OK.
+ response = self.client.get('/news/all-about-teeth/')
+ self.assertEqual(response.status_code, 200)
+
+ self.assertEqual(response.context['newsarticle'], self.tootharticle)
+
+ def test_news_and_events_views_in_multiple_entity_mode(self):
+ self.school.save()
+
+ response = self.client.get('/news-and-events/')
+ self.assertEqual(response.status_code, 200)
+
+ response = self.client.get('/news-and-events/medicine/')
+ self.assertEqual(response.status_code, 200)
+
+ response = self.client.get('/news-and-events/xxxx/')
+ self.assertEqual(response.status_code, 404)
+
+ response = self.client.get('/news-archive/')
+ self.assertEqual(response.status_code, 200)
+
+ response = self.client.get('/news-archive/medicine/')
+ self.assertEqual(response.status_code, 200)
+
+ response = self.client.get('/news-archive/xxxx/')
+ self.assertEqual(response.status_code, 404)
+
+ response = self.client.get('/previous-events/')
+ self.assertEqual(response.status_code, 200)
+
+ response = self.client.get('/previous-events/medicine/')
+ self.assertEqual(response.status_code, 200)
+
+ response = self.client.get('/previous-events/xxxx/')
+ self.assertEqual(response.status_code, 404)
+
+ response = self.client.get('/forthcoming-events/')
+ self.assertEqual(response.status_code, 200)
+
+ response = self.client.get('/forthcoming-events/medicine/')
+ self.assertEqual(response.status_code, 200)
+
+ response = self.client.get('/forthcoming-events/xxx/')
+ self.assertEqual(response.status_code, 404)
+
View
@@ -1,24 +1,18 @@
from django.conf.urls.defaults import *
+from news_and_events import views
# from news_and_events.views import NewsAndEventsViews
urlpatterns = patterns('',
# news and events items
- (r"^news/(?P<slug>[-\w]+)/$", "news_and_events.views.newsarticle"),
- (r"^event/(?P<slug>[-\w]+)/$", "news_and_events.views.event"),
+ url(r"^news/(?P<slug>[-\w]+)/$", views.newsarticle, name="newsarticle"),
+ url(r"^event/(?P<slug>[-\w]+)/$", views.event, name="event"),
# named entities' news and events
- (r'^news-archive/(?P<slug>[-\w]+)/$', "news_and_events.views.news_archive"),
- (r'^previous-events/(?P<slug>[-\w]+)/$', "news_and_events.views.previous_events"),
- (r'^forthcoming-events/(?P<slug>[-\w]+)/$', "news_and_events.views.all_forthcoming_events"),
- (r"^news-and-events/(?P<slug>[-\w]+)/$", "news_and_events.views.news_and_events"),
-
- # base entity's news and events
- (r'^news-archive/$', "news_and_events.views.news_archive"),
- (r'^previous-events/$', "news_and_events.views.previous_events"),
- (r'^forthcoming-events/$', "news_and_events.views.all_forthcoming_events"),
- (r'^news-and-events/$', "news_and_events.views.news_and_events"),
- # (r'^news-and-events/$', NewsAndEventsViews.test),
-
+ url(r'^news-archive/(?:(?P<slug>[-\w]+)/)?$', views.news_archive, name="news_archive"),
+ url(r'^previous-events/(?:(?P<slug>[-\w]+)/)?$', views.previous_events, name="previous_events"),
+ url(r'^forthcoming-events/(?:(?P<slug>[-\w]+)/)?$', views.all_forthcoming_events, name="forthcoming_event"),
+ url(r"^news-and-events/(?:(?P<slug>[-\w]+)/)?$", views.news_and_events, name="news_and_events"),
)
#(r"^entity/(?P<slug>[-\w]+)/news/$", "news_and_events.views.news"), # in development
+
View
@@ -1,5 +1,7 @@
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
+from django.http import Http404
+from django.utils import timezone
from contacts_and_people.models import Entity
from links.link_functions import object_links
@@ -11,7 +13,13 @@
def common_settings(request, slug):
- entity = Entity.objects.get(slug=slug) or Entity.objects.base_entity()
+ if slug:
+ try:
+ entity = Entity.objects.get(slug=slug)
+ except Entity.DoesNotExist:
+ raise Http404
+ else:
+ entity = Entity.objects.base_entity()
request.auto_page_url = request.path
# request.path = entity.get_website.get_absolute_url() # for the menu, so it knows where we are
request.current_page = entity.get_website
@@ -30,7 +38,7 @@ def common_settings(request, slug):
return instance, context, entity
-def news_and_events(request, slug=getattr(Entity.objects.base_entity(), "slug", None)):
+def news_and_events(request, slug):
instance, context, entity = common_settings(request, slug)
instance.type = "main_page"
@@ -59,7 +67,7 @@ def news_and_events(request, slug=getattr(Entity.objects.base_entity(), "slug",
context,
)
-def previous_events(request, slug=getattr(Entity.objects.base_entity(), "slug", None)):
+def previous_events(request, slug):
instance, context, entity = common_settings(request, slug)
instance.type = "sub_page"
@@ -87,7 +95,7 @@ def previous_events(request, slug=getattr(Entity.objects.base_entity(), "slug",
context,
)
-def all_forthcoming_events(request, slug=getattr(Entity.objects.base_entity(), "slug", None)):
+def all_forthcoming_events(request, slug):
instance, context, entity = common_settings(request, slug)
instance.type = "sub_page"
@@ -115,7 +123,7 @@ def all_forthcoming_events(request, slug=getattr(Entity.objects.base_entity(), "
context,
)
-def news_archive(request, slug=getattr(Entity.objects.base_entity(),"slug", None)):
+def news_archive(request, slug):
instance, context, entity = common_settings(request, slug)
instance.type = "sub_page"
@@ -152,7 +160,7 @@ def newsarticle(request, slug):
if request.user.is_staff:
newsarticle = get_object_or_404(NewsArticle, slug=slug)
else:
- newsarticle = get_object_or_404(NewsArticle, slug=slug, published=True)
+ newsarticle = get_object_or_404(NewsArticle, slug=slug, published=True, date__lte=timezone.now())
return render_to_response(
"news_and_events/newsarticle.html",
{

0 comments on commit cae56e4

Please sign in to comment.