1 from django.views.decorators.http import require_http_methods
\r
2 from django.shortcuts import get_object_or_404, redirect, render_to_response
\r
3 from django.core.urlresolvers import reverse
\r
4 from django.template import RequestContext
6 from models import Poll, PollItem
\r
7 from forms import PollForm
10 @require_http_methods(['GET', 'POST'])
\r
11 def poll(request, slug):
13 poll = get_object_or_404(Poll, slug = slug, open = True)
15 if request.method == 'POST':
\r
16 redirect_to = reverse('poll', args = [slug])
17 form = PollForm(request.POST, poll = poll)
\r
19 if not poll.voted(request.session):
\r
21 poll_item = PollItem.objects.filter(pk = form.cleaned_data['vote'], poll = poll).get()
22 except PollItem.DoesNotExist:
\r
25 poll_item.vote(request.session)
26 return redirect(redirect_to)
27 elif request.method == 'GET':
28 context = RequestContext(request)
29 context['poll'] = poll
\r
30 context['voted_already'] = poll.voted(request.session)
31 return render_to_response('polls/poll.html', context)