Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Replace deprecated generic FBV with equivalent CBV in weblog.

  • Loading branch information...
commit 4e6d0c056ba1e706cad03ca9eef65fa6c446de3b 1 parent d82240a
@aaugustin aaugustin authored
Showing with 37 additions and 32 deletions.
  1. +16 −5 blog/urls.py
  2. +21 −27 blog/views.py
View
21 blog/urls.py
@@ -5,9 +5,20 @@
from . import views
urlpatterns = patterns('',
- (r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\w{1,2})/(?P<slug>[\w-]+)/$', views.entry_detail),
- (r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\w{1,2})/$', views.archive_day),
- (r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/$', views.archive_month),
- (r'^(?P<year>\d{4})/$', views.archive_year),
- url(r'^/?$', views.archive_index, name="blog-index"),
+ url(r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\w{1,2})/(?P<slug>[\w-]+)/$',
+ views.BlogDateDetailView.as_view()
+ ),
+ url(r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\w{1,2})/$',
+ views.BlogDayArchiveView.as_view()
+ ),
+ url(r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/$',
+ views.BlogMonthArchiveView.as_view()
+ ),
+ url(r'^(?P<year>\d{4})/$',
+ views.BlogYearArchiveView.as_view()
+ ),
+ url(r'^/?$',
+ views.BlogArchiveIndexView.as_view(),
+ name="blog-index"
+ ),
)
View
48 blog/views.py
@@ -1,38 +1,32 @@
-import functools
-from django.views.generic import date_based
+from django.views.generic.dates import (ArchiveIndexView, YearArchiveView,
+ MonthArchiveView, DayArchiveView, DateDetailView)
from .models import Entry
-def prepare_arguments(view):
- @functools.wraps(view)
- def wrapped(request, *args, **kwargs):
- kwargs['allow_future'] = request.user.is_staff
- kwargs['queryset'] = Entry.objects.all() if request.user.is_staff else Entry.objects.published()
- kwargs['date_field'] = 'pub_date'
- return view(request, *args, **kwargs)
- return wrapped
+class BlogViewMixin(object):
-@prepare_arguments
-def entry_detail(request, *args, **kwargs):
- return date_based.object_detail(request, *args, **kwargs)
+ date_field = 'pub_date'
-@prepare_arguments
-def archive_day(request, *args, **kwargs):
- return date_based.archive_day(request, *args, **kwargs)
+ def get_allow_future(self):
+ return self.request.user.is_staff
-@prepare_arguments
-def archive_month(request, *args, **kwargs):
- return date_based.archive_month(request, *args, **kwargs)
-
-@prepare_arguments
-def archive_year(request, *args, **kwargs):
- return date_based.archive_year(request, *args, **kwargs)
-
-@prepare_arguments
-def archive_index(request, *args, **kwargs):
- return date_based.archive_index(request, *args, **kwargs)
+ def get_queryset(self):
+ if self.request.user.is_staff:
+ return Entry.objects.all()
+ else:
+ return Entry.objects.published()
+class BlogArchiveIndexView(BlogViewMixin, ArchiveIndexView):
+ pass
+class BlogYearArchiveView(BlogViewMixin, YearArchiveView):
+ pass
+class BlogMonthArchiveView(BlogViewMixin, MonthArchiveView):
+ pass
+class BlogDayArchiveView(BlogViewMixin, DayArchiveView):
+ pass
+class BlogDateDetailView(BlogViewMixin, DateDetailView):
+ pass
Please sign in to comment.
Something went wrong with that request. Please try again.