From 6f774c1dfd43897fddd14db9a89f6d013b21cfb0 Mon Sep 17 00:00:00 2001
From: Jonathan Weth <git@jonathanweth.de>
Date: Tue, 8 Jun 2021 19:45:47 +0200
Subject: [PATCH] Fix lessons_on_day to work correctly if a person has no
 lessons

---
 CHANGELOG.rst                            | 1 +
 aleksis/apps/chronos/model_extensions.py | 6 +++++-
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index ce6d5f86..f61c4ed0 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -20,6 +20,7 @@ Fixed
 * Preference section verbose names were displayed in server language and not
   user language (fixed by using gettext_lazy).
 * Affected groups and persons in substitutions list were displayed multiple times.
+* ``lessons_on_day`` didn't work as expected if a person has no lessons.
 
 `2.0b2` - 2021-06-02
 --------------------
diff --git a/aleksis/apps/chronos/model_extensions.py b/aleksis/apps/chronos/model_extensions.py
index 6cb98dad..570df4e2 100644
--- a/aleksis/apps/chronos/model_extensions.py
+++ b/aleksis/apps/chronos/model_extensions.py
@@ -83,7 +83,11 @@ def lesson_periods_as_teacher(self):
 @Person.method
 def lessons_on_day(self, day: date):
     """Get all lessons of this person (either as participant or teacher) on the given day."""
-    return LessonPeriod.objects.on_day(day).filter_from_person(self).order_by("period__period")
+    qs = LessonPeriod.objects.on_day(day).filter_from_person(self)
+    if qs:
+        # This is a union queryset, so order by must be after the union.
+        return qs.order_by("period__period")
+    return None
 
 
 @Person.method
-- 
GitLab