comparison gpp/polls/views.py @ 1:dbd703f7d63a

Initial import of sg101 stuff from private repository.
author gremmie
date Mon, 06 Apr 2009 02:43:12 +0000
parents
children 1f139de929c4
comparison
equal deleted inserted replaced
0:900ba3c7b765 1:dbd703f7d63a
1 """Views for the polls application"""
2
3 from django.shortcuts import render_to_response
4 from django.template import RequestContext
5 from django.contrib.auth.decorators import login_required
6 from django.shortcuts import get_object_or_404
7 from django.http import Http404
8 from django.http import HttpResponseRedirect
9 from django.core.urlresolvers import reverse
10
11 from polls.models import Poll
12 from polls.models import Choice
13 from polls.forms import VoteForm
14
15 #######################################################################
16
17 def poll_index(request):
18 current_polls = Poll.objects.get_current_polls()
19 old_polls = Poll.objects.get_old_polls()
20 return render_to_response('polls/index.html', {
21 'current_polls': current_polls,
22 'old_polls': old_polls,
23 },
24 context_instance = RequestContext(request))
25
26 #######################################################################
27
28 def poll_detail(request, poll_id):
29 poll = get_object_or_404(Poll, pk = poll_id)
30 if not poll.is_enabled:
31 raise Http404
32
33 return render_to_response('polls/poll.html', {
34 'poll': poll,
35 },
36 context_instance = RequestContext(request))
37
38 #######################################################################
39
40 @login_required
41 def poll_vote(request, poll_id):
42 poll = get_object_or_404(Poll, pk = poll_id)
43 if not poll.is_enabled:
44 raise Http404
45 if not poll.is_open():
46 return HttpResponseRedirect(reverse('polls.views.poll_results', args=[poll_id]))
47
48 if request.method == "POST":
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
59 return render_to_response('polls/poll_vote.html', {
60 'poll': poll,
61 'vote_form': vote_form,
62 },
63 context_instance = RequestContext(request))
64
65 #######################################################################
66
67 def poll_results(request, poll_id):
68 poll = get_object_or_404(Poll, pk = poll_id)
69 total_votes, choices = poll.results()
70 return render_to_response('polls/poll_results.html', {
71 'poll': poll,
72 'total_votes': total_votes,
73 'choices': choices,
74 },
75 context_instance = RequestContext(request))
76
77 #######################################################################