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):
18 poll = get_object_or_404(Poll, slug=slug, open=True)
20 if request.method == 'POST':
21 redirect_to = reverse('poll', args=[slug])
22 form = PollForm(request.POST, poll=poll)
24 if not poll.voted(request.session):
26 poll_item = PollItem.objects.filter(pk=form.cleaned_data['vote'], poll=poll).get()
27 except PollItem.DoesNotExist:
30 poll_item.vote(request.session)
31 return redirect(redirect_to)
32 elif request.method == 'GET':
33 context = RequestContext(request)
34 context['poll'] = poll
35 context['voted_already'] = poll.voted(request.session)
36 return render_to_response('polls/poll.html', context)