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