From 1005ae503aa52acd15ce1577487a652f43876dd6 Mon Sep 17 00:00:00 2001
From: Jonathan Weth <git@jonathanweth.de>
Date: Fri, 10 Apr 2020 14:48:14 +0200
Subject: [PATCH] Fix that substituted lessons and supervisions has only been
 shown at the old teacher

---
 aleksis/apps/chronos/models.py     | 14 +++++++++++++-
 aleksis/apps/chronos/util/build.py |  2 +-
 2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/aleksis/apps/chronos/models.py b/aleksis/apps/chronos/models.py
index 194b5fc9..41172d67 100644
--- a/aleksis/apps/chronos/models.py
+++ b/aleksis/apps/chronos/models.py
@@ -222,7 +222,7 @@ class LessonPeriodQuerySet(LessonDataQuerySet):
         if type_ == "teacher":
             # Teacher
 
-            return person.lesson_periods_as_teacher
+            return self.filter_teacher(person)
 
         elif type_ == "group":
             # Student
@@ -761,6 +761,18 @@ class SupervisionQuerySet(models.QuerySet):
             | Q(break_item__after_period__weekday=weekday)
         )
 
+    def filter_by_teacher(self, teacher: Union[Person, int]):
+        """ Filter for all supervisions given by a certain teacher. """
+
+        if self.count() > 0:
+            week = CalendarWeek(week=self[0]._week)
+
+            dates = [week[w] for w in range(0, 7)]
+
+            return self.filter(Q(substitutions__teacher=teacher, substitutions__date__in=dates) | Q(teacher=teacher))
+
+        return self
+
 
 class Supervision(ExtensibleModel):
     objects = models.Manager.from_queryset(SupervisionQuerySet)()
diff --git a/aleksis/apps/chronos/util/build.py b/aleksis/apps/chronos/util/build.py
index 5e9f7cef..ee9d8710 100644
--- a/aleksis/apps/chronos/util/build.py
+++ b/aleksis/apps/chronos/util/build.py
@@ -61,7 +61,7 @@ def build_timetable(
             week = CalendarWeek.from_date(date_ref)
         else:
             week = date_ref
-        supervisions = Supervision.objects.filter(teacher=obj).annotate_week(week)
+        supervisions = Supervision.objects.all().annotate_week(week).filter_by_teacher(obj)
 
         if is_person:
             supervisions.filter_by_weekday(date_ref.weekday())
-- 
GitLab