bgneal@439: """ bgneal@439: Views for the polls application. gremmie@1: bgneal@439: """ bgneal@439: from django.shortcuts import render gremmie@1: from django.contrib.auth.decorators import login_required gremmie@1: from django.shortcuts import get_object_or_404 gremmie@1: from django.http import Http404 gremmie@1: from django.http import HttpResponseRedirect gremmie@1: from django.core.urlresolvers import reverse bgneal@439: from django.views.decorators.http import require_POST bgneal@439: from django.db.models import F gremmie@1: gremmie@1: from polls.models import Poll gremmie@1: from polls.models import Choice gremmie@1: from polls.forms import VoteForm gremmie@1: gremmie@1: ####################################################################### gremmie@1: bgneal@439: def get_user_choice(user, poll): bgneal@439: """ bgneal@439: Return the Choice object the give user voted for from the given poll, bgneal@439: or None of no vote has been recorded (or the user is not authenticated. bgneal@439: bgneal@439: """ bgneal@439: user_choice = None bgneal@439: if user.is_authenticated(): bgneal@439: user_choices = user.choice_set.filter(poll=poll) bgneal@439: if user_choices: bgneal@439: user_choice = user_choices[0] bgneal@439: bgneal@439: return user_choice bgneal@439: bgneal@439: ####################################################################### bgneal@439: gremmie@1: def poll_index(request): bgneal@439: current_polls = Poll.objects.get_current_polls() bgneal@439: old_polls = Poll.objects.get_old_polls() bgneal@439: return render(request, 'polls/index.html', { bgneal@439: 'current_polls': current_polls, bgneal@439: 'old_polls': old_polls, bgneal@439: }) gremmie@1: gremmie@1: ####################################################################### gremmie@1: gremmie@1: def poll_detail(request, poll_id): bgneal@439: poll = get_object_or_404(Poll, pk=poll_id) bgneal@439: if not poll.is_enabled: bgneal@439: raise Http404 gremmie@1: bgneal@439: return render(request, 'polls/poll.html', { bgneal@439: 'poll': poll, bgneal@439: 'user_choice': get_user_choice(request.user, poll), bgneal@439: }) gremmie@1: gremmie@1: ####################################################################### gremmie@1: gremmie@1: @login_required gremmie@1: def poll_vote(request, poll_id): bgneal@439: poll = get_object_or_404(Poll, pk=poll_id) bgneal@439: if not poll.is_enabled: bgneal@439: raise Http404 bgneal@439: if not poll.is_open(): bgneal@439: return HttpResponseRedirect(reverse('polls-results', args=[poll_id])) gremmie@1: bgneal@439: user_choice = get_user_choice(request.user, poll) gremmie@1: bgneal@439: if request.method == "POST": bgneal@439: vote_form = VoteForm(poll, request.POST, user=request.user, user_choice=user_choice) bgneal@439: if vote_form.is_valid(): bgneal@439: vote_form.save() bgneal@439: return HttpResponseRedirect(reverse('polls-results', args=[poll_id])) bgneal@439: else: bgneal@439: vote_form = VoteForm(poll) bgneal@439: bgneal@439: return render(request, 'polls/poll_vote.html', { bgneal@439: 'poll': poll, bgneal@439: 'vote_form': vote_form, bgneal@439: 'user_choice': user_choice, bgneal@439: }) gremmie@1: gremmie@1: ####################################################################### gremmie@1: gremmie@1: def poll_results(request, poll_id): bgneal@439: poll = get_object_or_404(Poll, pk=poll_id) bgneal@439: total_votes, choices = poll.results() bgneal@439: return render(request, 'polls/poll_results.html', { bgneal@439: 'poll': poll, bgneal@439: 'total_votes': total_votes, bgneal@439: 'choices': choices, bgneal@439: 'user_choice': get_user_choice(request.user, poll), bgneal@439: }) gremmie@1: gremmie@1: ####################################################################### bgneal@439: bgneal@439: @require_POST bgneal@439: @login_required bgneal@439: def poll_delete_vote(request): bgneal@439: poll = get_object_or_404(Poll, pk=request.POST.get('poll_id')) bgneal@439: user_choice = get_user_choice(request.user, poll) bgneal@439: if user_choice: bgneal@439: Choice.objects.filter(id=user_choice.id).update(votes=F('votes') - 1) bgneal@439: user_choice.voters.remove(request.user) bgneal@439: bgneal@439: return HttpResponseRedirect(reverse('polls-results', args=[poll.id]))