annotate polls/views.py @ 887:9a15f7c27526

Actually save model object upon change. This commit was tested on the comments model. Additional logging added. Added check for Markdown image references. Added TODOs after observing behavior on comments.
author Brian Neal <bgneal@gmail.com>
date Tue, 03 Feb 2015 21:09:44 -0600
parents ee87ea74d46b
children 71685387dd11
rev   line source
bgneal@439 1 """
bgneal@439 2 Views for the polls application.
gremmie@1 3
bgneal@439 4 """
bgneal@447 5 import datetime
bgneal@447 6
bgneal@439 7 from django.shortcuts import render
gremmie@1 8 from django.contrib.auth.decorators import login_required
gremmie@1 9 from django.shortcuts import get_object_or_404
gremmie@1 10 from django.http import Http404
gremmie@1 11 from django.http import HttpResponseRedirect
gremmie@1 12 from django.core.urlresolvers import reverse
bgneal@439 13 from django.views.decorators.http import require_POST
bgneal@439 14 from django.db.models import F
gremmie@1 15
gremmie@1 16 from polls.models import Poll
gremmie@1 17 from polls.models import Choice
gremmie@1 18 from polls.forms import VoteForm
gremmie@1 19
gremmie@1 20 #######################################################################
gremmie@1 21
bgneal@439 22 def get_user_choice(user, poll):
bgneal@439 23 """
bgneal@447 24 Return the Choice object the given user voted for from the given poll,
bgneal@447 25 or None if no vote has been recorded (or the user is not authenticated.
bgneal@439 26
bgneal@439 27 """
bgneal@439 28 user_choice = None
bgneal@439 29 if user.is_authenticated():
bgneal@439 30 user_choices = user.choice_set.filter(poll=poll)
bgneal@439 31 if user_choices:
bgneal@439 32 user_choice = user_choices[0]
bgneal@439 33
bgneal@439 34 return user_choice
bgneal@439 35
bgneal@439 36 #######################################################################
bgneal@439 37
gremmie@1 38 def poll_index(request):
bgneal@439 39 current_polls = Poll.objects.get_current_polls()
bgneal@439 40 old_polls = Poll.objects.get_old_polls()
bgneal@439 41 return render(request, 'polls/index.html', {
bgneal@439 42 'current_polls': current_polls,
bgneal@439 43 'old_polls': old_polls,
bgneal@439 44 })
gremmie@1 45
gremmie@1 46 #######################################################################
gremmie@1 47
gremmie@1 48 def poll_detail(request, poll_id):
bgneal@439 49 poll = get_object_or_404(Poll, pk=poll_id)
bgneal@447 50 if not poll.is_enabled or poll.start_date > datetime.datetime.now():
bgneal@439 51 raise Http404
gremmie@1 52
bgneal@447 53 total_votes, choices = poll.results()
bgneal@447 54
bgneal@447 55 return render(request, 'polls/poll_detail.html', {
bgneal@439 56 'poll': poll,
bgneal@447 57 'total_votes': total_votes,
bgneal@447 58 'choices': choices,
bgneal@439 59 'user_choice': get_user_choice(request.user, poll),
bgneal@439 60 })
gremmie@1 61
gremmie@1 62 #######################################################################
gremmie@1 63
gremmie@1 64 @login_required
gremmie@1 65 def poll_vote(request, poll_id):
bgneal@439 66 poll = get_object_or_404(Poll, pk=poll_id)
bgneal@439 67 if not poll.is_enabled:
bgneal@439 68 raise Http404
bgneal@439 69 if not poll.is_open():
bgneal@447 70 return HttpResponseRedirect(reverse('polls-detail',
bgneal@447 71 kwargs={'poll_id': poll_id}))
gremmie@1 72
bgneal@439 73 user_choice = get_user_choice(request.user, poll)
gremmie@1 74
bgneal@439 75 if request.method == "POST":
bgneal@447 76 vote_form = VoteForm(poll, request.POST, user=request.user,
bgneal@447 77 user_choice=user_choice)
bgneal@439 78 if vote_form.is_valid():
bgneal@439 79 vote_form.save()
bgneal@447 80 return HttpResponseRedirect(reverse('polls-detail',
bgneal@447 81 kwargs={'poll_id': poll_id}))
bgneal@439 82 else:
bgneal@439 83 vote_form = VoteForm(poll)
bgneal@439 84
bgneal@439 85 return render(request, 'polls/poll_vote.html', {
bgneal@439 86 'poll': poll,
bgneal@439 87 'vote_form': vote_form,
bgneal@439 88 'user_choice': user_choice,
bgneal@439 89 })
gremmie@1 90
gremmie@1 91 #######################################################################
gremmie@1 92
bgneal@439 93 @require_POST
bgneal@439 94 @login_required
bgneal@439 95 def poll_delete_vote(request):
bgneal@439 96 poll = get_object_or_404(Poll, pk=request.POST.get('poll_id'))
bgneal@439 97 user_choice = get_user_choice(request.user, poll)
bgneal@439 98 if user_choice:
bgneal@439 99 Choice.objects.filter(id=user_choice.id).update(votes=F('votes') - 1)
bgneal@439 100 user_choice.voters.remove(request.user)
bgneal@439 101
bgneal@447 102 return HttpResponseRedirect(reverse('polls-detail', kwargs={'poll_id': poll.id}))