From 02de74103cf7aa8c4030fbae3239cc591785dd5f Mon Sep 17 00:00:00 2001
From: Jonathan Weth <git@jonathanweth.de>
Date: Sun, 6 Jun 2021 14:35:30 +0200
Subject: [PATCH] Use distincts for affected querysets to prevent double
 entries

(cherry picked from commit 4c705a68d687a5417b29a6ee56aa23c627f31964)
---
 CHANGELOG.rst                    |  1 +
 aleksis/apps/chronos/managers.py | 18 +++++++++++++-----
 2 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index 2f649677..ce6d5f86 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -19,6 +19,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.
 
 `2.0b2` - 2021-06-02
 --------------------
diff --git a/aleksis/apps/chronos/managers.py b/aleksis/apps/chronos/managers.py
index 82021ef4..042d91a6 100644
--- a/aleksis/apps/chronos/managers.py
+++ b/aleksis/apps/chronos/managers.py
@@ -545,7 +545,7 @@ class LessonSubstitutionQuerySet(LessonDataQuerySet):
         """Return all lessons which are affected by selected substitutions."""
         from .models import Lesson  # noaq
 
-        return Lesson.objects.filter(lesson_periods__substitutions__in=self)
+        return Lesson.objects.filter(lesson_periods__substitutions__in=self).distinct()
 
     def affected_teachers(self):
         """Get affected teachers.
@@ -553,13 +553,21 @@ class LessonSubstitutionQuerySet(LessonDataQuerySet):
         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)
-        ).order_by("short_name")
+        return (
+            Person.objects.filter(
+                Q(lessons_as_teacher__in=self.affected_lessons()) | Q(lesson_substitutions__in=self)
+            )
+            .distinct()
+            .order_by("short_name")
+        )
 
     def affected_groups(self):
         """Return all groups which are affected by selected substitutions."""
-        return Group.objects.filter(lessons__in=self.affected_lessons()).order_by("short_name")
+        return (
+            Group.objects.filter(lessons__in=self.affected_lessons())
+            .distinct()
+            .order_by("short_name")
+        )
 
 
 class DateRangeQuerySetMixin:
-- 
GitLab