~widelands-dev/widelands-website/django_staticfiles

« back to all changes in this revision

Viewing changes to wlpoll/views.py

  • Committer: franku
  • Date: 2016-12-13 18:28:51 UTC
  • mto: This revision was merged to the branch mainline in revision 443.
  • Revision ID: somal@arcor.de-20161213182851-bo5ebf8pdvw5beua
run the script

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
from models import Poll, Choice, Vote
7
7
from django.views import generic
8
8
 
 
9
 
9
10
class DetailView(generic.DetailView):
10
11
    model = Poll
11
12
    template_name = 'wlpoll/poll_detail.html'
12
13
 
13
14
 
14
 
#class ResultsView(generic.DetailView):
 
15
# class ResultsView(generic.DetailView):
15
16
#    model = Poll
16
17
#    template_name = 'polls/results.html'
17
18
 
18
19
@login_required
19
 
def vote(request, object_id, next = None):
20
 
    if request.method == "GET":
21
 
        return HttpResponseNotAllowed(["POST"])
 
20
def vote(request, object_id, next=None):
 
21
    if request.method == 'GET':
 
22
        return HttpResponseNotAllowed(['POST'])
22
23
 
23
 
    p = get_object_or_404(Poll,pk=object_id)
 
24
    p = get_object_or_404(Poll, pk=object_id)
24
25
 
25
26
    user = request.user
26
27
    if user.poll_votes.filter(poll=p):
27
28
        return HttpResponseForbidden("Can't vote more than once")
28
29
 
29
 
 
30
 
    if not p.is_closed() and "choice_id" in request.POST:
31
 
        c = get_object_or_404(Choice, pk=int(request.POST["choice_id"]),poll=p)
 
30
    if not p.is_closed() and 'choice_id' in request.POST:
 
31
        c = get_object_or_404(Choice, pk=int(
 
32
            request.POST['choice_id']), poll=p)
32
33
 
33
34
        c.votes += 1
34
35
        c.save()
35
36
 
36
37
        v = Vote.objects.create(
37
 
            user = user,
38
 
            poll = p,
39
 
            choice = c
 
38
            user=user,
 
39
            poll=p,
 
40
            choice=c
40
41
        )
41
42
        v.save()
42
43
 
43
 
 
44
 
    return HttpResponseRedirect(reverse("wlpoll_detail", args = (p.id,)))
 
44
    return HttpResponseRedirect(reverse('wlpoll_detail', args=(p.id,)))