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