comparison 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
comparison
equal deleted inserted replaced
446:72ef6e809f79 447:8f46ba2f1b81
1 """ 1 """
2 Views for the polls application. 2 Views for the polls application.
3 3
4 """ 4 """
5 import datetime
6
5 from django.shortcuts import render 7 from django.shortcuts import render
6 from django.contrib.auth.decorators import login_required 8 from django.contrib.auth.decorators import login_required
7 from django.shortcuts import get_object_or_404 9 from django.shortcuts import get_object_or_404
8 from django.http import Http404 10 from django.http import Http404
9 from django.http import HttpResponseRedirect 11 from django.http import HttpResponseRedirect
17 19
18 ####################################################################### 20 #######################################################################
19 21
20 def get_user_choice(user, poll): 22 def get_user_choice(user, poll):
21 """ 23 """
22 Return the Choice object the give user voted for from the given poll, 24 Return the Choice object the given user voted for from the given poll,
23 or None of no vote has been recorded (or the user is not authenticated. 25 or None if no vote has been recorded (or the user is not authenticated.
24 26
25 """ 27 """
26 user_choice = None 28 user_choice = None
27 if user.is_authenticated(): 29 if user.is_authenticated():
28 user_choices = user.choice_set.filter(poll=poll) 30 user_choices = user.choice_set.filter(poll=poll)
43 45
44 ####################################################################### 46 #######################################################################
45 47
46 def poll_detail(request, poll_id): 48 def poll_detail(request, poll_id):
47 poll = get_object_or_404(Poll, pk=poll_id) 49 poll = get_object_or_404(Poll, pk=poll_id)
48 if not poll.is_enabled: 50 if not poll.is_enabled or poll.start_date > datetime.datetime.now():
49 raise Http404 51 raise Http404
50 52
51 return render(request, 'polls/poll.html', { 53 total_votes, choices = poll.results()
54
55 return render(request, 'polls/poll_detail.html', {
52 'poll': poll, 56 'poll': poll,
57 'total_votes': total_votes,
58 'choices': choices,
53 'user_choice': get_user_choice(request.user, poll), 59 'user_choice': get_user_choice(request.user, poll),
54 }) 60 })
55 61
56 ####################################################################### 62 #######################################################################
57 63
59 def poll_vote(request, poll_id): 65 def poll_vote(request, poll_id):
60 poll = get_object_or_404(Poll, pk=poll_id) 66 poll = get_object_or_404(Poll, pk=poll_id)
61 if not poll.is_enabled: 67 if not poll.is_enabled:
62 raise Http404 68 raise Http404
63 if not poll.is_open(): 69 if not poll.is_open():
64 return HttpResponseRedirect(reverse('polls-results', args=[poll_id])) 70 return HttpResponseRedirect(reverse('polls-detail',
71 kwargs={'poll_id': poll_id}))
65 72
66 user_choice = get_user_choice(request.user, poll) 73 user_choice = get_user_choice(request.user, poll)
67 74
68 if request.method == "POST": 75 if request.method == "POST":
69 vote_form = VoteForm(poll, request.POST, user=request.user, user_choice=user_choice) 76 vote_form = VoteForm(poll, request.POST, user=request.user,
77 user_choice=user_choice)
70 if vote_form.is_valid(): 78 if vote_form.is_valid():
71 vote_form.save() 79 vote_form.save()
72 return HttpResponseRedirect(reverse('polls-results', args=[poll_id])) 80 return HttpResponseRedirect(reverse('polls-detail',
81 kwargs={'poll_id': poll_id}))
73 else: 82 else:
74 vote_form = VoteForm(poll) 83 vote_form = VoteForm(poll)
75 84
76 return render(request, 'polls/poll_vote.html', { 85 return render(request, 'polls/poll_vote.html', {
77 'poll': poll, 86 'poll': poll,
78 'vote_form': vote_form, 87 'vote_form': vote_form,
79 'user_choice': user_choice, 88 'user_choice': user_choice,
80 })
81
82 #######################################################################
83
84 def poll_results(request, poll_id):
85 poll = get_object_or_404(Poll, pk=poll_id)
86 total_votes, choices = poll.results()
87 return render(request, 'polls/poll_results.html', {
88 'poll': poll,
89 'total_votes': total_votes,
90 'choices': choices,
91 'user_choice': get_user_choice(request.user, poll),
92 }) 89 })
93 90
94 ####################################################################### 91 #######################################################################
95 92
96 @require_POST 93 @require_POST
100 user_choice = get_user_choice(request.user, poll) 97 user_choice = get_user_choice(request.user, poll)
101 if user_choice: 98 if user_choice:
102 Choice.objects.filter(id=user_choice.id).update(votes=F('votes') - 1) 99 Choice.objects.filter(id=user_choice.id).update(votes=F('votes') - 1)
103 user_choice.voters.remove(request.user) 100 user_choice.voters.remove(request.user)
104 101
105 return HttpResponseRedirect(reverse('polls-results', args=[poll.id])) 102 return HttpResponseRedirect(reverse('polls-detail', kwargs={'poll_id': poll.id}))