Skip to content

Commit

Permalink
Add and render candidate rank
Browse files Browse the repository at this point in the history
  • Loading branch information
VirginiaDooley committed May 5, 2023
1 parent cc8960a commit 4449553
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
18 changes: 18 additions & 0 deletions wcivf/apps/people/migrations/0043_personpost_rank.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.1.6 on 2023-05-05 09:01

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("people", "0042_personpost_previous_party_affiliations"),
]

operations = [
migrations.AddField(
model_name="personpost",
name="rank",
field=models.PositiveIntegerField(null=True),
),
]
15 changes: 15 additions & 0 deletions wcivf/apps/people/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class PersonPost(models.Model):
previous_party_affiliations = models.ManyToManyField(
Party, related_name="affiliated_memberships", blank=True
)
rank = models.PositiveIntegerField(null=True)

objects = PersonPostManager()

Expand Down Expand Up @@ -110,6 +111,20 @@ def get_results_text(self) -> str:
}
return _("Not elected (vote count not available)")

@property
def get_results_rank(self):
"""Get the rank of the person in the election"""

election = self.post_election.election
candidate_count = len(election.personpost_set.all())
sorted_votes_cast = election.personpost_set.order_by("-votes_cast")
# find the index of the candidate in the sorted_votes_cast
# and assign the index + 1 to the rank
for index, candidate in enumerate(sorted_votes_cast):
if candidate.person == self.person:
rank = index + 1
return f"{ordinal(rank)} / {candidate_count} candidates"

class Meta:
ordering = ("-election__election_date",)
unique_together = ("person", "post", "election")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ <h3 class="ds-padded">{{ object.name }}'s {% trans "Elections" %}</h3>
</td>
{% endif %}
<td>
TO DO: UPDATE WITH POSITION SUCH AS 2/8, IF AVAILABLE
{% if person_post.votes_cast %}
{{ person_post.get_results_rank }}
{% else %}
Position not available
{% endif %}
</td>
</tr>
{% endfor %}
Expand Down

0 comments on commit 4449553

Please sign in to comment.