Mercurial > public > sg101
comparison polls/views.py @ 581:ee87ea74d46b
For Django 1.4, rearranged project structure for new manage.py.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 05 May 2012 17:10:48 -0500 |
parents | gpp/polls/views.py@8f46ba2f1b81 |
children | 71685387dd11 |
comparison
equal
deleted
inserted
replaced
580:c525f3e0b5d0 | 581:ee87ea74d46b |
---|---|
1 """ | |
2 Views for the polls application. | |
3 | |
4 """ | |
5 import datetime | |
6 | |
7 from django.shortcuts import render | |
8 from django.contrib.auth.decorators import login_required | |
9 from django.shortcuts import get_object_or_404 | |
10 from django.http import Http404 | |
11 from django.http import HttpResponseRedirect | |
12 from django.core.urlresolvers import reverse | |
13 from django.views.decorators.http import require_POST | |
14 from django.db.models import F | |
15 | |
16 from polls.models import Poll | |
17 from polls.models import Choice | |
18 from polls.forms import VoteForm | |
19 | |
20 ####################################################################### | |
21 | |
22 def get_user_choice(user, poll): | |
23 """ | |
24 Return the Choice object the given user voted for from the given poll, | |
25 or None if no vote has been recorded (or the user is not authenticated. | |
26 | |
27 """ | |
28 user_choice = None | |
29 if user.is_authenticated(): | |
30 user_choices = user.choice_set.filter(poll=poll) | |
31 if user_choices: | |
32 user_choice = user_choices[0] | |
33 | |
34 return user_choice | |
35 | |
36 ####################################################################### | |
37 | |
38 def poll_index(request): | |
39 current_polls = Poll.objects.get_current_polls() | |
40 old_polls = Poll.objects.get_old_polls() | |
41 return render(request, 'polls/index.html', { | |
42 'current_polls': current_polls, | |
43 'old_polls': old_polls, | |
44 }) | |
45 | |
46 ####################################################################### | |
47 | |
48 def poll_detail(request, poll_id): | |
49 poll = get_object_or_404(Poll, pk=poll_id) | |
50 if not poll.is_enabled or poll.start_date > datetime.datetime.now(): | |
51 raise Http404 | |
52 | |
53 total_votes, choices = poll.results() | |
54 | |
55 return render(request, 'polls/poll_detail.html', { | |
56 'poll': poll, | |
57 'total_votes': total_votes, | |
58 'choices': choices, | |
59 'user_choice': get_user_choice(request.user, poll), | |
60 }) | |
61 | |
62 ####################################################################### | |
63 | |
64 @login_required | |
65 def poll_vote(request, poll_id): | |
66 poll = get_object_or_404(Poll, pk=poll_id) | |
67 if not poll.is_enabled: | |
68 raise Http404 | |
69 if not poll.is_open(): | |
70 return HttpResponseRedirect(reverse('polls-detail', | |
71 kwargs={'poll_id': poll_id})) | |
72 | |
73 user_choice = get_user_choice(request.user, poll) | |
74 | |
75 if request.method == "POST": | |
76 vote_form = VoteForm(poll, request.POST, user=request.user, | |
77 user_choice=user_choice) | |
78 if vote_form.is_valid(): | |
79 vote_form.save() | |
80 return HttpResponseRedirect(reverse('polls-detail', | |
81 kwargs={'poll_id': poll_id})) | |
82 else: | |
83 vote_form = VoteForm(poll) | |
84 | |
85 return render(request, 'polls/poll_vote.html', { | |
86 'poll': poll, | |
87 'vote_form': vote_form, | |
88 'user_choice': user_choice, | |
89 }) | |
90 | |
91 ####################################################################### | |
92 | |
93 @require_POST | |
94 @login_required | |
95 def poll_delete_vote(request): | |
96 poll = get_object_or_404(Poll, pk=request.POST.get('poll_id')) | |
97 user_choice = get_user_choice(request.user, poll) | |
98 if user_choice: | |
99 Choice.objects.filter(id=user_choice.id).update(votes=F('votes') - 1) | |
100 user_choice.voters.remove(request.user) | |
101 | |
102 return HttpResponseRedirect(reverse('polls-detail', kwargs={'poll_id': poll.id})) |