From 5559ac9472f536e4c7916669a25c19abc90d09af Mon Sep 17 00:00:00 2001
From: Julian Leucker <leuckerj@gmail.com>
Date: Sat, 15 May 2021 22:55:12 +0200
Subject: [PATCH] Annotate subject and date in the personal note queryset

---
 aleksis/apps/alsijil/managers.py | 19 +++++++++++++++++++
 aleksis/apps/alsijil/views.py    |  2 +-
 2 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/aleksis/apps/alsijil/managers.py b/aleksis/apps/alsijil/managers.py
index 4c7741d3b..555042a12 100644
--- a/aleksis/apps/alsijil/managers.py
+++ b/aleksis/apps/alsijil/managers.py
@@ -6,6 +6,7 @@ from django.db.models.fields import DateField
 from django.db.models.functions import Concat
 from django.db.models.query import Prefetch
 from django.db.models.query_utils import Q
+from django.utils.translation import gettext as _
 
 from calendarweek import CalendarWeek
 
@@ -71,6 +72,24 @@ class RegisterObjectRelatedQuerySet(QuerySet):
             ),
         )
 
+    def annotate_subject(self) -> QuerySet:
+        """
+        Annotate every personal note/lesson documentation with the subject of the lesson/event.
+        """
+        return self.annotate(
+            subject=Case(
+                When(
+                    lesson_period__isnull=False,
+                    then="lesson_period__lesson__subject__name",
+                ),
+                When(
+                    extra_lesson__isnull=False,
+                    then="extra_lesson__subject__name",
+                ),
+                default=Value(_("Event"))
+            )
+        )
+
 
 class PersonalNoteManager(CurrentSiteManagerWithoutMigrations):
     """Manager adding specific methods to personal notes."""
diff --git a/aleksis/apps/alsijil/views.py b/aleksis/apps/alsijil/views.py
index c3bfd95ff..f8bff4645 100644
--- a/aleksis/apps/alsijil/views.py
+++ b/aleksis/apps/alsijil/views.py
@@ -807,7 +807,7 @@ def overview_person(request: HttpRequest, id_: Optional[int] = None) -> HttpResp
         )
         .order_by(
             "-school_term_start", "-order_year", "-order_week", "-order_weekday", "order_period",
-        )
+        ).annotate_date_range().annotate_subject()
     )
     personal_note_filter_object = PersonalNoteFilter(request.GET, queryset=personal_notes)
     filtered_personal_notes = personal_note_filter_object.qs
-- 
GitLab