bgneal@439: """ bgneal@439: Views for the polls application. gremmie@1: bgneal@439: """ bgneal@447: import datetime bgneal@447: 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@447: Return the Choice object the given user voted for from the given poll, bgneal@447: or None if 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@1090: bgneal@1090: # Do some stuff manually to avoid cascading hits to the database bgneal@1090: now = datetime.datetime.now() bgneal@1090: qs = Poll.objects.filter(is_enabled=True) bgneal@1090: poll_dict = {} bgneal@1090: current_polls = [] bgneal@1090: old_polls = [] bgneal@1090: for poll in qs: bgneal@1090: poll.total_votes_ = 0 bgneal@1090: poll_dict[poll.pk] = poll bgneal@1090: if (poll.start_date <= now and bgneal@1090: (poll.end_date is None or poll.end_date >= now)): bgneal@1090: current_polls.append(poll) bgneal@1090: elif (poll.start_date < now and bgneal@1090: (poll.end_date is not None and poll.end_date < now)): bgneal@1090: old_polls.append(poll) bgneal@1090: bgneal@1090: for choice in Choice.objects.iterator(): bgneal@1090: poll = poll_dict.get(choice.poll_id) bgneal@1090: if poll: bgneal@1090: poll.total_votes_ += choice.votes bgneal@1090: bgneal@439: return render(request, 'polls/index.html', { bgneal@439: 'current_polls': current_polls, bgneal@439: 'old_polls': old_polls, bgneal@1091: 'V3_DESIGN': True, 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@447: if not poll.is_enabled or poll.start_date > datetime.datetime.now(): bgneal@439: raise Http404 gremmie@1: bgneal@447: total_votes, choices = poll.results() bgneal@447: bgneal@447: return render(request, 'polls/poll_detail.html', { bgneal@439: 'poll': poll, bgneal@447: 'total_votes': total_votes, bgneal@447: 'choices': choices, bgneal@439: 'user_choice': get_user_choice(request.user, poll), bgneal@1091: 'V3_DESIGN': True, 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@447: return HttpResponseRedirect(reverse('polls-detail', bgneal@447: kwargs={'poll_id': poll_id})) gremmie@1: bgneal@439: user_choice = get_user_choice(request.user, poll) gremmie@1: bgneal@439: if request.method == "POST": bgneal@447: vote_form = VoteForm(poll, request.POST, user=request.user, bgneal@447: user_choice=user_choice) bgneal@439: if vote_form.is_valid(): bgneal@439: vote_form.save() bgneal@447: return HttpResponseRedirect(reverse('polls-detail', bgneal@447: kwargs={'poll_id': 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@1091: 'V3_DESIGN': True, bgneal@439: }) gremmie@1: gremmie@1: ####################################################################### gremmie@1: 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@447: return HttpResponseRedirect(reverse('polls-detail', kwargs={'poll_id': poll.id}))