diff --git a/election/forms.py b/election/forms.py index 0e901c4c..8a4f7601 100644 --- a/election/forms.py +++ b/election/forms.py @@ -1,6 +1,7 @@ from django import forms from django.utils.translation import gettext_lazy as _ +from core.models import User from core.views.forms import SelectDateTime from core.views.widgets.ajax_select import ( AutoCompleteSelect, @@ -59,22 +60,23 @@ class CandidateForm(forms.ModelForm): class VoteForm(forms.Form): - def __init__(self, election, user, *args, **kwargs): + def __init__(self, election: Election, user: User, *args, **kwargs): super().__init__(*args, **kwargs) - if not election.has_voted(user): - for role in election.roles.all(): - cand = role.candidatures - if role.max_choice > 1: - self.fields[role.title] = LimitedCheckboxField( - cand, role.max_choice, required=False - ) - else: - self.fields[role.title] = forms.ModelChoiceField( - cand, - required=False, - widget=forms.RadioSelect(), - empty_label=_("Blank vote"), - ) + if election.can_vote(user): + return + for role in election.roles.all(): + cand = role.candidatures + if role.max_choice > 1: + self.fields[role.title] = LimitedCheckboxField( + cand, role.max_choice, required=False + ) + else: + self.fields[role.title] = forms.ModelChoiceField( + cand, + required=False, + widget=forms.RadioSelect(), + empty_label=_("Blank vote"), + ) class RoleForm(forms.ModelForm): diff --git a/election/models.py b/election/models.py index a3abd019..fc3a90dc 100644 --- a/election/models.py +++ b/election/models.py @@ -1,5 +1,6 @@ from django.db import models from django.utils import timezone +from django.utils.functional import cached_property from django.utils.translation import gettext_lazy as _ from ordered_model.models import OrderedModel @@ -84,7 +85,7 @@ class Election(models.Model): def has_voted(self, user): return self.voters.filter(id=user.id).exists() - @property + @cached_property def results(self): results = {} total_vote = self.voters.count() diff --git a/election/templates/election/election_detail.jinja b/election/templates/election/election_detail.jinja index 2c628211..b93ab9b7 100644 --- a/election/templates/election/election_detail.jinja +++ b/election/templates/election/election_detail.jinja @@ -31,7 +31,7 @@ {% trans %} at {% endtrans %}

- {%- if election.has_voted(user) %} + {%- if user_has_voted %}

{%- if election.is_vote_active %} {% trans %}You already have submitted your vote.{% endtrans %} @@ -45,12 +45,11 @@

{% csrf_token %} - {%- set election_lists = election.election_lists.all() -%} - + {%- for election_list in election_lists %} - - {%- set role_list = election.roles.order_by('order').all() %} - {%- for role in role_list %} - {%- set count = [0] %} + {%- for role in election_roles %} {%- set role_data = election_form.data.getlist(role.title) if role.title in election_form.data else [] %}

{{ role.title }}

{{ role.description }}

- {%- if role.max_choice > 1 and not election.has_voted(user) and election.can_vote(user) %} - {% trans %}You may choose up to{% endtrans %} {{ role.max_choice }} {% trans %}people.{% endtrans %} + {%- if role.max_choice > 1 and show_vote_buttons %} + + {% trans trimmed nb_choices=role.max_choice %} + You may choose up to {{ nb_choices }} people. + {% endtrans %} + {%- endif %} {%- if election_form.errors[role.title] is defined %} @@ -87,36 +88,40 @@ {% if user.can_edit(role) and election.is_vote_editable -%}
- - - {%- if role == role_list.last() %} + ️ + + + + + + {%- if loop.last -%} - {%- else %} + {%- else -%} - {%- endif %} - {% if role == role_list.first() %} + {%- endif -%} + {%- if loop.first -%} - {% else %} + {%- else -%} - {% endif %} + {%- endif -%}
{%- endif -%} - {%- for election_list in election_lists %} -
{% trans %}Blank vote{% endtrans %}{% trans %}Blank vote{% endtrans %} + {{ election_list.title }} {% if user.can_edit(election_list) and election.is_vote_editable -%} @@ -59,9 +58,7 @@ {%- endfor %}
- {%- if role.max_choice == 1 and election.can_vote(user) %} + + {%- if role.max_choice == 1 and show_vote_buttons %}
- -
- {%- set _ = count.append(count.pop() + 1) %} {%- endif %} {%- if election.is_vote_finished %} {%- set results = election_results[role.title]['blank vote'] %} @@ -126,13 +131,14 @@ {%- endif %}
+
    - {%- for candidature in election_list.candidatures.filter(role=role) %} + {%- for candidature in election_list.candidatures.select_related("user", "user__profile_pict").filter(role=role) %}
  • - {%- if election.can_vote(user) %} - -