1. Curtis Maloney
  2. gnocchi-forum

Commits

funkybob  committed 6f092ea

Change views to CBV

  • Participants
  • Parent commits 6091f48
  • Branches default

Comments (0)

Files changed (1)

File gnocchi/forum/views.py

View file
  • Ignore whitespace
-from django.views.generic.simple import direct_to_template
-from django.views.generic.list_detail import object_list
-from django.shortcuts import get_object_or_404
 from django.db.models import Max, Count
 from django import http
 from django.contrib.auth.decorators import permission_required
 
+from django.views import generic
+
 from gnocchi.forum import models, forms, settings
 
-# Need:
-#   Front page [Categories + Forums]
-def forum_index(request):
-    return direct_to_template(request, 'forum/index.html',
-        extra_context = {
-            # XXX Any reason to filter these?
-            'category_list': models.Category.objects.all()
-        }
-    )
+class CategoryList(generic.ListView):
+    model = models.Category
 
-#   Forum detail [ thread list ]
-def forum_detail(request, forum_slug, forum_id):
-    forum = get_object_or_404(models.Forum, pk=forum_id)
-    if not forum_slug == forum.slug:
-        return http.HttpResponsePermanentRedirect(forum.get_absolute_url())
-    # permssion check?
-    return object_list(request,
-        queryset = forum.thread_set.annotate(
-            most_recent_post=Max('post__created'),
-            post_count=Count('post')
-        ).all(),
-        template_name='forum/forum_detail.html',
-        extra_context = {
-            'forum': forum,
-        }
-    )
+class SlugIdMixin(object):
+    '''A mixin to look up objects by pk, but ensure the slug in the URL
+    matches.  Will redirect if it doesn't.'''
+    def get_object(self, queryset=None):
+        if queryset is None:
+            queryset = self.get_queryset()
+        try:
+            obj = queryset.get(pk=self.kwargs['pk'])
+        except self.model.DoesNotExist:
+            raise http.Http404('No matching record found!')
+        if not obj.slug == self.kwargs['slug']:
+            return self.redirect(obj.get_absolute_url())
+        return obj
 
-# Create new Thread
-@permission_required('thread.can_add')
-def thread_create(request, forum_id):
-    forum = get_object_or_404(models.Forum, pk=forum_id)
-    if not request.user.has_perm('create_thread', forum):
-        return http.HttpResponseForbidden()
+class ForumDetail(SlugIdMixin, generic.ListView):
+    model = models.Forum
 
-    if request.method == 'POST':
-        form = forms.NewThreadForm(request.POST)
-        if form.is_valid():
-            obj = form.save()
-            return http.HttpResponseRedirect(obj.get_absolute_url())
-    else:
-        form = forms.NewThreadForm()
-    return direct_to_template(request,
-        'forum/forum_form.html',
-        extra_context = {
-            'forum': forum,
-        }
-    )
+
+#@permission_required('thread.can_add')
+class ThreadCreate(SlugIdMixin, ModelFormMixin, ProcessFormView):
+    form_class = forms.NewThreadForm
+    template_name_suffix = '_form'
+
+    def get(self, request, *args, **kwargs):
+        self.forum = self.get_object()
+        return super(ThreadCreate, self).get(request, *args, **kwargs)
+    
+    def post(self, request, *args, **kwargs):
+        self.forum = self.get_object()
+        return super(ThreadCreate, self).post(request, *args, **kwargs)
+
+    def form_valid(self, form):
+        obj = form.save(commit=False)
+        obj.forum = self.forum
+        obj.save()
+        return http.HttpResponseRedirect(self.forum.get_absolute_url())
+
+    def get_context_data(self, **kwargs):
+        data = super(ThreadCreate, self).get_context_data(**kwargs)
+        data['forum'] = self.forum
+        return data
+
+class ThreadDetail(generic.ListView):
 
 #   Thread detail [ post list ]
 def thread_detail(request, thread_slug, thread_id):