diff gpp/polls/views.py @ 447:8f46ba2f1b81

For #219, rework the polls views for a better flow. Added some tests for the views.
author Brian Neal <bgneal@gmail.com>
date Sat, 25 Jun 2011 23:23:20 +0000
parents 1f139de929c4
children
line wrap: on
line diff
--- a/gpp/polls/views.py	Mon Jun 13 02:19:13 2011 +0000
+++ b/gpp/polls/views.py	Sat Jun 25 23:23:20 2011 +0000
@@ -2,6 +2,8 @@
 Views for the polls application.
 
 """
+import datetime
+
 from django.shortcuts import render
 from django.contrib.auth.decorators import login_required
 from django.shortcuts import get_object_or_404
@@ -19,8 +21,8 @@
 
 def get_user_choice(user, poll):
     """
-    Return the Choice object the give user voted for from the given poll,
-    or None of no vote has been recorded (or the user is not authenticated.
+    Return the Choice object the given user voted for from the given poll,
+    or None if no vote has been recorded (or the user is not authenticated.
 
     """
     user_choice = None
@@ -45,11 +47,15 @@
 
 def poll_detail(request, poll_id):
     poll = get_object_or_404(Poll, pk=poll_id)
-    if not poll.is_enabled:
+    if not poll.is_enabled or poll.start_date > datetime.datetime.now():
         raise Http404
 
-    return render(request, 'polls/poll.html', {
+    total_votes, choices = poll.results()
+
+    return render(request, 'polls/poll_detail.html', {
         'poll': poll,
+        'total_votes': total_votes,
+        'choices': choices,
         'user_choice': get_user_choice(request.user, poll),
         })
 
@@ -61,15 +67,18 @@
     if not poll.is_enabled:
         raise Http404
     if not poll.is_open():
-        return HttpResponseRedirect(reverse('polls-results', args=[poll_id]))
+        return HttpResponseRedirect(reverse('polls-detail',
+                                            kwargs={'poll_id': poll_id}))
 
     user_choice = get_user_choice(request.user, poll)
 
     if request.method == "POST":
-        vote_form = VoteForm(poll, request.POST, user=request.user, user_choice=user_choice)
+        vote_form = VoteForm(poll, request.POST, user=request.user,
+                             user_choice=user_choice)
         if vote_form.is_valid():
             vote_form.save()
-            return HttpResponseRedirect(reverse('polls-results', args=[poll_id]))
+            return HttpResponseRedirect(reverse('polls-detail',
+                                                kwargs={'poll_id': poll_id}))
     else:
         vote_form = VoteForm(poll)
 
@@ -81,18 +90,6 @@
 
 #######################################################################
 
-def poll_results(request, poll_id):
-    poll = get_object_or_404(Poll, pk=poll_id)
-    total_votes, choices = poll.results()
-    return render(request, 'polls/poll_results.html', {
-        'poll': poll,
-        'total_votes': total_votes,
-        'choices': choices,
-        'user_choice': get_user_choice(request.user, poll),
-        })
-
-#######################################################################
-
 @require_POST
 @login_required
 def poll_delete_vote(request):
@@ -102,4 +99,4 @@
         Choice.objects.filter(id=user_choice.id).update(votes=F('votes') - 1)
         user_choice.voters.remove(request.user)
 
-    return HttpResponseRedirect(reverse('polls-results', args=[poll.id]))
+    return HttpResponseRedirect(reverse('polls-detail', kwargs={'poll_id': poll.id}))