gremmie@1: """Views for the polls application""" gremmie@1: gremmie@1: from django.shortcuts import render_to_response gremmie@1: from django.template import RequestContext 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 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: gremmie@1: def poll_index(request): gremmie@1: current_polls = Poll.objects.get_current_polls() gremmie@1: old_polls = Poll.objects.get_old_polls() gremmie@1: return render_to_response('polls/index.html', { gremmie@1: 'current_polls': current_polls, gremmie@1: 'old_polls': old_polls, gremmie@1: }, gremmie@1: context_instance = RequestContext(request)) gremmie@1: gremmie@1: ####################################################################### gremmie@1: gremmie@1: def poll_detail(request, poll_id): gremmie@1: poll = get_object_or_404(Poll, pk = poll_id) gremmie@1: if not poll.is_enabled: gremmie@1: raise Http404 gremmie@1: gremmie@1: return render_to_response('polls/poll.html', { gremmie@1: 'poll': poll, gremmie@1: }, gremmie@1: context_instance = RequestContext(request)) gremmie@1: gremmie@1: ####################################################################### gremmie@1: gremmie@1: @login_required gremmie@1: def poll_vote(request, poll_id): gremmie@1: poll = get_object_or_404(Poll, pk = poll_id) gremmie@1: if not poll.is_enabled: gremmie@1: raise Http404 gremmie@1: if not poll.is_open(): gremmie@1: return HttpResponseRedirect(reverse('polls.views.poll_results', args=[poll_id])) gremmie@1: gremmie@1: if request.method == "POST": gremmie@1: vote_form = VoteForm(poll, request.POST) gremmie@1: if vote_form.is_valid(): gremmie@1: choice_id = request.POST.get('choices', None) gremmie@1: choice = get_object_or_404(Choice, pk = choice_id) gremmie@1: choice.votes += 1 gremmie@1: choice.save() gremmie@1: return HttpResponseRedirect(reverse('polls.views.poll_results', args=[poll_id])) gremmie@1: gremmie@1: vote_form = VoteForm(poll) gremmie@1: gremmie@1: return render_to_response('polls/poll_vote.html', { gremmie@1: 'poll': poll, gremmie@1: 'vote_form': vote_form, gremmie@1: }, gremmie@1: context_instance = RequestContext(request)) gremmie@1: gremmie@1: ####################################################################### gremmie@1: gremmie@1: def poll_results(request, poll_id): gremmie@1: poll = get_object_or_404(Poll, pk = poll_id) gremmie@1: total_votes, choices = poll.results() gremmie@1: return render_to_response('polls/poll_results.html', { gremmie@1: 'poll': poll, gremmie@1: 'total_votes': total_votes, gremmie@1: 'choices': choices, gremmie@1: }, gremmie@1: context_instance = RequestContext(request)) gremmie@1: gremmie@1: #######################################################################