|
@@ -48,7 +48,7 @@ def index(request):
|
|
|
voter_question.save()
|
|
|
|
|
|
# Add the vote to the Vote table
|
|
|
- vote = Vote(question=question, vote=questions[question])
|
|
|
+ vote = Vote(question=question, vote=questions[question], vote_zip=voter.zip_code.zip, vote_age=voter.age_range)
|
|
|
vote.save()
|
|
|
|
|
|
# Redirect to the results page
|
|
@@ -79,4 +79,54 @@ def results(request):
|
|
|
'questions': questions,
|
|
|
}
|
|
|
|
|
|
- return render(request, 'vote/results.html', context=context)
|
|
|
+ return render(request, 'vote/results.html', context=context)
|
|
|
+
|
|
|
+
|
|
|
+def question_results(request, question_id):
|
|
|
+ question = Question.objects.filter(question_id=question_id).first()
|
|
|
+ yes_votes = Vote.objects.filter(question=question, vote=True).count()
|
|
|
+ no_votes = Vote.objects.filter(question=question, vote=False).count()
|
|
|
+ question.total_votes = yes_votes + no_votes
|
|
|
+ question.yes_votes = yes_votes
|
|
|
+ question.no_votes = no_votes
|
|
|
+
|
|
|
+ # Group votes by age range
|
|
|
+ age_ranges = AgeRange.objects.all()
|
|
|
+ for age_range in age_ranges:
|
|
|
+ yes_votes = Vote.objects.filter(question=question, vote=True, vote_age=age_range).count()
|
|
|
+ no_votes = Vote.objects.filter(question=question, vote=False, vote_age=age_range).count()
|
|
|
+ age_range.total_votes = yes_votes + no_votes
|
|
|
+ age_range.yes_votes = yes_votes
|
|
|
+ age_range.no_votes = no_votes
|
|
|
+
|
|
|
+ # Get all zip codes from votes
|
|
|
+ zip_codes = Vote.objects.filter(question=question).values('vote_zip').distinct()
|
|
|
+
|
|
|
+ for zip_code in zip_codes:
|
|
|
+
|
|
|
+ zc = ZipCode.objects.filter(zip=zip_code['vote_zip']).first()
|
|
|
+
|
|
|
+ yes_votes = Vote.objects.filter(question=question, vote=True, vote_zip=zc).count()
|
|
|
+ no_votes = Vote.objects.filter(question=question, vote=False, vote_zip=zc).count()
|
|
|
+
|
|
|
+ zip_code['yes_votes'] = yes_votes
|
|
|
+ zip_code['no_votes'] = no_votes
|
|
|
+
|
|
|
+ '''
|
|
|
+ # Group votes by zip code
|
|
|
+ zip_codes = ZipCode.objects.all()
|
|
|
+ for zip_code in zip_codes:
|
|
|
+ yes_votes = Vote.objects.filter(question=question, vote=True, vote_zip=zip_code).count()
|
|
|
+ no_votes = Vote.objects.filter(question=question, vote=False, vote_zip=zip_code).count()
|
|
|
+ zip_code.total_votes = yes_votes + no_votes
|
|
|
+ zip_code.yes_votes = yes_votes
|
|
|
+ zip_code.no_votes = no_votes
|
|
|
+ '''
|
|
|
+
|
|
|
+ context = {
|
|
|
+ 'question': question,
|
|
|
+ 'age_ranges': age_ranges,
|
|
|
+ 'zip_codes': zip_codes,
|
|
|
+ }
|
|
|
+
|
|
|
+ return render(request, 'vote/question_results.html', context=context)
|