From 4b0d35c1b365c94da438a758321454a427d2099c Mon Sep 17 00:00:00 2001 From: George Katsikas <giorgakis.katsikas@gmail.com> Date: Tue, 26 Sep 2023 19:16:32 +0200 Subject: [PATCH] fix dup nominations when sorting by round props --- scipost_django/colleges/forms.py | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/scipost_django/colleges/forms.py b/scipost_django/colleges/forms.py index ee3448800..2fba465b6 100644 --- a/scipost_django/colleges/forms.py +++ b/scipost_django/colleges/forms.py @@ -547,9 +547,9 @@ class FellowshipNominationSearchForm(forms.Form): orderby = forms.ChoiceField( label="Order by", choices=( - ("voting_rounds__voting_deadline", "Deadline"), - ("voting_rounds__voting_opens", "Voting start"), - ("voting_rounds__decision__outcome", "Decision"), + ("latest_round_deadline", "Deadline"), + ("latest_round_open", "Voting start"), + ("latest_round_decision_outcome", "Decision"), ("profile__last_name", "Nominee"), ), required=False, @@ -632,13 +632,32 @@ class FellowshipNominationSearchForm(forms.Form): session.save() - nominations = FellowshipNomination.objects.all() + def latest_round_subquery(key): + return Subquery( + FellowshipNominationVotingRound.objects.filter( + nomination=OuterRef("pk") + ) + .order_by("-voting_deadline") + .values(key)[:1] + ) + + nominations = ( + FellowshipNomination.objects.all() + .annotate( + latest_round_deadline=latest_round_subquery("voting_deadline"), + latest_round_open=latest_round_subquery("voting_opens"), + latest_round_decision_outcome=latest_round_subquery( + "decision__outcome" + ), + ) + .distinct() + ) if self.cleaned_data.get("can_vote") or not self.user.has_perm( "scipost.can_view_all_nomination_voting_rounds" ): # Restrict rounds to those the user can vote on - nominations = nominations.with_user_votable_rounds(self.user) + nominations = nominations.with_user_votable_rounds(self.user).distinct() if nominee := self.cleaned_data.get("nominee"): nominations = nominations.filter( -- GitLab