From 924ffb6155cb7010b43989c5fb1927fa13d5a5ea Mon Sep 17 00:00:00 2001
From: George Katsikas <giorgakis.katsikas@gmail.com>
Date: Thu, 4 Apr 2024 12:54:31 +0200
Subject: [PATCH] add ordering by latest event in nominations

fixes #238
---
 scipost_django/colleges/forms.py                  | 15 ++++++++++++---
 .../colleges/_hx_nomination_summary.html          |  4 ++--
 2 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/scipost_django/colleges/forms.py b/scipost_django/colleges/forms.py
index 7705205a9..cbe62e424 100644
--- a/scipost_django/colleges/forms.py
+++ b/scipost_django/colleges/forms.py
@@ -631,15 +631,15 @@ class FellowshipNominationSearchForm(forms.Form):
             ("latest_round_decision_outcome", "Decision"),
             ("profile__last_name", "Nominee"),
             ("nominated_on", "Nominated date"),
+            ("latest_event_on", "Last event date"),
         ),
         required=False,
     )
     ordering = forms.ChoiceField(
         label="Ordering",
         choices=(
-            # FIXME: Emperically, the ordering appers to be reversed for dates?
-            ("-", "Ascending"),
-            ("+", "Descending"),
+            ("+", "Ascending"),
+            ("-", "Descending"),
         ),
         required=False,
     )
@@ -734,6 +734,13 @@ class FellowshipNominationSearchForm(forms.Form):
                 .values(key)[:1]
             )
 
+        def latest_event_subquery(key):
+            return Subquery(
+                FellowshipNominationEvent.objects.filter(nomination=OuterRef("pk"))
+                .order_by("-on")
+                .values(key)[:1]
+            )
+
         nominations = (
             FellowshipNomination.objects.all()
             .annotate(
@@ -742,6 +749,8 @@ class FellowshipNominationSearchForm(forms.Form):
                 latest_round_decision_outcome=latest_round_subquery(
                     "decision__outcome"
                 ),
+                latest_event_on=latest_event_subquery("on"),
+                latest_event_description=latest_event_subquery("description"),
             )
             .distinct()
         )
diff --git a/scipost_django/colleges/templates/colleges/_hx_nomination_summary.html b/scipost_django/colleges/templates/colleges/_hx_nomination_summary.html
index 9491c5d49..e36cc2929 100644
--- a/scipost_django/colleges/templates/colleges/_hx_nomination_summary.html
+++ b/scipost_django/colleges/templates/colleges/_hx_nomination_summary.html
@@ -48,9 +48,9 @@
                 <div class="col text-truncate ">
                   <small class="text-muted">Last event</small>
                   <br />
-                  <time>{{ nomination.events.latest.on|date:'Y-m-d' }}</time>
+                  <time>{{ nomination.latest_event_on|date:'Y-m-d' }}</time>
                   -
-                  <span>{{ nomination.events.latest.description|truncatechars:40 }}</span>
+                  <span>{{ nomination.latest_event_description|truncatechars:40 }}</span>
                 </div>
               {% endif %}
 
-- 
GitLab