annotate gpp/polls/views.py @ 197:2baadae33f2e

Got autocomplete working for the member search. Updated django and ran into a bug where url tags with comma separated kwargs starting consuming tons of CPU throughput. The work-around is to cut over to using spaces between arguments. This is now allowed to be consistent with other tags. Did some query optimization for the news app.
author Brian Neal <bgneal@gmail.com>
date Sat, 10 Apr 2010 04:32:24 +0000
parents dbd703f7d63a
children 1f139de929c4
rev   line source
gremmie@1 1 """Views for the polls application"""
gremmie@1 2
gremmie@1 3 from django.shortcuts import render_to_response
gremmie@1 4 from django.template import RequestContext
gremmie@1 5 from django.contrib.auth.decorators import login_required
gremmie@1 6 from django.shortcuts import get_object_or_404
gremmie@1 7 from django.http import Http404
gremmie@1 8 from django.http import HttpResponseRedirect
gremmie@1 9 from django.core.urlresolvers import reverse
gremmie@1 10
gremmie@1 11 from polls.models import Poll
gremmie@1 12 from polls.models import Choice
gremmie@1 13 from polls.forms import VoteForm
gremmie@1 14
gremmie@1 15 #######################################################################
gremmie@1 16
gremmie@1 17 def poll_index(request):
gremmie@1 18 current_polls = Poll.objects.get_current_polls()
gremmie@1 19 old_polls = Poll.objects.get_old_polls()
gremmie@1 20 return render_to_response('polls/index.html', {
gremmie@1 21 'current_polls': current_polls,
gremmie@1 22 'old_polls': old_polls,
gremmie@1 23 },
gremmie@1 24 context_instance = RequestContext(request))
gremmie@1 25
gremmie@1 26 #######################################################################
gremmie@1 27
gremmie@1 28 def poll_detail(request, poll_id):
gremmie@1 29 poll = get_object_or_404(Poll, pk = poll_id)
gremmie@1 30 if not poll.is_enabled:
gremmie@1 31 raise Http404
gremmie@1 32
gremmie@1 33 return render_to_response('polls/poll.html', {
gremmie@1 34 'poll': poll,
gremmie@1 35 },
gremmie@1 36 context_instance = RequestContext(request))
gremmie@1 37
gremmie@1 38 #######################################################################
gremmie@1 39
gremmie@1 40 @login_required
gremmie@1 41 def poll_vote(request, poll_id):
gremmie@1 42 poll = get_object_or_404(Poll, pk = poll_id)
gremmie@1 43 if not poll.is_enabled:
gremmie@1 44 raise Http404
gremmie@1 45 if not poll.is_open():
gremmie@1 46 return HttpResponseRedirect(reverse('polls.views.poll_results', args=[poll_id]))
gremmie@1 47
gremmie@1 48 if request.method == "POST":
gremmie@1 49 vote_form = VoteForm(poll, request.POST)
gremmie@1 50 if vote_form.is_valid():
gremmie@1 51 choice_id = request.POST.get('choices', None)
gremmie@1 52 choice = get_object_or_404(Choice, pk = choice_id)
gremmie@1 53 choice.votes += 1
gremmie@1 54 choice.save()
gremmie@1 55 return HttpResponseRedirect(reverse('polls.views.poll_results', args=[poll_id]))
gremmie@1 56
gremmie@1 57 vote_form = VoteForm(poll)
gremmie@1 58
gremmie@1 59 return render_to_response('polls/poll_vote.html', {
gremmie@1 60 'poll': poll,
gremmie@1 61 'vote_form': vote_form,
gremmie@1 62 },
gremmie@1 63 context_instance = RequestContext(request))
gremmie@1 64
gremmie@1 65 #######################################################################
gremmie@1 66
gremmie@1 67 def poll_results(request, poll_id):
gremmie@1 68 poll = get_object_or_404(Poll, pk = poll_id)
gremmie@1 69 total_votes, choices = poll.results()
gremmie@1 70 return render_to_response('polls/poll_results.html', {
gremmie@1 71 'poll': poll,
gremmie@1 72 'total_votes': total_votes,
gremmie@1 73 'choices': choices,
gremmie@1 74 },
gremmie@1 75 context_instance = RequestContext(request))
gremmie@1 76
gremmie@1 77 #######################################################################