Guest User

view.py

a guest
Feb 2nd, 2012
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. # views.py
  2.  
  3.  
  4. from django.shortcuts import get_object_or_404, render_to_response
  5. from django.http import HttpResponseRedirect, HttpResponse
  6. from django.core.urlresolvers import reverse
  7. from django.template import RequestContext
  8. from polls.models import Choice, Poll
  9.  
  10. from django.contrib.auth import authenticate, login
  11. from django.contrib.auth import logout
  12. from polls.models import Voter
  13.  
  14.  
  15. def login_view(request,poll_id=0):
  16. if (request.POST):
  17. username = request.POST['username']
  18. password = request.POST['password']
  19. user = authenticate(username=username, password=password)
  20. if user is not None:
  21. if user.is_active:
  22. login(request, user)
  23. return HttpResponseRedirect('welcome')
  24. else:
  25. return render_to_response('polls/login.html', {'error_message': "invalid user"})
  26. else:
  27. return render_to_response('polls/login.html', {'error_message': "invalid userID and password combination"},context_instance=RequestContext(request))
  28. else:
  29. return render_to_response('polls/login.html', {'error_message': "Please enter login credentials"},context_instance=RequestContext(request))
  30.  
  31.  
  32.  
  33. def logout_view(request,poll_id=0):
  34. logout(request)
  35. return render_to_response('/polls/login.html', {'error_message': "successfully logged out"},context_instance=RequestContext(request))
  36.  
  37.  
  38.  
  39. def index(request):
  40. latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5]
  41. return render_to_response('polls/index.html', {'latest_poll_list': latest_poll_list})
  42.  
  43. def detail(request, poll_id):
  44. p = get_object_or_404(Poll, pk=poll_id)
  45. voters=[u.user.id for u in Voter.objects.filter(poll__id=poll_id)]
  46.  
  47. if request.user.id in voters:
  48. # return HttpResponseRedirect(reverse('polls.views.results', kwargs={'message':'You have already voted','poll_id':p.id}))
  49. return render_to_response('polls/results.html',{'poll':p,'error_message':"You have already voted"})
  50.  
  51. else:
  52. return render_to_response('polls/detail.html', {'poll': p},context_instance=RequestContext(request))
  53.  
  54. def results(request, poll_id, message=' '):
  55. p = get_object_or_404(Poll, pk=poll_id)
  56. return render_to_response('polls/results.html', {'poll': p,'error_message':message})
  57.  
  58. def vote(request, poll_id):
  59. poll = get_object_or_404(Poll, pk=poll_id)
  60.  
  61. if not request.user.is_authenticated():
  62. return render_to_response('polls/detail.html',RequestContext(request,{'object': poll,'error_message': "Anonymous visitors can not vote.", }))
  63.  
  64. voters=[u.user.id for u in Voter.objects.filter(poll__id=poll_id)]
  65. if request.user.id in voters:
  66. return render_to_response('polls/results.html',RequestContext(request,{'object': poll,'error_message': "You have already voted.", }))
  67.  
  68. try:
  69. selected_choice = poll.choice_set.get(pk=request.POST['choice'])
  70. except (KeyError, Choice.DoesNotExist):
  71. # Redisplay the poll voting form.
  72. return render_to_response('polls/detail.html', {
  73. 'poll': poll,
  74. 'error_message': "You didn't select a choice.",
  75. }, context_instance=RequestContext(request))
  76. else:
  77. selected_choice.votes += 1
  78. #request.session['has_voted_poll']=voted_polls+[poll_id]
  79. selected_choice.save()
  80. # Always return an HttpResponseRedirect after successfully dealing
  81. # with POST data. This prevents data from being posted twice if a
  82. # user hits the Back button.
  83. # We save the state that a given user has voted in a given poll
  84. v = Voter(user=request.user,poll=poll)
  85. v.save()
  86. return HttpResponseRedirect(reverse('polls.views.results', args=(poll.id,)))
Advertisement
Add Comment
Please, Sign In to add comment