Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # views.py
- from django.shortcuts import get_object_or_404, render_to_response
- from django.http import HttpResponseRedirect, HttpResponse
- from django.core.urlresolvers import reverse
- from django.template import RequestContext
- from polls.models import Choice, Poll
- from django.contrib.auth import authenticate, login
- from django.contrib.auth import logout
- from polls.models import Voter
- def login_view(request,poll_id=0):
- if (request.POST):
- username = request.POST['username']
- password = request.POST['password']
- user = authenticate(username=username, password=password)
- if user is not None:
- if user.is_active:
- login(request, user)
- return HttpResponseRedirect('welcome')
- else:
- return render_to_response('polls/login.html', {'error_message': "invalid user"})
- else:
- return render_to_response('polls/login.html', {'error_message': "invalid userID and password combination"},context_instance=RequestContext(request))
- else:
- return render_to_response('polls/login.html', {'error_message': "Please enter login credentials"},context_instance=RequestContext(request))
- def logout_view(request,poll_id=0):
- logout(request)
- return render_to_response('/polls/login.html', {'error_message': "successfully logged out"},context_instance=RequestContext(request))
- def index(request):
- latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5]
- return render_to_response('polls/index.html', {'latest_poll_list': latest_poll_list})
- def detail(request, poll_id):
- p = get_object_or_404(Poll, pk=poll_id)
- voters=[u.user.id for u in Voter.objects.filter(poll__id=poll_id)]
- if request.user.id in voters:
- # return HttpResponseRedirect(reverse('polls.views.results', kwargs={'message':'You have already voted','poll_id':p.id}))
- return render_to_response('polls/results.html',{'poll':p,'error_message':"You have already voted"})
- else:
- return render_to_response('polls/detail.html', {'poll': p},context_instance=RequestContext(request))
- def results(request, poll_id, message=' '):
- p = get_object_or_404(Poll, pk=poll_id)
- return render_to_response('polls/results.html', {'poll': p,'error_message':message})
- def vote(request, poll_id):
- poll = get_object_or_404(Poll, pk=poll_id)
- if not request.user.is_authenticated():
- return render_to_response('polls/detail.html',RequestContext(request,{'object': poll,'error_message': "Anonymous visitors can not vote.", }))
- voters=[u.user.id for u in Voter.objects.filter(poll__id=poll_id)]
- if request.user.id in voters:
- return render_to_response('polls/results.html',RequestContext(request,{'object': poll,'error_message': "You have already voted.", }))
- try:
- selected_choice = poll.choice_set.get(pk=request.POST['choice'])
- except (KeyError, Choice.DoesNotExist):
- # Redisplay the poll voting form.
- return render_to_response('polls/detail.html', {
- 'poll': poll,
- 'error_message': "You didn't select a choice.",
- }, context_instance=RequestContext(request))
- else:
- selected_choice.votes += 1
- #request.session['has_voted_poll']=voted_polls+[poll_id]
- selected_choice.save()
- # Always return an HttpResponseRedirect after successfully dealing
- # with POST data. This prevents data from being posted twice if a
- # user hits the Back button.
- # We save the state that a given user has voted in a given poll
- v = Voter(user=request.user,poll=poll)
- v.save()
- return HttpResponseRedirect(reverse('polls.views.results', args=(poll.id,)))
Advertisement
Add Comment
Please, Sign In to add comment