comparison gpp/polls/views.py @ 439:1f139de929c4

Fixing #216; added anti-ballot stuffing feature to the polls application.
author Brian Neal <bgneal@gmail.com>
date Sat, 21 May 2011 19:55:48 +0000
parents dbd703f7d63a
children 8f46ba2f1b81
comparison
equal deleted inserted replaced
438:524fd1b3919a 439:1f139de929c4
1 """Views for the polls application""" 1 """
2 Views for the polls application.
2 3
3 from django.shortcuts import render_to_response 4 """
4 from django.template import RequestContext 5 from django.shortcuts import render
5 from django.contrib.auth.decorators import login_required 6 from django.contrib.auth.decorators import login_required
6 from django.shortcuts import get_object_or_404 7 from django.shortcuts import get_object_or_404
7 from django.http import Http404 8 from django.http import Http404
8 from django.http import HttpResponseRedirect 9 from django.http import HttpResponseRedirect
9 from django.core.urlresolvers import reverse 10 from django.core.urlresolvers import reverse
11 from django.views.decorators.http import require_POST
12 from django.db.models import F
10 13
11 from polls.models import Poll 14 from polls.models import Poll
12 from polls.models import Choice 15 from polls.models import Choice
13 from polls.forms import VoteForm 16 from polls.forms import VoteForm
14 17
15 ####################################################################### 18 #######################################################################
16 19
20 def get_user_choice(user, poll):
21 """
22 Return the Choice object the give user voted for from the given poll,
23 or None of no vote has been recorded (or the user is not authenticated.
24
25 """
26 user_choice = None
27 if user.is_authenticated():
28 user_choices = user.choice_set.filter(poll=poll)
29 if user_choices:
30 user_choice = user_choices[0]
31
32 return user_choice
33
34 #######################################################################
35
17 def poll_index(request): 36 def poll_index(request):
18 current_polls = Poll.objects.get_current_polls() 37 current_polls = Poll.objects.get_current_polls()
19 old_polls = Poll.objects.get_old_polls() 38 old_polls = Poll.objects.get_old_polls()
20 return render_to_response('polls/index.html', { 39 return render(request, 'polls/index.html', {
21 'current_polls': current_polls, 40 'current_polls': current_polls,
22 'old_polls': old_polls, 41 'old_polls': old_polls,
23 }, 42 })
24 context_instance = RequestContext(request))
25 43
26 ####################################################################### 44 #######################################################################
27 45
28 def poll_detail(request, poll_id): 46 def poll_detail(request, poll_id):
29 poll = get_object_or_404(Poll, pk = poll_id) 47 poll = get_object_or_404(Poll, pk=poll_id)
30 if not poll.is_enabled: 48 if not poll.is_enabled:
31 raise Http404 49 raise Http404
32 50
33 return render_to_response('polls/poll.html', { 51 return render(request, 'polls/poll.html', {
34 'poll': poll, 52 'poll': poll,
35 }, 53 'user_choice': get_user_choice(request.user, poll),
36 context_instance = RequestContext(request)) 54 })
37 55
38 ####################################################################### 56 #######################################################################
39 57
40 @login_required 58 @login_required
41 def poll_vote(request, poll_id): 59 def poll_vote(request, poll_id):
42 poll = get_object_or_404(Poll, pk = poll_id) 60 poll = get_object_or_404(Poll, pk=poll_id)
43 if not poll.is_enabled: 61 if not poll.is_enabled:
44 raise Http404 62 raise Http404
45 if not poll.is_open(): 63 if not poll.is_open():
46 return HttpResponseRedirect(reverse('polls.views.poll_results', args=[poll_id])) 64 return HttpResponseRedirect(reverse('polls-results', args=[poll_id]))
47 65
48 if request.method == "POST": 66 user_choice = get_user_choice(request.user, poll)
49 vote_form = VoteForm(poll, request.POST)
50 if vote_form.is_valid():
51 choice_id = request.POST.get('choices', None)
52 choice = get_object_or_404(Choice, pk = choice_id)
53 choice.votes += 1
54 choice.save()
55 return HttpResponseRedirect(reverse('polls.views.poll_results', args=[poll_id]))
56
57 vote_form = VoteForm(poll)
58 67
59 return render_to_response('polls/poll_vote.html', { 68 if request.method == "POST":
60 'poll': poll, 69 vote_form = VoteForm(poll, request.POST, user=request.user, user_choice=user_choice)
61 'vote_form': vote_form, 70 if vote_form.is_valid():
62 }, 71 vote_form.save()
63 context_instance = RequestContext(request)) 72 return HttpResponseRedirect(reverse('polls-results', args=[poll_id]))
73 else:
74 vote_form = VoteForm(poll)
75
76 return render(request, 'polls/poll_vote.html', {
77 'poll': poll,
78 'vote_form': vote_form,
79 'user_choice': user_choice,
80 })
64 81
65 ####################################################################### 82 #######################################################################
66 83
67 def poll_results(request, poll_id): 84 def poll_results(request, poll_id):
68 poll = get_object_or_404(Poll, pk = poll_id) 85 poll = get_object_or_404(Poll, pk=poll_id)
69 total_votes, choices = poll.results() 86 total_votes, choices = poll.results()
70 return render_to_response('polls/poll_results.html', { 87 return render(request, 'polls/poll_results.html', {
71 'poll': poll, 88 'poll': poll,
72 'total_votes': total_votes, 89 'total_votes': total_votes,
73 'choices': choices, 90 'choices': choices,
74 }, 91 'user_choice': get_user_choice(request.user, poll),
75 context_instance = RequestContext(request)) 92 })
76 93
77 ####################################################################### 94 #######################################################################
95
96 @require_POST
97 @login_required
98 def poll_delete_vote(request):
99 poll = get_object_or_404(Poll, pk=request.POST.get('poll_id'))
100 user_choice = get_user_choice(request.user, poll)
101 if user_choice:
102 Choice.objects.filter(id=user_choice.id).update(votes=F('votes') - 1)
103 user_choice.voters.remove(request.user)
104
105 return HttpResponseRedirect(reverse('polls-results', args=[poll.id]))