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_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
11 from .models import Poll, PollItem
12 from .forms import PollForm
16 @require_http_methods(['GET', 'POST'])
17 def poll(request, slug):
19 poll = get_object_or_404(Poll, slug=slug, open=True)
21 if request.method == 'POST':
22 redirect_to = reverse('poll', args = [slug])
23 form = PollForm(request.POST, poll = poll)
25 if not poll.voted(request.session):
27 poll_item = PollItem.objects.filter(pk=form.cleaned_data['vote'], poll=poll).get()
28 except PollItem.DoesNotExist:
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)