From ab23a954555e642b4cf4b0d435311c20a6bd324c Mon Sep 17 00:00:00 2001 From: Felix Sargent Date: Tue, 23 Jul 2024 17:48:57 -0700 Subject: [PATCH] Improve formatting to be consistent with bootstrap 5 (#212) --- approval_polls/staticfiles/scss/custom.scss | 4 +- approval_polls/templates/base.html | 125 ++++++++------- approval_polls/templates/detail.html | 6 +- approval_polls/templates/index.html | 80 ++++++---- approval_polls/templates/my_info.html | 159 +++++++++++--------- approval_polls/templates/my_polls.html | 102 +++++++------ approval_polls/templates/results.html | 95 ++++++------ approval_polls/views.py | 35 +++-- 8 files changed, 326 insertions(+), 280 deletions(-) diff --git a/approval_polls/staticfiles/scss/custom.scss b/approval_polls/staticfiles/scss/custom.scss index 0edfbac..c94a3f9 100644 --- a/approval_polls/staticfiles/scss/custom.scss +++ b/approval_polls/staticfiles/scss/custom.scss @@ -3,8 +3,8 @@ $primary: #000000; $secondary: #ffffff; $success: #000000; $info: #ffffff; -$warning: #000000; -$danger: #000000; +$warning: #aa4a44; +$danger: #aa4a44; $light: #ffffff; $dark: #000000; diff --git a/approval_polls/templates/base.html b/approval_polls/templates/base.html index c31a9a5..af209f9 100644 --- a/approval_polls/templates/base.html +++ b/approval_polls/templates/base.html @@ -52,84 +52,81 @@ {% load tz %} {% get_current_timezone as TIME_ZONE %} - -
-
- {% block content %} - {% endblock content %} -
-
-
-

- This poll uses approval voting, instead of the more common plurality system. -
+ + +

+ {% block content %} + {% endblock content %} +
+
+ + + + + {% block extra_js %}{% endblock %} diff --git a/approval_polls/templates/detail.html b/approval_polls/templates/detail.html index ef98e84..0e81962 100644 --- a/approval_polls/templates/detail.html +++ b/approval_polls/templates/detail.html @@ -27,8 +27,8 @@

{{ poll.question }}

id='choice{{ forloop.counter }}' {% if checked_choices %} {% if choice in checked_choices %}checked{% endif %} {% endif %}> - {{ choice.choice_text }} - + {{ choice.choice_text }} +
{% endfor %} {% if poll.show_write_in and not poll.is_closed %} @@ -39,7 +39,7 @@

{{ poll.question }}

title='Add Option' data-toggle='tooltip' data-placement='bottom'> - + Add Option

{% endif %} diff --git a/approval_polls/templates/index.html b/approval_polls/templates/index.html index 3c60509..6e32812 100644 --- a/approval_polls/templates/index.html +++ b/approval_polls/templates/index.html @@ -1,36 +1,60 @@ {% extends 'base.html' %} {% block content %} - {% if latest_poll_list %} -
-
-

Latest Public Polls

- {% for poll in latest_poll_list %} -
- {{ poll.question }} -

Published on {{ poll.pub_date|date:"N j, Y, P e" }} by {% firstof poll.user.username poll.user.first_name %}

-
- {% endfor %} +
+ {% if latest_poll_list %} +

Latest Public Polls

+
+
+ {% for poll in latest_poll_list %} +
+
+

+ {{ poll.question }} +

+

+ Published on {{ poll.pub_date|date:"N j, Y, P e" }} by {% firstof poll.user.username poll.user.first_name %} +

+
+
+ {% endfor %} +
-
-
-
-
+
-
-
- {% else %} -
-
-

No polls are available.

-
-
- {% endif %} +
  • + Page {{ latest_poll_list.number }} of {{ latest_poll_list.paginator.num_pages }} +
  • + {% if latest_poll_list.has_next %} +
  • + + Next + +
  • + {% endif %} + + + {% else %} + + {% endif %} +
    {% endblock content %} {% block footer %} - Create a Poll +
    +
    + +
    +
    {% endblock %} diff --git a/approval_polls/templates/my_info.html b/approval_polls/templates/my_info.html index cfa328d..275c0c0 100644 --- a/approval_polls/templates/my_info.html +++ b/approval_polls/templates/my_info.html @@ -1,85 +1,98 @@ {% extends 'base.html' %} -{% block head %} - {% load static %} +{% load static %} +{% block extra_css %}{% endblock %} +{% block extra_js %} {% endblock %} {% block content %} -
    -
    -

    My User Profile

    -
    -
    -
    -
    -
    - {% comment %}

    First Name: {{ current_user.first_name }}

    -

    Last Name: {{ current_user.last_name }}

    {% endcomment %} -

    Email: {{ current_user.email }}

    -

    Member since: {{ current_user.date_joined|date:"F j, Y e" }}

    -

    Last Login: {{ current_user.last_login|date:"F j, Y e" }}

    -

    - Polls I created: - {{ current_user.poll_set.all.count }} -

    -

    - Change Username -

    -

    - Change Password -

    -

    - Manage Subscriptions -

    +
    +

    My User Profile

    +
    +
    +

    Personal Information

    +
    +
    First Name:
    +
    + {{ current_user.first_name }} +
    +
    Last Name:
    +
    + {{ current_user.last_name }} +
    +
    Username:
    +
    + {{ current_user.username }} +
    +
    Email:
    +
    + {{ current_user.email }} +
    +
    Member since:
    +
    + {{ current_user.date_joined|date:"F j, Y" }} +
    +
    Last Login:
    +
    + {{ current_user.last_login|date:"F j, Y" }} +
    +
    Polls created:
    +
    + {{ current_user.poll_set.all.count }} +
    +
    +
    + Change Password + {% comment %} + Change Username + Manage Subscriptions + {% endcomment %} +
    -
    -
    -
    -

    My Polls

    -
    -
    - {% if latest_poll_list %} -
    -
    - {% for poll in latest_poll_list %} -
    -
    +

    My Polls

    + {% if latest_poll_list %} + {% for poll in latest_poll_list %} +
    +
    +

    {{ poll.question }} +

    +

    Published on {{ poll.pub_date|date:"N j, Y, P e" }}

    +
    + Edit +
    -
    - remove - {% comment %} {%with suspend_text=poll.is_suspended|get_suspend_text%} {% endcomment %} - {% comment %} {% endcomment %} - {% comment %} {{ suspend_text }} {% endcomment %} - {% comment %} {%endwith%} {% endcomment %} - edit -
    -
    - -
    -

    Published on {{ poll.pub_date|date:"N j, Y, P e" }}

    - {% endfor %} -
    -
    -
    -
    -
    +
    + {% endfor %} +
    -
    -
    - {% else %} -
    -
    -

    No polls are available.

    -
    -
    - {% endif %} +
  • + Page {{ latest_poll_list.number }} of {{ latest_poll_list.paginator.num_pages }} +
  • + {% if latest_poll_list.has_next %} +
  • + + Next + +
  • + {% endif %} + + + {% else %} + + {% endif %} + +
    {% endblock content %} diff --git a/approval_polls/templates/my_polls.html b/approval_polls/templates/my_polls.html index 97b4806..4acf887 100644 --- a/approval_polls/templates/my_polls.html +++ b/approval_polls/templates/my_polls.html @@ -1,57 +1,65 @@ {% extends 'base.html' %} +{% load static %} {% load filters %} -{% block head %} - {% load static %} +{% block extra_css %}{% endblock %} +{% block extra_js %} {% endblock %} {% block content %} -
    -
    -

    My Polls

    -
    -
    - {% if latest_poll_list %} -
    -
    - {% for poll in latest_poll_list %} - -
    - remove - {% comment %} {% with suspend_text=poll.is_suspended|get_suspend_text %} - {{ suspend_text }} - {% endwith %} {% endcomment %} - edit -
    -
    - +
    +

    My Polls

    + {% if latest_poll_list %} +
    +
    + {% for poll in latest_poll_list %} +
    +
    +
    + {{ poll.question }} +
    +

    Published on {{ poll.pub_date|date:"N j, Y, P e" }}

    +
    + Edit + {% comment %} + {% with suspend_text=poll.is_suspended|get_suspend_text %} + + {% endwith %} + {% endcomment %} + +
    +
    -

    Published on {{ poll.pub_date|date:"N j, Y, P e" }}

    -
    - {% endfor %} + {% endfor %} +
    -
    -
    -
    -
    +
    -
    -
    - {% else %} -
    -
    -

    No polls are available.

    -
    -
    - {% endif %} +
  • + Page {{ latest_poll_list.number }} of {{ latest_poll_list.paginator.num_pages }} +
  • + {% if latest_poll_list.has_next %} +
  • + + Next + +
  • + {% endif %} + + + {% else %} + + {% endif %} + +
    {% endblock content %} diff --git a/approval_polls/templates/results.html b/approval_polls/templates/results.html index 7c68f90..f142cc9 100644 --- a/approval_polls/templates/results.html +++ b/approval_polls/templates/results.html @@ -1,65 +1,58 @@ {% extends 'base.html' %} {% load filters %} {% block content %} -
    -
    -

    {{ poll.question }}

    +
    +

    {{ poll.question }}

    +
    +

    + {{ poll.total_ballots }} ballot{{ poll.total_ballots|pluralize }} + {% if poll.is_closed and poll.total_votes == 0 %}No votes in this poll{% endif %} +

    -
    -
    -
    -

    - - {{ poll.total_ballots }} - ballot{{ poll.total_ballots|pluralize }} - {% if poll.is_closed and poll.total_votes == 0 %}

    No votes in this poll

    {% endif %} -
    -

    -
    -
    -
    -
    - {% for choice in poll.choice_set.all|sort_by_votes %} -

    - {{ choice.choice_text }} - {% if poll.is_closed and choice in leading_choices %} - - Winner - +
    + {% for choice in choices %} +
    +

    + {{ choice.choice_text }} + {% if choice in leading_choices %} + + {% if poll.is_closed %} + Winner + {% else %} + Leading + {% endif %} + - - {% endif %} - {{ choice.votes }} vote{{ choice.votes|pluralize }} ({{ choice.percentage|to_percent_str }}) -

    -
    - {% if poll.show_lead_color %} + {% endif %} +

    +

    + {{ choice.vote_count }} vote{{ choice.vote_count|pluralize }} + ({{ choice.percentage|to_percent_str }}) {% if choice in leading_choices %} -

    - {% else %} -
    + + + {% endif %} - {% else %} -
    +
    +
    - {% endif %} + style="width: {% widthratio choice.vote_count poll.total_votes 100 %}%" + aria-valuenow="{% widthratio choice.vote_count poll.total_votes 100 %}" + aria-valuemin="0" + aria-valuemax="100"> + {% widthratio choice.vote_count poll.total_votes 100 %}% +
    +
    {% endfor %}
    -
    -
    -
    -
    - {% if 'invitation' in request.META.HTTP_REFERER %} - Back to poll - {% else %} - Back to poll - {% endif %} -
    +
    + {% if 'invitation' in request.META.HTTP_REFERER %} + Back to poll + {% else %} + Back to poll + {% endif %}
    {% endblock %} diff --git a/approval_polls/views.py b/approval_polls/views.py index 9416612..41cd761 100644 --- a/approval_polls/views.py +++ b/approval_polls/views.py @@ -8,6 +8,7 @@ from django.contrib.auth.models import User from django.core.exceptions import PermissionDenied from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator +from django.db.models import Count from django.http import Http404, HttpResponseRedirect from django.shortcuts import get_object_or_404, render from django.urls import reverse @@ -273,20 +274,30 @@ def get_queryset(self): return Poll.objects.filter(pub_date__lte=timezone.now()) def get_context_data(self, **kwargs): - context = super(ResultsView, self).get_context_data(**kwargs) + context = super().get_context_data(**kwargs) poll = self.object - choices = {} - for choice in poll.choice_set.all(): - choices[choice] = choice.votes() - max_votes = max(choices.values()) - if max_votes == 0: - leading_choices = [] - else: - leading_choices = [k for k, v in list(choices.items()) if v == max_votes] - choices = list(poll.choice_set.all()) - choices.sort(key=lambda choice: choice.votes(), reverse=True) - context["leading_choices"] = leading_choices + # Annotate choices with vote count and order by votes + choices = poll.choice_set.annotate(vote_count=Count("vote")).order_by( + "-vote_count" + ) + + # Calculate max votes + max_votes = choices.first().vote_count if choices.exists() else 0 + + # Determine leading choices + leading_choices = [ + choice for choice in choices if choice.vote_count == max_votes + ] + + context.update( + { + "choices": choices, + "leading_choices": leading_choices, + "max_votes": max_votes, + } + ) + return context