Browse Source

Have the basics working.

Adam Day 2 years ago
parent
commit
c26792cc39

+ 5 - 48
static/css/app.css

@@ -40,80 +40,37 @@
 }
 
 .button_yes {
-    display: inline-block;
-    background-color: white;
-    color: #4CAF50;
-    border: 4px solid #4CAF50;
     padding:20px;
     font-size: 2em;
     min-width:200px;
-    text-align: center;
-    border-radius:15px;
-    text-decoration: none;
-    margin-left: 50px;
-    margin-right: 50px;
-}
-
-.button_yes_answered {
-    display: inline-block;
-    background-color: #4CAF50;
-    color: white;
-    border: 4px solid #4CAF50;
-    padding:20px;
-    font-size: 2em;
-    min-width:200px;
-    border-radius:15px;
-    text-decoration: none;
     margin-left: 50px;
     margin-right: 50px;
+    border:4px solid;
 }
 
 .button_no {
-    display: inline-block;
-    background-color: white;
-    color: #af4c4c;
-    border: 4px solid #af4c4c;
-    padding:20px;
-    font-size: 2em;
-    min-width:200px;
-    text-align: center;
-    border-radius:15px;
-    text-decoration: none;
-    margin-left: 50px;
-    margin-right: 50px;
-}
-
-.button_no_answered {
-    display: inline-block;
-    background-color: #af4c4c;
-    color: white;
-    border: 4px solid #af4c4c;
     padding:20px;
     font-size: 2em;
     min-width:200px;
-    border-radius:15px;
-    text-decoration: none;
     margin-left: 50px;
     margin-right: 50px;
+    border:4px solid;
 }
 
 .form-block {
     display: block;
 }
 
-form {
-    font-size:1.5em;
-}
-
 input {
-    font-size:1.1em !important;
+    font-size:1.5em !important;
 }
 
 select {
-    font-size:1.1em !important;
+    font-size:1.5em !important;
 }
 
 button {
     margin-top:25px;
+    padding:30px;
     font-size:1.5em !important;
 }

+ 3 - 0
static/js/app.js

@@ -0,0 +1,3 @@
+$(document).ready(function() {
+
+});

+ 1 - 1
vote/forms.py

@@ -6,6 +6,6 @@ from .models import AgeRange
 class VoterRegistrationForm(forms.Form):
     first_name = forms.CharField(max_length=100)
     last_name = forms.CharField(max_length=100)
-    age_range = forms.ModelChoiceField(queryset=AgeRange.objects.all())
+    age_range = forms.ModelChoiceField(queryset=AgeRange.objects.all(), empty_label='Age Range')
     zip_code = forms.CharField(max_length=5)
 

+ 33 - 24
vote/templates/vote/index.html

@@ -2,33 +2,42 @@
 {% load django_bootstrap5 %}
 
 {% block content %}
-<div class="app-container">
-    <div class="shadow-md border-end" id="form-container">
-        <div class="form-block">
-            <div class="text-center mb-5">
-                <h1 class="display-3"><i class="ri-voiceprint-line"></i> AskPatron</h1>
+<form action="" method="post">
+    {% csrf_token %}
+    <div class="app-container">
+        <div class="" id="poll-container">
+            <div class="questions">
+                {% for question in questions %}
+                    <div class="poll-item">
+                        <div class="poll-question">
+                            {{ question }}
+                        </div>
+                        <div class="text-center">
+                            <input type="radio" class="btn-check" value="no" name="{{ question.question_id }}" id="no-{{ forloop.counter0 }}" autocomplete="off">
+                            <label class="btn btn-outline-danger button_no" for="no-{{ forloop.counter0 }}">No</label>
+
+                            <input type="radio" class="btn-check" value="yes" name="{{ question.question_id }}" id="yes-{{ forloop.counter0 }}" autocomplete="off">
+                            <label class="btn btn-outline-success button_yes" for="yes-{{ forloop.counter0 }}">Yes</label>
+                        </div>
+                    </div>
+                {% endfor %}
             </div>
-            <form method="post">
-                {% csrf_token %}
-                {% bootstrap_form form %}
-                {% bootstrap_button button_type="submit" content="Submit" extra_classes="w-100" %}
-            </form>
         </div>
-    </div>
-    <div class="" id="poll-container">
-        <div class="questions">
-            {% for question in questions %}
-                <div class="poll-item">
-                    <div class="poll-question">
-                        {{ question }}
-                    </div>
-                    <div class="text-center">
-                        <a href="" class="button_no">No</a>
-                        <a href="" class="button_yes">Yes</a>
-                    </div>
+
+        <div class="shadow-md border-end" id="form-container">
+            <div class="form-block">
+                <div class="text-center mb-5">
+                    <h1 class="display-3"><i class="ri-voiceprint-line"></i> AskPatron</h1>
                 </div>
-            {% endfor %}
+                <div class="voter_info">
+                    {% bootstrap_field form.first_name show_label=False %}
+                    {% bootstrap_field form.last_name show_label=False %}
+                    {% bootstrap_field form.age_range show_label=False %}
+                    {% bootstrap_field form.zip_code show_label=False %}
+                    {% bootstrap_button button_type="submit" content="Submit" extra_classes="w-100" %}
+                </div>
+            </div>
         </div>
     </div>
-</div>
+</form>
 {% endblock %}

+ 2 - 0
vote/templates/vote/layout.html

@@ -11,6 +11,8 @@
   </head>
   <body>
     {% 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>
+    <script src="{% static '/js/app.js' %}"></script>
   </body>
 </html>

+ 18 - 0
vote/templates/vote/results.html

@@ -0,0 +1,18 @@
+{% extends 'vote/layout.html' %}
+{% block content %}
+    <div class="container">
+        <div class="row">
+            <div class="col text-center mt-5 pt-5">
+                <h1 class="display-1">Thank you for your submission.</h1>
+                <a class="btn btn-lg btn-primary" href="{% url 'index' %}">Start a submission</a>
+            </div>
+        </div>
+    </div>
+
+    <script>
+        // Redirect after 10 seconds
+        setTimeout(function() {
+            window.location.href = "/";
+        }, 10000);
+    </script>
+{% endblock %}

+ 43 - 4
vote/views.py

@@ -10,11 +10,50 @@ def index(request):
 
     if request.method == 'POST':
         voter_registration_form = VoterRegistrationForm(request.POST)
+
         if voter_registration_form.is_valid():
-            print('Valid')
-            print(voter_registration_form.cleaned_data)
-        else:
-            print('Invalid')
+            voter_info = voter_registration_form.cleaned_data
+
+            zip_code = ZipCode.objects.filter(zip=voter_info['zip_code']).first()
+
+            if zip_code:
+                # Check if voter exists and if not add them to the database
+                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()
+                if voter is None:
+                    voter = Voter(first_name=voter_info['first_name'], last_name=voter_info['last_name'], age_range=voter_info['age_range'], zip_code=zip_code)
+                    voter.save()
+
+                # Extract questions from the form post
+                questions = dict()
+                for key in request.POST:
+                    if key != "first_name" and key != "last_name" and key != "age_range" and key != "zip_code" and key != "csrfmiddlewaretoken":
+                        # Check if each question_id is valid
+                        question = Question.objects.filter(question_id=key).first()
+                        value = request.POST[key]
+                        if question:
+                            if value == "yes":
+                                value = True
+
+                            if value == "no":
+                                value = False
+
+                            questions[question] = value
+
+                # Check if voter has already voted on this question
+                for question in questions:
+                    print(question)
+                    voter_question = VoterQuestion.objects.filter(voter=voter, question=question).first()
+                    if voter_question is None:
+                        # Add the voter to the VoterQuestion table
+                        voter_question = VoterQuestion(voter=voter, question=question)
+                        voter_question.save()
+
+                        # Add the vote to the Vote table
+                        vote = Vote(question=question, vote=questions[question])
+                        vote.save()
+
+                # Redirect to the results page
+                return render(request, 'vote/results.html')
 
     questions = Question.objects.all()