1 # This file is part of Wolne Lektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
4 from django.shortcuts import get_object_or_404, redirect, render
5 from django.urls import reverse
6 from django.views.decorators import cache
7 from django.views.decorators.http import require_http_methods
9 from .models import Poll, PollItem
10 from .forms import PollForm
14 @require_http_methods(['GET', 'POST'])
15 def poll(request, slug):
16 poll = get_object_or_404(Poll, slug=slug, open=True)
18 if request.method == 'POST':
19 redirect_to = reverse('poll', args=[slug])
20 form = PollForm(request.POST, poll=poll)
22 if not poll.voted(request.session):
24 poll_item = PollItem.objects.filter(pk=form.cleaned_data['vote'], poll=poll).get()
25 except PollItem.DoesNotExist:
28 poll_item.vote(request.session)
29 return redirect(redirect_to)
30 elif request.method == 'GET':
32 context['poll'] = poll
33 context['voted_already'] = poll.voted(request.session)
34 return render(request, 'polls/poll.html', context)