views.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. from django.shortcuts import render
  2. from . forms import VoterRegistrationForm
  3. from . models import *
  4. # Create your views here.
  5. def index(request):
  6. voter_registration_form = VoterRegistrationForm()
  7. if request.method == 'POST':
  8. voter_registration_form = VoterRegistrationForm(request.POST)
  9. if voter_registration_form.is_valid():
  10. voter_info = voter_registration_form.cleaned_data
  11. zip_code = ZipCode.objects.filter(zip=voter_info['zip_code']).first()
  12. if zip_code:
  13. # Check if voter exists and if not add them to the database
  14. voter = Voter.objects.filter(first_name=voter_info['first_name'], last_name=voter_info['last_name'], age_range=voter_info['age_range'], zip_code=zip_code).first()
  15. if voter is None:
  16. voter = Voter(first_name=voter_info['first_name'], last_name=voter_info['last_name'], age_range=voter_info['age_range'], zip_code=zip_code)
  17. voter.save()
  18. # Extract questions from the form post
  19. questions = dict()
  20. for key in request.POST:
  21. if key != "first_name" and key != "last_name" and key != "age_range" and key != "zip_code" and key != "csrfmiddlewaretoken":
  22. # Check if each question_id is valid
  23. question = Question.objects.filter(question_id=key).first()
  24. value = request.POST[key]
  25. if question:
  26. if value == "yes":
  27. value = True
  28. if value == "no":
  29. value = False
  30. questions[question] = value
  31. # Check if voter has already voted on this question
  32. for question in questions:
  33. voter_question = VoterQuestion.objects.filter(voter=voter, question=question).first()
  34. if voter_question is None:
  35. # Add the voter to the VoterQuestion table
  36. voter_question = VoterQuestion(voter=voter, question=question)
  37. voter_question.save()
  38. # Add the vote to the Vote table
  39. vote = Vote(question=question, vote=questions[question], vote_zip=voter.zip_code.zip, vote_age=voter.age_range)
  40. vote.save()
  41. # Redirect to the results page
  42. return render(request, 'vote/thankyou.html')
  43. questions = Question.objects.all()
  44. context = {
  45. 'form': voter_registration_form,
  46. 'questions': questions,
  47. }
  48. return render(request, 'vote/index.html', context=context)
  49. def results(request):
  50. # Tally the votes for each question
  51. questions = Question.objects.all()
  52. for question in questions:
  53. yes_votes = Vote.objects.filter(question=question, vote=True).count()
  54. no_votes = Vote.objects.filter(question=question, vote=False).count()
  55. question.total_votes = yes_votes + no_votes
  56. question.yes_votes = yes_votes
  57. question.no_votes = no_votes
  58. context = {
  59. 'questions': questions,
  60. }
  61. return render(request, 'vote/results.html', context=context)
  62. def question_results(request, question_id):
  63. question = Question.objects.filter(question_id=question_id).first()
  64. yes_votes = Vote.objects.filter(question=question, vote=True).count()
  65. no_votes = Vote.objects.filter(question=question, vote=False).count()
  66. question.total_votes = yes_votes + no_votes
  67. question.yes_votes = yes_votes
  68. question.no_votes = no_votes
  69. # Group votes by age range
  70. age_ranges = AgeRange.objects.all()
  71. for age_range in age_ranges:
  72. yes_votes = Vote.objects.filter(question=question, vote=True, vote_age=age_range).count()
  73. no_votes = Vote.objects.filter(question=question, vote=False, vote_age=age_range).count()
  74. age_range.total_votes = yes_votes + no_votes
  75. age_range.yes_votes = yes_votes
  76. age_range.no_votes = no_votes
  77. # Get all zip codes from votes
  78. zip_codes = Vote.objects.filter(question=question).values('vote_zip').distinct()
  79. for zip_code in zip_codes:
  80. zc = ZipCode.objects.filter(zip=zip_code['vote_zip']).first()
  81. yes_votes = Vote.objects.filter(question=question, vote=True, vote_zip=zc).count()
  82. no_votes = Vote.objects.filter(question=question, vote=False, vote_zip=zc).count()
  83. zip_code['yes_votes'] = yes_votes
  84. zip_code['no_votes'] = no_votes
  85. '''
  86. # Group votes by zip code
  87. zip_codes = ZipCode.objects.all()
  88. for zip_code in zip_codes:
  89. yes_votes = Vote.objects.filter(question=question, vote=True, vote_zip=zip_code).count()
  90. no_votes = Vote.objects.filter(question=question, vote=False, vote_zip=zip_code).count()
  91. zip_code.total_votes = yes_votes + no_votes
  92. zip_code.yes_votes = yes_votes
  93. zip_code.no_votes = no_votes
  94. '''
  95. context = {
  96. 'question': question,
  97. 'age_ranges': age_ranges,
  98. 'zip_codes': zip_codes,
  99. }
  100. return render(request, 'vote/question_results.html', context=context)