Add Book.ancestor m2m.
[wolnelektury.git] / apps / polls / views.py
1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 #
5 from django.views.decorators.http import require_http_methods\r
6 from django.shortcuts import get_object_or_404, redirect, render_to_response\r
7 from django.core.urlresolvers import reverse\r
8 from django.template import RequestContext
9
10 from .models import Poll, PollItem\r
11 from .forms import PollForm
12
13 \r
14 @require_http_methods(['GET', 'POST'])\r
15 def poll(request, slug):
16
17     poll = get_object_or_404(Poll, slug=slug, open=True)
18
19     if request.method == 'POST':\r
20         redirect_to = reverse('poll', args = [slug])
21         form = PollForm(request.POST, poll = poll)\r
22         if form.is_valid():\r
23             if not poll.voted(request.session):\r
24                 try:\r
25                     poll_item = PollItem.objects.filter(pk=form.cleaned_data['vote'], poll=poll).get()
26                 except PollItem.DoesNotExist:\r
27                     pass\r
28                 else:\r
29                     poll_item.vote(request.session)
30         return redirect(redirect_to)
31     elif request.method == 'GET':
32         context = RequestContext(request)
33         context['poll'] = poll\r
34         context['voted_already'] = poll.voted(request.session)
35         return render_to_response('polls/poll.html', context)