Commits

funkybob  committed dc0872d

Shifted to class based views

  • Participants
  • Parent commits ee2edf0

Comments (0)

Files changed (2)

File gnocchi_blog/urls.py

 from django.conf.urls.defaults import patterns, url
 
+from gnocchi_blog.views import PostList, PostDetail
+
 urlpatterns = patterns('gnocchi_blog.views',
     url(r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\w{1,2})/(?P<slug>[\w-]+)/$',
-        'blog_detail',
+        PostDetail.as_view(),
         name='blog-detail',
     ),
-    url(r'^$', 'blog_list', ),
+    url(r'^$', PostList.as_view(),),
 )

File gnocchi_blog/views.py

 from django.views.generic import date_based, list_detail
 from django.views.generic.dates import BaseDateListView, DateDetailView
 
-from gnocchi_blog import models
+from gnocchi_blog import models, forms
 
 # This will roll for django-1.3
 class PostMixin(object):
 class PostDetail(DateDetailView, PostMixin):
     template_name = 'blog/post_detail.html'
 
-def _build_queryset(request, username=None, **kw):
-    qset = models.Post.objects.current()
-    if username:
-        qset = qset.filter(posted_by__username=username)
-    tags = request.GET.getlist('tags')
-    if tags:
-        return qset.filter(tags__in=tags)
-    return qset
+    def post(self, request, *args, **kwargs):
+        self.object = self.get_object()
+        form = forms.CommentForm(request.META['REQUEST_ADDR'], request.POST)
+        if form.is_valid():
+            comment = form.save(commit=False)
+            comment.post = self.object
+            if request.user.is_authenticated():
+                comment.user = request.user
+            comment.save()
+            return HttpResponseRedirect(obj.get_absolute_url())
+        context = self.get_context_data(object=self.object)
+        context['comment_form'] = form
+        return self.render_to_response(context)
 
-def blog_detail(request, *a, **kw):
-    return date_based.object_detail(request,
-        queryset=_build_queryset(request, *a, **kw),
-        date_field='post_date',
-        slug_field='slug',
-        *a, **kw)
-
-def blog_list(request, *a, **kw):
-    return list_detail.object_list(request,
-        queryset=_build_queryset(request, *a, **kw),
-        *a, **kw)
+    def get_context_data(self, **kwargs):
+        context = super(PostDetail, self).get_context_data(**kwargs)
+        if not self.request.method == 'POST':
+            context['comment_form'] = forms.CommentForm()
+        return context