Permalink
Browse files

Adding decorators support

  • Loading branch information...
1 parent e6e1281 commit 3637cfe3edab3218ea7eb7fa82a9249b2e5ee3b5 @davidbgk davidbgk committed May 27, 2010
@@ -14,6 +14,7 @@ def __init__(self, **kwargs):
mimetype = 'text/html',
template_loader = None,
template_name = None,
+ decorators = None,
methods = ['GET',],
restriction_message = _('Restricted page.'),
warning_message = _('You must define "%s" method.'),
@@ -30,6 +31,10 @@ def __call__(self, request, *args, **kwargs):
method = getattr(self, method_name)
except AttributeError:
return HttpResponseNotAllowed(self.warning_message % method_name)
+
+ if self.decorators is not None:
+ for decorator in reversed(self.decorators):
+ method = decorator(method)
return method(request, obj, *args, **kwargs)
def get(self, request, obj, *args, **kwargs):
@@ -8,3 +8,4 @@
'class_based_views',
'class_based_views.tests',
]
+SITE_ID = 1
@@ -115,6 +115,11 @@ def test_detail_by_pk(self):
self.assertEqual(res.context['author'], Author.objects.get(pk=1))
self.assertTemplateUsed(res, 'tests/author_detail.html')
+ def test_detail_by_pk_restricted(self):
+ res = self.client.get('/detail/author/1/restricted/')
+ self.assertEqual(res.status_code, 302)
+ self.assertRedirects(res, 'http://testserver/accounts/login/?next=/detail/author/1/restricted/')
+
def test_detail_by_slug(self):
res = self.client.get('/detail/author/byslug/scott-rosenberg/')
self.assertEqual(res.status_code, 200)
@@ -141,6 +146,16 @@ def test_create(self):
self.assertEqual(res.status_code, 302)
self.assertEqual(str(Author.objects.all()), "[<Author: Randall Munroe>]")
+ def test_restricted_create_restricted(self):
+ res = self.client.get('/edit/authors/create/')
+ self.assertEqual(res.status_code, 200)
+ self.assertTemplateUsed(res, 'tests/list.html')
+
+ res = self.client.post('/edit/authors/create/restricted/',
+ {'name': 'Randall Munroe', 'slug': 'randall-munroe'})
+ self.assertEqual(res.status_code, 302)
+ self.assertRedirects(res, 'http://testserver/accounts/login/?next=/edit/authors/create/restricted/')
+
def test_update(self):
Author.objects.create(**{'name': 'Randall Munroe', 'slug': 'randall-munroe'})
res = self.client.get('/edit/author/1/update/')
@@ -14,12 +14,14 @@
# DetailView
(r'^detail/obj/$', views.ObjectDetail()),
url(r'^detail/author/(?P<pk>\d+)/$', views.AuthorDetail(), name="author_detail"),
+ (r'^detail/author/(?P<pk>\d+)/restricted/$', views.AuthorDetailRestricted()),
(r'^detail/author/byslug/(?P<slug>[\w-]+)/$', views.AuthorDetail()),
(r'^detail/author/invalid/url/$', views.AuthorDetail()),
(r'^detail/author/invalid/qs/$', views.AuthorDetail(queryset=None)),
# EditView
(r'^edit/authors/create/$', views.AuthorCreate()),
+ (r'^edit/authors/create/restricted/$', views.AuthorCreateRestricted()),
(r'^edit/author/(?P<pk>\d+)/update/$', views.AuthorUpdate()),
(r'^edit/author/(?P<pk>\d+)/delete/$', views.AuthorDelete()),
@@ -67,4 +69,7 @@
(r'^dates/books/(\d{4})/([a-z]{3})/(\d{1,2})/nopk/$', views.BookDetail()),
(r'^dates/books/(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\d{1,2})/byslug/(?P<slug>[\w-]+)/$', views.BookDetail()),
+
+ # Useful for testing redirects
+ (r'^accounts/login/$', 'django.contrib.auth.views.login')
)
@@ -1,4 +1,6 @@
from django.core.urlresolvers import reverse
+from django.contrib.auth.decorators import login_required
+from django.utils.decorators import method_decorator
from models import Author, Book
from forms import AuthorForm
@@ -19,6 +21,9 @@ class AuthorList(class_based_views.ListView):
class AuthorDetail(class_based_views.DetailView):
queryset = Author.objects.all()
+class AuthorDetailRestricted(AuthorDetail):
+ decorators = [login_required, ]
+
class AuthorCreate(class_based_views.CreateView):
queryset = Author.objects.all()
template_name = 'tests/list.html'
@@ -34,6 +39,10 @@ def redirect_to(self, request, obj):
return reverse('authors_list')
+class AuthorCreateRestricted(AuthorCreate):
+ post = method_decorator(login_required)(AuthorCreate.post)
+
+
class AuthorUpdate(class_based_views.UpdateView):
queryset = Author.objects.all()
template_name = 'tests/detail.html'

0 comments on commit 3637cfe

Please sign in to comment.