def submit_vote(request, question_id): """ Submits a vote that has been made in the show_question view """ question = get_object_or_404(PollQuestion, poll=user.poll, id=question_id) next_question = question.next() valid_choices = {"yes": 0, "no": 1, "abstention": 2} # Check if the vote key exists and contains a valid value. Otherwise, # redirect to next question if not "vote" in request.POST or not request.POST["vote"] in valid_choices: if next_question == None: return HttpResponseRedirect(reverse("thanks")) return HttpResponseRedirect(reverse("show_question", kwargs={"question_id": next_question.id})) # Check if user already voted on this. If not, create new Vote object try: vote = Vote.objects.get(voting_user=user, poll_question=question) except Vote.DoesNotExist: vote = Vote(voting_user=user, poll_question=question) vote.choice = valid_choices[request.POST["vote"]] vote.save() if next_question == None: return HttpResponseRedirect(reverse("thanks")) return HttpResponseRedirect(reverse("show_question", kwargs={"question_id": next_question.id}))
def add_car_vote(request, car_name): s = Survey.get() v = Vote() v.survey = s v.choice = car_name v.save() cnt = Vote.objects.filter(survey=s, choice=car_name).count() return HttpResponse(str(cnt))
def index(request): s = Survey.get() current_vote = request.GET.get('type') current_car = request.GET.get('car') if current_vote in ('man', 'woman'): if current_vote == 'man': v = Vote(type=Man) else: v = Vote(type=Woman) v.survey = s v.save() elif current_car: if current_car != 'delete': v = Vote() v.survey = s v.choice = current_car v.save() else: vote = Vote.objects.last() vote.delete() votes = Vote.objects.filter(survey=s).order_by('-date') if s == 'tesla': cnt = votes.count() #tallies = Vote.objects.filter(survey=s).values('choice').annotate(Count('choice')).order_by('choice') tallies = {} tallies['TeslaS'] = Vote.objects.filter(survey=s, choice='tesla_s').count() tallies['TeslaX'] = Vote.objects.filter(survey=s, choice='tesla_x').count() tallies['Tesla3'] = Vote.objects.filter(survey=s, choice='tesla_3').count() return render(request, 'vote/cars.html', {'vtot': cnt, 'tallies': tallies, 'survey': s, 'votes': votes[0:5]}) else: mtot = votes.filter(type=Man).count() wtot = votes.filter(type=Woman).count() return render(request, 'vote/index.html', {'vtot': mtot+wtot, 'mtot': mtot, 'wtot': wtot, 'survey': s, 'votes': votes[0:5]})