From 2045aa19800e7b27de7f857b0423f56d4e1c7f8f Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jan 09 2019 10:55:16 +0000 Subject: Fix the logic around showing who won the election With more recent version of jinja2 the way we were using {% set %} was no longer working because of the limited scope of this macro. With this commit we replace the use of {% set %} in the inner scope by using a list and just checking the last element in that list. From there, we could mimic the old behavior we had with sets. For simplicity, we also moved the sorting of the candidates out of the templates and into the controller just before calling the template. Signed-off-by: Pierre-Yves Chibon --- diff --git a/fedora_elections/__init__.py b/fedora_elections/__init__.py index 88adfa2..7d51af8 100644 --- a/fedora_elections/__init__.py +++ b/fedora_elections/__init__.py @@ -272,7 +272,10 @@ def about_election(election_alias): stats=stats, voted=voted, evolution_label=evolution_label, - evolution_data=evolution_data) + evolution_data=evolution_data, + candidates=sorted( + election.candidates, key=lambda x: x.vote_count, reverse=True), + ) @APP.route('/archives') diff --git a/fedora_elections/elections.py b/fedora_elections/elections.py index ac80ef7..8516066 100644 --- a/fedora_elections/elections.py +++ b/fedora_elections/elections.py @@ -141,6 +141,8 @@ def election_results_text(election_alias): election=election, usernamemap=usernamemap, stats=stats, + candidates=sorted( + election.candidates, key=lambda x: x.vote_count, reverse=True) ) diff --git a/fedora_elections/templates/about.html b/fedora_elections/templates/about.html index 2518b27..b87fea8 100644 --- a/fedora_elections/templates/about.html +++ b/fedora_elections/templates/about.html @@ -107,41 +107,44 @@ {% endif %}

Results

- - - - - {% if stats['candidate_voters'] %} - - - {% endif %} - + + + + + {% if stats['candidate_voters'] %} + + + {% endif %} + - {% for candidate in election.candidates|sort(attribute='vote_count', reverse=True) %} - {% if loop.index <= election.seats_elected %} - {# If we are below the number of user that will be selected, get the number - of votes and the flag to False#} - {% set flag = False %} - {% set votes = candidate.vote_count %} - {% elif loop.index > election.seats_elected and votes > candidate.vote_count and not flag %} - {# if we are above the number of user that will be selected (seats - available), check if the number of votes for this candidate is lower than - the number of votes for the last candidate and if the Flag is False - So this takes care of the case where there are 10 seats elected and the 11th - candidate has the same score as the 10th one. - In this case we would end up with one more person that the number of seats - available and we'll need to either find a way to select one over the other - or deal with having one more candidate accepted #} - {% set flag = True %} - {% set lastrow = True %} - {% else %} - {# we are above the number of seats available, the number of votes is below - that of the last candidate above selected and the Flag is True which means - we already passed the condition above #} - {% set lastrow = False %} - {% endif %} - + {%- set lastrow = [0] -%} + {%- set flag = [0] -%} + {% for candidate in candidates %} + {% if loop.index <= election.seats_elected %} + {# If we are below the number of user that will be selected, get the number + of votes and the flag to False#} + {%- set _ = flag.append(0) -%} + {%- elif loop.index > election.seats_elected + and candidates[loop.index -2].vote_count > candidate.vote_count + and flag[-1] == 0 -%} + {# if we are above the number of user that will be selected (seats + available), check if the number of votes for this candidate is lower than + the number of votes for the last candidate and if the Flag is False + So this takes care of the case where there are 10 seats elected and the 11th + candidate has the same score as the 10th one. + In this case we would end up with one more person that the number of seats + available and we'll need to either find a way to select one over the other + or deal with having one more candidate accepted #} + {%- set _ = lastrow.append(1) -%} + {%- set _ = flag.append(1) -%} + {% else %} + {# we are above the number of seats available, the number of votes is below + that of the last candidate above selected and the Flag is True which means + we already passed the condition above #} + {% set _ = lastrow.append(0) -%} + {% endif %} + diff --git a/fedora_elections/templates/results_text.html b/fedora_elections/templates/results_text.html index 9c99734..14f9503 100644 --- a/fedora_elections/templates/results_text.html +++ b/fedora_elections/templates/results_text.html @@ -21,14 +21,17 @@ could accumulate up to {{ stats['n_voters'] * stats['max_vote'] }} votes ({{stat The results for the elections are as follows: # votes | name -- --------+----------------------{% -for candidate in election.candidates|sort(attribute='vote_count', reverse=True) -%} +- --------+---------------------- +{%- set lastrow = [0] -%} +{%- set flag = [0] -%} +{%- for candidate in candidates -%} {% if loop.index <= election.seats_elected -%} - {# If we are below the number of user that will be selected, + {# If we are below the number of user that will be selected get the number of votes and the flag to False -#} - {% set flag = False -%} - {% set votes = candidate.vote_count -%} - {%- elif loop.index > election.seats_elected and votes > candidate.vote_count and not flag -%} + {%- set _ = flag.append(0) -%} + {%- elif loop.index > election.seats_elected + and candidates[loop.index -2].vote_count > candidate.vote_count + and flag[-1] == 0 -%} {# if we are above the number of user that will be selected (seats available), check if the number of votes for this candidate is lower than the number of votes for the last candidate and if the Flag is @@ -38,14 +41,17 @@ for candidate in election.candidates|sort(attribute='vote_count', reverse=True) In this case we would end up with one more person that the number of seats available and we'll need to either find a way to select one over the other or deal with having one more candidate accepted -#} - {% set flag = True -%} - {% set lastrow = True -%} + {%- set _ = lastrow.append(1) -%} + {%- set _ = flag.append(1) -%} {%- else -%} {# we are above the number of seats available, the number of votes is below that of the last candidate above selected and the Flag is True which means we already passed the condition above -#} - {% set lastrow = False -%} {%- endif %}{% if lastrow == True %} -- --------+---------------------- {%- endif %} + {% set _ = lastrow.append(0) -%} + {%- endif -%} + {%- if lastrow[-1] == 1 %} +- --------+---------------------- + {%- endif %} {{ candidate.vote_count | rjust(8) }} | {% if election.candidates_are_fasusers -%} {{ usernamemap[candidate.id] }} {%- else -%} {{candidate.name}} {%- endif %}
CandidateVotesVoters per candidateAverage votes per candidate
CandidateVotesVoters per candidateAverage votes per candidate
{% if candidate.url %} @@ -154,7 +157,7 @@ {% if candidate.url %} {% endif %} - {% if not flag %} + {% if flag[-1] == 0 %} Elected {% endif %}