def vote(request, poll_id): p = get_object_or_404(Poll, pk=poll_id) if p.type in [p.SIMPLE, p.MULTI]: try: choice_ids = request.POST.getlist('choice') except (KeyError, Choice.DoesNotExist): # Redisplay the poll voting form. return render(request, 'polls/detail.html', { 'poll': p, 'error_message': "You didn't select a choice.", }) else: try: voter = Voter.objects.get(user=request.user, poll=p) except Voter.DoesNotExist: voter = Voter(poll=p, user=request.user) voter.save() for vote in voter.vote_set.all(): vote.delete() for id in choice_ids: choice = p.choice_set.get(pk=id) vote = Vote(voter=voter) vote.choice = choice vote.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. return HttpResponseRedirect(reverse('polls:results', args=(p.id,))) elif p.type == p.RANKED: try: voter = Voter.objects.get(user=request.user, poll=p) except Voter.DoesNotExist: voter = Voter(poll=p, user=request.user) voter.save() for vote in voter.vote_set.all(): vote.delete() rank = request.POST.get('rank').split(',') for i,e in enumerate(rank): choice = p.choice_set.get(pk=e) vote = Vote(voter=voter) vote.choice = choice vote.ranking = i; vote.save() return HttpResponseRedirect(reverse('polls:results', args=(p.id,))) else: return render(request, 'polls/detail.html', { 'poll': p, 'error_message': "Poll not implemented :(", })
def vote(request, question_id): question = get_object_or_404(Question, pk=question_id) try: selected_choice = question.choice_set.get(pk=request.POST['choice']) except (KeyError, Choice.DoesNotExist): # Redisplay the question voting form. return render(request, 'polls/question_detail.html', { 'question': question, 'error_message': "You didn't select a choice.", }) else: if len(Vote.objects.filter( question_id=selected_choice.question.id, voter_id=request.user.id)) > 0: return render(request, 'polls/question_detail.html', { 'question': question, 'error_message': "Vous avez déjà voté pour cette question", }) user_vote = Vote() user_vote.voter = request.user.profile user_vote.choice = selected_choice user_vote.question = selected_choice.question user_vote.save() selected_choice.votes += 1 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. return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))
def vote(request, poll_id): poll = get_object_or_404(Poll, pk=poll_id) try: selected_choice = poll.choice_set.get(pk=request.POST['choice']) except (KeyError, Choice.DoesNotExist): return render_to_response('polls/slide_poll.html', { 'poll': poll, }, context_instance=RequestContext(request)) else: vote = Vote() vote.choice = selected_choice if user_is_logged_in(request): profile = UserProfile.objects.get(email = request.session['login_email']) vote.user_profile = profile vote.save() return render_to_response('presentations/slide.html', {'slide': poll}, context_instance=RequestContext(request))
def vote(request, pk): """ Record a vote for a poll question. The selected choice is in the POST body. Arguments: pk = the question id (primary key) """ question_id = pk # lookup the question try: question = Question.objects.get(pk=question_id) except Question.DoesNotExist: return Http404(f"Question {question_id} does not exist") if not question.can_vote(): return HttpResponse("Voting not allowed for that question", status=403) # get the user's choice try: choice_id = request.POST['choice'] except KeyError: context = { 'question': question, 'error_message': "You didn't select a valid choice" } return render(request, 'polls/detail.html', context) try: selected_choice = question.choice_set.get(pk=choice_id) except Choice.DoesNotExist: context = { 'question': question, 'error_message': "You didn't select a valid choice" } return render(request, 'polls/detail.html', context) # Does user already have a Vote for this question? vote = get_vote_for_user(request.user, question) if not vote: vote = Vote(user=request.user, choice=selected_choice) else: # change an existing vote vote.choice = selected_choice vote.save() return HttpResponseRedirect(reverse('polls:results', args=(question_id, )))
def vote(request, poll_id): p = get_object_or_404(Poll, pk=poll_id) user = request.user try: selected_choice = p.choice_set.get(pk=request.POST['choice']) except (KeyError, Choice.DoesNotExist): return render(request, 'polls/detail.html', { 'poll': p, 'error_message': "You didn't select a choice" }) else: if not isinstance(user, AnonymousUser) and user.is_active: v = Vote() v.choice = selected_choice v.user = user v.save() return redirect(reverse('polls:results', args=(p.id, ))) else: return render(request, 'polls/detail.html', { 'poll': p, 'error_message': "You are not logged in" })