From e3711533dae274c8745354f5fc6ca919e2f57448 Mon Sep 17 00:00:00 2001 From: klmp200 Date: Tue, 10 Jan 2017 19:06:34 +0100 Subject: [PATCH] Less precise election results --- election/models.py | 14 +++++++++----- election/templates/election/election_detail.jinja | 10 +++++++--- election/views.py | 2 +- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/election/models.py b/election/models.py index 635ec7e5..b2659424 100644 --- a/election/models.py +++ b/election/models.py @@ -85,18 +85,22 @@ class Role(models.Model): def results(self, total_vote): results = {} total_vote *= self.max_choice - if total_vote == 0: - return None non_blank = 0 for candidature in self.candidatures.all(): cand_results = {} cand_results['vote'] = self.votes.filter(candidature=candidature).count() - cand_results['percent'] = cand_results['vote'] * 100 / total_vote + if total_vote == 0: + cand_results['percent'] = 0 + else: + cand_results['percent'] = cand_results['vote'] * 100 / total_vote non_blank += cand_results['vote'] results[candidature.user.username] = cand_results results['total vote'] = total_vote - results['blank vote'] = {'vote': total_vote - non_blank, - 'percent': (total_vote - non_blank) * 100 / total_vote} + if total_vote == 0: + results['blank vote'] = {'vote': 0, 'percent': 0} + else: + results['blank vote'] = {'vote': total_vote - non_blank, + 'percent': (total_vote - non_blank) * 100 / total_vote} return results @property diff --git a/election/templates/election/election_detail.jinja b/election/templates/election/election_detail.jinja index 777c00d8..5f2b4e57 100644 --- a/election/templates/election/election_detail.jinja +++ b/election/templates/election/election_detail.jinja @@ -301,7 +301,7 @@ th { {%- if election.is_vote_finished %} {%- set results = election_results[role.title]['blank vote'] %}
- {{ results.vote }} {% trans %}votes{% endtrans %} ({{ results.percent }} %) + {{ results.vote }} {% trans %}votes{% endtrans %} ({{ "%.2f" % results.percent }} %)
{%- endif %} @@ -318,12 +318,14 @@ th {
{{ candidature.user.first_name }} {{candidature.user.nick_name or ''}} {{ candidature.user.last_name }} + {%- if not election.is_vote_finished %} {{ candidature.program or '' }} + {%- endif %} {%- if user.can_edit(candidature) -%} {% if election.is_vote_editable %} {% trans %}Edit{% endtrans %} {% endif %} - {% if election.can_candidate -%} + {% if election.is_vote_editable -%} {% trans %}Delete{% endtrans %} {%- endif -%} {%- endif -%} @@ -339,7 +341,7 @@ th { {%- if election.is_vote_finished %} {%- set results = election_results[role.title][candidature.user.username] %}
- {{ results.vote }} {% trans %}votes{% endtrans %} ({{ results.percent }} %) + {{ results.vote }} {% trans %}votes{% endtrans %} ({{ "%.2f" % results.percent }} %)
{%- endif %} @@ -362,7 +364,9 @@ th { {%- if (election.can_candidate(user) and election.is_candidature_active) or (user.can_edit(election) and election.is_vote_editable) %} {% trans %}Candidate{% endtrans %} {%- endif %} + {%- if election.is_vote_editable %} {% trans %}Add a new list{% endtrans %} + {%- endif %} {%- if user.can_edit(election) %} {% if election.is_vote_editable %} {% trans %}Add a new role{% endtrans %} diff --git a/election/views.py b/election/views.py index 11f933c3..f9091906 100644 --- a/election/views.py +++ b/election/views.py @@ -472,7 +472,7 @@ class CandidatureDeleteView(CanEditMixin, DeleteView): def dispatch(self, request, *arg, **kwargs): self.object = self.get_object() self.election = self.object.role.election - if not self.election.can_candidate: + if not self.election.can_candidate or not self.election.is_vote_editable: raise PermissionDenied return super(CandidatureDeleteView, self).dispatch(request, *arg, **kwargs)