Commits

Anonymous committed 8d04470

Converted entirely to class based views

  • Participants
  • Parent commits c1556b4

Comments (0)

Files changed (2)

gnocchi/ask/urls.py

         permission_required('ask.add_response')(QuestionRespond.as_view()),
         name='question-response'
     ),
-    url(r'^accept/(?P<response_id>\d+)/$', 'response_accept',
+    url(r'^accept/(?P<response_id>\d+)/$',
+        ResponseAccept.as_view(),
         name='response-accept'
     ),
-    url(r'^vote/(?P<response_id>\d+)/(?P<direction>up|down)/$', 'question_vote',
+    url(r'^vote/(?P<response_id>\d+)/(?P<direction>up|down)/$',
+        permission_required('ask.add_vote')(ResponseVote.as_view()),
         name='question-vote'
     ),
 )

gnocchi/ask/views.py

 from django.views import generic
 from django.views.generic.edit import SingleObjectMixin
-from django.contrib.auth.decorators import permission_required
-from django.http import HttpResponseRedirect
+from django.core.exceptions import ObjectDoesNotExist
+from django.http import HttpResponseRedirect, Http404
 from django.contrib import messages
-from django.shortcuts import get_object_or_404
 import shlex
 from django.db.models import Q
 from gnocchi.ask import settings, models, forms
 
 __all__ = [
     'QuestionList', 'QuestionDetail', 'QuestionAsk', 'QuestionRespond',
-    'question_vote', 'response_accept',
+    'ResponseVote', 'ResponseAccept',
 ]
 class QuestionList(generic.ListView):
     model = models.Question
         form.save_m2m()
         return HttpResponseRedirect(obj.get_absolute_url())
 
+class ResponseMixin(SingleObjectMixin):
+    '''Operate on Responses of questions, where pk is the Question,
+    and response_id is the Response'''
+    model = models.Question
+    def get_object(self, queryset=None):
+        self.question = super(ResponseMixin, self).get_object(queryset)
+        try:
+            obj = self.question.responses.get(pk=self.kwargs['response_id'])
+        except ObjectDoesNotExist:
+            raise Http404
+        return obj
+
 class QuestionRespond(generic.CreateView):
     form_class = forms.AddResponseForm
     model = models.Question	# So that get_object looks for the right thing
     'down': -1,
 }
 
-@permission_required('response.can_vote')
-def question_vote(request, pk, response_id, direction, weight=1):
-    response = get_object_or_404(models.Response, pk=pk,
-        question__id=question_id)
+class ResponseVote(ResponseMixin, generic.View):
+    def get(self, request, direction, weight=1, *args, **kwargs):
+        self.object = self.get_object()
+        weight = DIRECTION_WEIGHT[direction] * weight
 
-    weight = DIRECTION_WEIGHT[direction] * weight
+        vote, created = models.Vote.objects.get_or_create(user=request.user,
+            response=self.object,
+            defaults={'weight': weight}
+        )
 
-    vote, created = models.Vote.objects.get_or_create(user=request.user,
-        response=response,
-        defaults={'weight': weight}
-    )
+        if not created:
+            if vote.weight == weight:
+                # Voted the same
+                pass
+            elif abs(vote.weight) == abs(weight):
+                # Voted opposite
+                vote.delete()
+            else:
+                vote.weight = weight
+                vote.save()
+        return HttpResponseRedirect(self.question.get_absolute_url())
 
-    if not created:
-        if vote.weight == weight:
-            # Voted the same
-            pass
-        elif abs(vote.weight) == abs(weight):
-            # Voted opposite
-            vote.delete()
+class ResponseAccept(ResponseMixin, generic.View):
+    '''Mark a response as the Accepted correct one.'''
+    def get(self, request, *args, **kwargs):
+        self.object = self.get_object()
+
+        if self.object.question.user == request.user:
+            self.object.question.accepted = self.object
+            self.object.question.save()
         else:
-            vote.weight = weight
-            vote.save()
-
-    return HttpResponseRedirect(response.question.get_absolute_url())
-
-def response_accept(request, pk, response_id):
-    response = get_object_or_404(models.Response, pk=pk, question__id=pk)
-
-    if response.question.user == request.user:
-        response.question.accepted = response
-        response.question.save()
-    else:
-        messages.add_message(request, messages.WARNING,
-            "You may not accept a response for a question you didn't ask."
-        )
-    return HttpResponseRedirect(response.question.get_absolute_url())
+            messages.add_message(request, messages.WARNING,
+                "You may not accept a response for a question you didn't ask."
+            )
+        return HttpResponseRedirect(self.object.question.get_absolute_url())