Less precise election results

This commit is contained in:
Antoine Bartuccio 2017-01-10 19:06:34 +01:00
parent 766d913afe
commit e3711533da
3 changed files with 17 additions and 9 deletions

View File

@ -85,18 +85,22 @@ class Role(models.Model):
def results(self, total_vote): def results(self, total_vote):
results = {} results = {}
total_vote *= self.max_choice total_vote *= self.max_choice
if total_vote == 0:
return None
non_blank = 0 non_blank = 0
for candidature in self.candidatures.all(): for candidature in self.candidatures.all():
cand_results = {} cand_results = {}
cand_results['vote'] = self.votes.filter(candidature=candidature).count() 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'] non_blank += cand_results['vote']
results[candidature.user.username] = cand_results results[candidature.user.username] = cand_results
results['total vote'] = total_vote results['total vote'] = total_vote
results['blank vote'] = {'vote': total_vote - non_blank, if total_vote == 0:
'percent': (total_vote - non_blank) * 100 / total_vote} 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 return results
@property @property

View File

@ -301,7 +301,7 @@ th {
{%- if election.is_vote_finished %} {%- if election.is_vote_finished %}
{%- set results = election_results[role.title]['blank vote'] %} {%- set results = election_results[role.title]['blank vote'] %}
<div class="election__results"> <div class="election__results">
<strong>{{ results.vote }} {% trans %}votes{% endtrans %} ({{ results.percent }} %)</strong> <strong>{{ results.vote }} {% trans %}votes{% endtrans %} ({{ "%.2f" % results.percent }} %)</strong>
</div> </div>
{%- endif %} {%- endif %}
</td> </td>
@ -318,12 +318,14 @@ th {
</div> </div>
<figcaption class="candidate__details"> <figcaption class="candidate__details">
<cite class="candidate__full-name">{{ candidature.user.first_name }} <em class="candidate__nick-name">{{candidature.user.nick_name or ''}} </em>{{ candidature.user.last_name }}</cite> <cite class="candidate__full-name">{{ candidature.user.first_name }} <em class="candidate__nick-name">{{candidature.user.nick_name or ''}} </em>{{ candidature.user.last_name }}</cite>
{%- if not election.is_vote_finished %}
<q class="candidate__program">{{ candidature.program or '' }}</q> <q class="candidate__program">{{ candidature.program or '' }}</q>
{%- endif %}
{%- if user.can_edit(candidature) -%} {%- if user.can_edit(candidature) -%}
{% if election.is_vote_editable %} {% if election.is_vote_editable %}
<a href="{{url('election:update_candidate', candidature_id=candidature.id)}}">{% trans %}Edit{% endtrans %}</a> <a href="{{url('election:update_candidate', candidature_id=candidature.id)}}">{% trans %}Edit{% endtrans %}</a>
{% endif %} {% endif %}
{% if election.can_candidate -%} {% if election.is_vote_editable -%}
<a href="{{url('election:delete_candidate', candidature_id=candidature.id)}}">{% trans %}Delete{% endtrans %}</a> <a href="{{url('election:delete_candidate', candidature_id=candidature.id)}}">{% trans %}Delete{% endtrans %}</a>
{%- endif -%} {%- endif -%}
{%- endif -%} {%- endif -%}
@ -339,7 +341,7 @@ th {
{%- if election.is_vote_finished %} {%- if election.is_vote_finished %}
{%- set results = election_results[role.title][candidature.user.username] %} {%- set results = election_results[role.title][candidature.user.username] %}
<div class="election__results"> <div class="election__results">
<strong>{{ results.vote }} {% trans %}votes{% endtrans %} ({{ results.percent }} %)</strong> <strong>{{ results.vote }} {% trans %}votes{% endtrans %} ({{ "%.2f" % results.percent }} %)</strong>
</div> </div>
{%- endif %} {%- endif %}
</li> </li>
@ -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) %} {%- if (election.can_candidate(user) and election.is_candidature_active) or (user.can_edit(election) and election.is_vote_editable) %}
<a href="{{ url('election:candidate', election_id=object.id) }}">{% trans %}Candidate{% endtrans %}</a> <a href="{{ url('election:candidate', election_id=object.id) }}">{% trans %}Candidate{% endtrans %}</a>
{%- endif %} {%- endif %}
{%- if election.is_vote_editable %}
<a href="{{ url('election:create_list', election_id=object.id) }}">{% trans %}Add a new list{% endtrans %}</a> <a href="{{ url('election:create_list', election_id=object.id) }}">{% trans %}Add a new list{% endtrans %}</a>
{%- endif %}
{%- if user.can_edit(election) %} {%- if user.can_edit(election) %}
{% if election.is_vote_editable %} {% if election.is_vote_editable %}
<a href="{{ url('election:create_role', election_id=object.id) }}">{% trans %}Add a new role{% endtrans %}</a> <a href="{{ url('election:create_role', election_id=object.id) }}">{% trans %}Add a new role{% endtrans %}</a>

View File

@ -472,7 +472,7 @@ class CandidatureDeleteView(CanEditMixin, DeleteView):
def dispatch(self, request, *arg, **kwargs): def dispatch(self, request, *arg, **kwargs):
self.object = self.get_object() self.object = self.get_object()
self.election = self.object.role.election 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 raise PermissionDenied
return super(CandidatureDeleteView, self).dispatch(request, *arg, **kwargs) return super(CandidatureDeleteView, self).dispatch(request, *arg, **kwargs)