From 2725493c07c52f7da85b4be5c845a7653770b48a Mon Sep 17 00:00:00 2001
From: Dominik George <dominik.george@teckids.org>
Date: Sat, 14 Sep 2019 17:31:12 +0200
Subject: [PATCH] Allow importing a new version of a timetable. Closes #3.

---
 biscuit/apps/untis/util.py | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/biscuit/apps/untis/util.py b/biscuit/apps/untis/util.py
index de2e030..45ef67c 100644
--- a/biscuit/apps/untis/util.py
+++ b/biscuit/apps/untis/util.py
@@ -1,4 +1,4 @@
-from datetime import date, time
+from datetime import date, time, timedelta
 from typing import BinaryIO, Optional, Union
 from xml.dom import minidom, Node
 
@@ -81,7 +81,13 @@ def untis_import_xml(request: HttpRequest, untis_xml: Union[BinaryIO, str]) -> N
             messages.warning(request, _('Could not set class teacher of %(class)s to %(teacher)s.') % {
                 'class': short_name, 'teacher': class_teacher_short_name})
 
-    Lesson.objects.all().delete()
+    # Set all existing lessons that overlap to end today
+    today = date.today()
+    Lesson.objects.filter(
+        date_end__gt=today
+    ).update(
+        date_end=today
+    )
 
     lessons = dom.getElementsByTagName('lesson')
     for lesson_node in lessons:
@@ -113,6 +119,10 @@ def untis_import_xml(request: HttpRequest, untis_xml: Union[BinaryIO, str]) -> N
         date_end = date(int(effectiveenddate[:4]), int(effectiveenddate[4:6]), int(
             effectiveenddate[6:])) if effectiveenddate else None
 
+        # Coerce effective start date to not be before tomorrow
+        if date_start and date_start <= today:
+            date_start = today + timedelta(days=1)
+
         try:
             groups = [Group.objects.get(short_name=v)
                       for v in group_short_names]
-- 
GitLab