From 8cbb193c2ea6c6bb1eb047c6f592e82432c74803 Mon Sep 17 00:00:00 2001
From: Jonathan Weth <git@jonathanweth.de>
Date: Mon, 9 Mar 2020 17:54:03 +0100
Subject: [PATCH] Fix queries for affected teachers and groups in order to show
 them only one time in the list

Close #67
---
 aleksis/apps/chronos/models.py | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/aleksis/apps/chronos/models.py b/aleksis/apps/chronos/models.py
index 5a6eb38f..b2a35a90 100644
--- a/aleksis/apps/chronos/models.py
+++ b/aleksis/apps/chronos/models.py
@@ -254,12 +254,17 @@ class LessonSubstitutionQuerySet(LessonDataQuerySet):
     def affected_teachers(self):
         """ Return all teachers which are affected by selected substitutions (as substituted or substituting) """
 
-        return Person.objects.filter(Q(lessons_as_teacher__in=self.affected_lessons()) | Q(lesson_substitutions__in=self))
+        return Person.objects.filter(
+            Q(lessons_as_teacher__in=self.affected_lessons())
+            | Q(lesson_substitutions__in=self)
+        ).annotate(lessons_count=Count("lessons_as_teacher"))
 
     def affected_groups(self):
         """ Return all groups which are affected by selected substitutions """
 
-        return Group.objects.filter(lessons__in=self.affected_lessons())
+        return Group.objects.filter(lessons__in=self.affected_lessons()).annotate(
+            lessons_count=Count("lessons")
+        )
 
 
 class TimePeriod(models.Model):
-- 
GitLab