From 37a25185db69bf31d69bb470f42761f1f677750c Mon Sep 17 00:00:00 2001
From: Jonathan Weth <git@jonathanweth.de>
Date: Fri, 4 Dec 2020 15:10:33 +0100
Subject: [PATCH] Add option to show parent groups of affected groups and fix
 layout accordingly

---
 aleksis/apps/chronos/preferences.py                  | 10 ++++++++++
 .../chronos/templates/chronos/partials/groups.html   |  4 ++--
 .../templates/chronos/partials/groups_part.html      |  2 +-
 .../templates/chronos/partials/headerbox.html        |  4 ++--
 aleksis/apps/chronos/views.py                        | 12 ++++++++++--
 5 files changed, 25 insertions(+), 7 deletions(-)

diff --git a/aleksis/apps/chronos/preferences.py b/aleksis/apps/chronos/preferences.py
index cddfb3f0..e4fc8752 100644
--- a/aleksis/apps/chronos/preferences.py
+++ b/aleksis/apps/chronos/preferences.py
@@ -57,3 +57,13 @@ class SubstitutionsShowHeaderBox(BooleanPreference):
     default = True
     verbose_name = _("Show header box in substitution views")
     help_text = _("The header box shows affected teachers/groups.")
+
+
+@site_preferences_registry.register
+class AffectedGroupsUseParentGroups(BooleanPreference):
+    section = chronos
+    name = "affected_groups_parent_groups"
+    default = True
+    verbose_name = _(
+        "Show parent groups in header box in substitution views instead of original groups"
+    )
diff --git a/aleksis/apps/chronos/templates/chronos/partials/groups.html b/aleksis/apps/chronos/templates/chronos/partials/groups.html
index 3622cb41..fb85c6d4 100644
--- a/aleksis/apps/chronos/templates/chronos/partials/groups.html
+++ b/aleksis/apps/chronos/templates/chronos/partials/groups.html
@@ -1,5 +1,5 @@
 {% if groups.count == 1 and groups.0.parent_groups.all and request.site.preferences.chronos__use_parent_groups %}
-  {% include "chronos/partials/groups_part.html" with groups=groups.0.parent_groups.all %}
+  {% include "chronos/partials/groups_part.html" with groups=groups.0.parent_groups.all no_collapsible=no_collapsible %}
 {% else %}
-  {% include "chronos/partials/groups_part.html" with groups=groups %}
+  {% include "chronos/partials/groups_part.html" with groups=groups no_collapsible=no_collapsible %}
 {% endif %}
diff --git a/aleksis/apps/chronos/templates/chronos/partials/groups_part.html b/aleksis/apps/chronos/templates/chronos/partials/groups_part.html
index 60980ace..edafcb19 100644
--- a/aleksis/apps/chronos/templates/chronos/partials/groups_part.html
+++ b/aleksis/apps/chronos/templates/chronos/partials/groups_part.html
@@ -1,4 +1,4 @@
-{% if groups.count > request.site.preferences.chronos__shorten_groups_limit and request.user.person.preferences.chronos__shorten_groups %}
+{% if groups.count > request.site.preferences.chronos__shorten_groups_limit and request.user.person.preferences.chronos__shorten_groups and not no_collapsible %}
   {% include "components/text_collapsible.html" with template="chronos/partials/group.html" qs=groups %}
 {% else %}
   {% for group in groups %}
diff --git a/aleksis/apps/chronos/templates/chronos/partials/headerbox.html b/aleksis/apps/chronos/templates/chronos/partials/headerbox.html
index 6d30df91..d298d5fc 100644
--- a/aleksis/apps/chronos/templates/chronos/partials/headerbox.html
+++ b/aleksis/apps/chronos/templates/chronos/partials/headerbox.html
@@ -23,7 +23,7 @@
             </strong>
           </div>
           <div class="col s12 m9 black-text-a">
-            {% include "chronos/partials/groups.html" with groups=absent_groups %}
+            {% include "chronos/partials/groups.html" with groups=absent_groups no_collapsible=True %}
           </div>
         </div>
       {% endif %}
@@ -47,7 +47,7 @@
             </strong>
           </div>
           <div class="col s12 m9 black-text-a">
-            {% include "chronos/partials/groups.html" with groups=affected_groups %}
+            {% include "chronos/partials/groups.html" with groups=affected_groups no_collapsible=True %}
           </div>
         </div>
       {% endif %}
diff --git a/aleksis/apps/chronos/views.py b/aleksis/apps/chronos/views.py
index c56a2e5c..82bc1616 100644
--- a/aleksis/apps/chronos/views.py
+++ b/aleksis/apps/chronos/views.py
@@ -1,7 +1,7 @@
 from datetime import datetime, timedelta
 from typing import Optional
 
-from django.db.models import Count
+from django.db.models import Count, Q
 from django.http import HttpRequest, HttpResponse, HttpResponseNotFound
 from django.shortcuts import get_object_or_404, redirect, render
 from django.urls import reverse
@@ -311,7 +311,15 @@ def substitutions(
             day_contexts[day]["absent_teachers"] = absences.absent_teachers()
             day_contexts[day]["absent_groups"] = absences.absent_groups()
             day_contexts[day]["affected_teachers"] = subs.affected_teachers()
-            day_contexts[day]["affected_groups"] = subs.affected_groups()
+            affected_groups = subs.affected_groups()
+            if get_site_preferences()["chronos__affected_groups_parent_groups"]:
+                groups_with_parent_groups = affected_groups.filter(parent_groups__isnull=False)
+                groups_without_parent_groups = affected_groups.filter(parent_groups__isnull=True)
+                affected_groups = Group.objects.filter(
+                    Q(child_groups__pk__in=groups_with_parent_groups.values_list("pk", flat=True))
+                    | Q(pk__in=groups_without_parent_groups.values_list("pk", flat=True))
+                ).distinct()
+            day_contexts[day]["affected_groups"] = affected_groups
 
     if not is_print:
         context = day_contexts[wanted_day]
-- 
GitLab