Bläddra i källkod

Started adding a basic question result details page.

Adam Day 2 år sedan
förälder
incheckning
21f5b939ff

+ 3 - 1
vote/models.py

@@ -85,6 +85,8 @@ class Vote(models.Model):
     question = models.ForeignKey(Question, on_delete=models.CASCADE)
     vote = models.BooleanField()
     vote_id = models.CharField(max_length=256, default=random_id)
+    vote_zip = models.CharField(max_length=5)
+    vote_age = models.ForeignKey(AgeRange, on_delete=models.CASCADE)
 
     def __str__(self):
         return "%s (%s)" % (self.question, self.vote)
@@ -101,4 +103,4 @@ class VoterQuestion(models.Model):
         return "%s (%s)" % (self.voter, self.question.id)
 
     class Meta:
-        ordering = ['voter']
+        ordering = ['voter']

+ 1 - 1
vote/templates/vote/dark-layout.html

@@ -9,7 +9,7 @@
     <link href="https://cdn.jsdelivr.net/npm/remixicon@2.5.0/fonts/remixicon.css" rel="stylesheet">
     <link href="{% static '/css/app.css' %}" rel="stylesheet">
   </head>
-  <body class="bg-dark">
+  <body style="background-color:#232628;">
     {% block content %}{% endblock %}
     <script src="https://code.jquery.com/jquery-3.6.4.min.js" integrity="sha256-oP6HI9z1XaZNBrJURtCoUT5SUnxFr8s3BzRl+cbzUq8=" crossorigin="anonymous"></script>
     <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js" integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN" crossorigin="anonymous"></script>

+ 79 - 0
vote/templates/vote/question_results.html

@@ -0,0 +1,79 @@
+{% extends 'vote/dark-layout.html' %}
+
+{% block content %}
+    <div class="container pt-5">
+        <div class="row justify-content-center">
+            <div class="col-sm-12">
+                <div class="text-center mb-5">
+                    <h1 class="text-light">Results</h1>
+                </div>
+                <div class="card p-5">
+                    <div class="card-body">
+                        <p class="lead">{{ question.question_text }}</p>
+                        <div class="row">
+                            <div class="col-4 text-center mb-5">
+                                <div class="card">
+                                    <div class="card-body">
+                                        <p>
+                                            Total
+                                            <h1>{{ question.total_votes }}</h1>
+                                        </p>
+                                    </div>
+                                </div>
+                            </div>
+                            <div class="col-4 text-center mb-5">
+                                <div class="card {% if question.yes_votes > question.no_votes %} text-bg-success {% else %} border-success text-success {% endif %}">
+                                    <div class="card-body">
+                                        <p>
+                                            In Favor
+                                            <h1>{{ question.yes_votes }}</h1>
+                                        </p>
+                                    </div>
+                                </div>
+                            </div>
+                            <div class="col-4 text-center mb-5">
+                                <div class="card {% if question.no_votes > question.yes_votes %} text-bg-danger {% else %} border-danger text-danger {% endif %}">
+                                    <div class="card-body">
+                                        <p>
+                                            Opposed
+                                            <h1>{{ question.no_votes }}</h1>
+                                        </p>
+                                    </div>
+                                </div>
+                            </div>
+                            <table class="table mb-5">
+                                <tr>
+                                    <th>Age Range</th>
+                                    <th>In Favor</th>
+                                    <th>Opposed</th>
+                                </tr>
+                                {% for range in age_ranges %}
+                                <tr>
+                                    <td>{{ range.age_range }}</td>
+                                    <td>{{ range.yes_votes }}</td>
+                                    <td>{{ range.no_votes }}</td>
+                                </tr>
+                                {% endfor %}
+                            </table>
+
+                            <table class="table">
+                                <tr>
+                                    <th>ZIP Code</th>
+                                    <th>In Favor</th>
+                                    <th>Opposed</th>
+                                </tr>
+                                {% for zip in zip_codes %}
+                                <tr>
+                                    <td>{{ zip.vote_zip }}</td>
+                                    <td>{{ zip.yes_votes }}</td>
+                                    <td>{{ zip.no_votes }}</td>
+                                </tr>
+                                {% endfor %}
+                            </table>
+                        </div>
+                    </div>
+                </div>
+            </div>
+        </div>
+    </div>
+{% endblock %}

+ 4 - 1
vote/templates/vote/results.html

@@ -4,7 +4,7 @@
     <div class="container p-5">
         <div class="row justify-content-center">
             <div class="col-sm-12 col-md-6">
-                <div class="text-center">
+                <div class="text-center mb-5">
                     <h1 class="text-light">Results</h1>
                 </div>
                 <div class="card">
@@ -43,6 +43,9 @@
                                         </div>
                                     </div>
                                 </div>
+                                <div class="col-12 text-end">
+                                    <a href="{% url 'question_results' question_id=question.question_id %}">Details</a>
+                                </div>
                             </div>
                         {% endfor %}
                     </div>

+ 1 - 0
vote/urls.py

@@ -4,4 +4,5 @@ from . import views
 urlpatterns = [
     path('', views.index, name='index'),
     path('results/', views.results, name='results'),
+    path('results/question/<slug:question_id>/', views.question_results, name='question_results'),
 ]

+ 52 - 2
vote/views.py

@@ -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)