Skip to content

Commit

Permalink
Sort by proportional percentage
Browse files Browse the repository at this point in the history
  • Loading branch information
fsargent committed Jan 24, 2025
1 parent 241f0d5 commit 84aa8c6
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions approval_polls/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,18 +309,24 @@ def get_context_data(self, **kwargs):
proportional_votes[choice_id] += weight
total_proportional_votes += weight

proportional_results = [
{
"choice_text": choice.choice_text,
"proportional_votes": proportional_votes[choice.id],
"proportional_percentage": (
proportional_votes[choice.id] / total_proportional_votes * 100
if total_proportional_votes > 0
else 0
),
}
for choice in poll.choice_set.all()
]
proportional_results = sorted(
[
{
"choice_text": choice.choice_text,
"proportional_votes": proportional_votes[choice.id],
"proportional_percentage": (
proportional_votes[choice.id] / total_proportional_votes * 100
if total_proportional_votes > 0
else 0
),
}
for choice in poll.choice_set.all()
],
key=lambda x: x[
"proportional_percentage"
], # Sort by proportional_percentage
reverse=True, # Highest percentage first
)

# Add data to context
context.update(
Expand Down

0 comments on commit 84aa8c6

Please sign in to comment.