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.
5 from django.core.urlresolvers import reverse
6 from django.shortcuts import get_object_or_404, redirect, render
7 from django.views.decorators import cache
8 from django.views.decorators.http import require_http_methods
10 from .models import Poll, PollItem
11 from .forms import PollForm
15 @require_http_methods(['GET', 'POST'])
16 def poll(request, slug):
17 poll = get_object_or_404(Poll, slug=slug, open=True)
19 if request.method == 'POST':
20 redirect_to = reverse('poll', args=[slug])
21 form = PollForm(request.POST, poll=poll)
23 if not poll.voted(request.session):
25 poll_item = PollItem.objects.filter(pk=form.cleaned_data['vote'], poll=poll).get()
26 except PollItem.DoesNotExist:
29 poll_item.vote(request.session)
30 return redirect(redirect_to)
31 elif request.method == 'GET':
33 context['poll'] = poll
34 context['voted_already'] = poll.voted(request.session)
35 return render(request, 'polls/poll.html', context)