ac1c3602841da7410e27bf4355dbc45ae7c0fc55
[wolnelektury.git] / apps / polls / views.py
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
5
6 from models import Poll, PollItem\r
7 from forms import PollForm
8
9 \r
10 @require_http_methods(['GET', 'POST'])\r
11 def poll(request, slug):
12     
13     poll = get_object_or_404(Poll, slug = slug)
14     
15     if request.method == 'POST':\r
16         redirect_to = reverse('poll', args = [slug])
17         form = PollForm(request.POST, poll = poll)\r
18         if form.is_valid():\r
19             if not poll.voted(request.session):\r
20                 try:\r
21                     poll_item = PollItem.objects.filter(pk = form.cleaned_data['vote'], poll = poll).get()
22                 except PollItem.DoesNotExist:\r
23                     pass\r
24                 else:\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)
32         
33         \r
34