From dc6c626e333815b205a53a270f818b14185e65df Mon Sep 17 00:00:00 2001
From: Jonathan Weth <git@jonathanweth.de>
Date: Sat, 21 Nov 2020 17:50:18 +0100
Subject: [PATCH] [Substitutions list/print] Show equal substitutions over
 multiple periods in one row

---
 .../chronos/partials/subs/period.html         |  4 ++-
 .../templates/chronos/substitutions.html      |  2 +-
 .../chronos/substitutions_print.html          |  2 +-
 aleksis/apps/chronos/util/build.py            | 25 ++++++++++++++++++-
 4 files changed, 29 insertions(+), 4 deletions(-)

diff --git a/aleksis/apps/chronos/templates/chronos/partials/subs/period.html b/aleksis/apps/chronos/templates/chronos/partials/subs/period.html
index 7d09cbeb..ef1d2838 100644
--- a/aleksis/apps/chronos/templates/chronos/partials/subs/period.html
+++ b/aleksis/apps/chronos/templates/chronos/partials/subs/period.html
@@ -1,6 +1,8 @@
 <strong>
-  {% if type == "substitution" %}
+  {% if type == "substitution" and item.start_period == item.end_period %}
     {{ el.lesson_period.period.period }}.
+  {% elif type == "substitution"  %}
+    {{ item.start_period }}.–{{ item.end_period }}.
   {% elif type == "extra_lesson" %}
     {{ el.period.period }}.
   {% elif type == "event" %}
diff --git a/aleksis/apps/chronos/templates/chronos/substitutions.html b/aleksis/apps/chronos/templates/chronos/substitutions.html
index cb47a88c..82e5d403 100644
--- a/aleksis/apps/chronos/templates/chronos/substitutions.html
+++ b/aleksis/apps/chronos/templates/chronos/substitutions.html
@@ -64,7 +64,7 @@
           {% include "chronos/partials/subs/groups.html" with type=item.type el=item.el %}
         </td>
         <td>
-          {% include "chronos/partials/subs/period.html" with type=item.type el=item.el %}
+          {% include "chronos/partials/subs/period.html" with type=item.type el=item.el item=item %}
         </td>
         <td>
           {% include "chronos/partials/subs/teachers.html" with type=item.type el=item.el %}
diff --git a/aleksis/apps/chronos/templates/chronos/substitutions_print.html b/aleksis/apps/chronos/templates/chronos/substitutions_print.html
index 29853991..adfdfebe 100644
--- a/aleksis/apps/chronos/templates/chronos/substitutions_print.html
+++ b/aleksis/apps/chronos/templates/chronos/substitutions_print.html
@@ -57,7 +57,7 @@
             {% include "chronos/partials/subs/groups.html" with type=item.type el=item.el %}
           </td>
           <td>
-            {% include "chronos/partials/subs/period.html" with type=item.type el=item.el %}
+            {% include "chronos/partials/subs/period.html" with type=item.type el=item.el item=item %}
           </td>
           <td>
             {% include "chronos/partials/subs/teachers.html" with type=item.type el=item.el %}
diff --git a/aleksis/apps/chronos/util/build.py b/aleksis/apps/chronos/util/build.py
index 08800d32..db23417d 100644
--- a/aleksis/apps/chronos/util/build.py
+++ b/aleksis/apps/chronos/util/build.py
@@ -245,19 +245,42 @@ def build_substitutions_list(wanted_day: date) -> List[dict]:
         "lesson_period__lesson__groups", "lesson_period__period"
     )
 
-    for sub in subs:
+    start_period = None
+    for i, sub in enumerate(subs):
         if not sub.cancelled_for_teachers:
             sort_a = sub.lesson_period.lesson.group_names
         else:
             sort_a = f"Z.{sub.lesson_period.lesson.teacher_names}"
 
+        # Get next substitution
+        next_sub = subs[i + 1] if i + 1 < len(subs) else None
+
+        # Check if next substitution is equal with this substitution
+        if (
+            next_sub
+            and sub.comment == next_sub.comment
+            and sub.cancelled == next_sub.cancelled
+            and sub.subject == next_sub.subject
+            and sub.room == next_sub.room
+            and sub.lesson_period.lesson == next_sub.lesson_period.lesson
+            and set(sub.teachers.all()) == set(next_sub.teachers.all())
+        ):
+            if not start_period:
+                start_period = sub.lesson_period.period.period
+            continue
+
         row = {
             "type": "substitution",
             "sort_a": sort_a,
             "sort_b": str(sub.lesson_period.period.period),
             "el": sub,
+            "start_period": start_period if start_period else sub.lesson_period.period.period,
+            "end_period": sub.lesson_period.period.period,
         }
 
+        if start_period:
+            start_period = None
+
         rows.append(row)
 
     # Get supervision substitutions
-- 
GitLab