fix dictionary for ptrad
[wolnelektury.git] / src / polls / views.py
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.
3 #
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
8
9 from .models import Poll, PollItem
10 from .forms import PollForm
11
12
13 @cache.never_cache
14 @require_http_methods(['GET', 'POST'])
15 def poll(request, slug):
16     poll = get_object_or_404(Poll, slug=slug, open=True)
17
18     if request.method == 'POST':
19         redirect_to = reverse('poll', args=[slug])
20         form = PollForm(request.POST, poll=poll)
21         if form.is_valid():
22             if not poll.voted(request.session):
23                 try:
24                     poll_item = PollItem.objects.filter(pk=form.cleaned_data['vote'], poll=poll).get()
25                 except PollItem.DoesNotExist:
26                     pass
27                 else:
28                     poll_item.vote(request.session)
29         return redirect(redirect_to)
30     elif request.method == 'GET':
31         context = {}
32         context['poll'] = poll
33         context['voted_already'] = poll.voted(request.session)
34         return render(request, 'polls/poll.html', context)