fix in librarian
[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.core.urlresolvers import reverse
6 from django.shortcuts import get_object_or_404, redirect, render_to_response
7 from django.template import RequestContext
8 from django.views.decorators import cache
9 from django.views.decorators.http import require_http_methods
10
11 from .models import Poll, PollItem
12 from .forms import PollForm
13
14
15 @cache.never_cache
16 @require_http_methods(['GET', 'POST'])
17 def poll(request, slug):
18
19     poll = get_object_or_404(Poll, slug=slug, open=True)
20
21     if request.method == 'POST':
22         redirect_to = reverse('poll', args = [slug])
23         form = PollForm(request.POST, poll = poll)
24         if form.is_valid():
25             if not poll.voted(request.session):
26                 try:
27                     poll_item = PollItem.objects.filter(pk=form.cleaned_data['vote'], poll=poll).get()
28                 except PollItem.DoesNotExist:
29                     pass
30                 else:
31                     poll_item.vote(request.session)
32         return redirect(redirect_to)
33     elif request.method == 'GET':
34         context = RequestContext(request)
35         context['poll'] = poll
36         context['voted_already'] = poll.voted(request.session)
37         return render_to_response('polls/poll.html', context)