From b2614ea46008da9315fc12807546a0ed5fec2a51 Mon Sep 17 00:00:00 2001
From: Jonathan Weth <git@jonathanweth.de>
Date: Tue, 7 Apr 2020 15:17:06 +0200
Subject: [PATCH] Fix import of lessons: Check if there are lessons from other
 terms ending in this term

---
 .../apps/untis/util/mysql/importers/lessons.py  | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/aleksis/apps/untis/util/mysql/importers/lessons.py b/aleksis/apps/untis/util/mysql/importers/lessons.py
index 2c65450..5418c01 100644
--- a/aleksis/apps/untis/util/mysql/importers/lessons.py
+++ b/aleksis/apps/untis/util/mysql/importers/lessons.py
@@ -1,4 +1,5 @@
 import logging
+from datetime import timedelta
 
 from constance import config
 from django.utils.translation import gettext as _
@@ -28,6 +29,18 @@ def import_lessons(
 
     # Get current term
     term = get_term()
+    date_start = untis_date_to_date(term.datefrom)
+    date_end = untis_date_to_date(term.dateto)
+
+    # Get all existing lessons for this term
+    lessons_in_term = chronos_models.Lesson.objects.filter(
+        term_untis=term.term_id
+    ).values_list("id", flat=True)
+
+    # Set the end date of all lessons from other terms ending in this term to the day before this term starts
+    chronos_models.Lesson.objects.filter(date_end__gte=date_start).exclude(
+        id__in=lessons_in_term
+    ).update(date_end=date_start - timedelta(days=1))
 
     # Lessons
     lessons = run_default_filter(mysql_models.Lesson.objects)
@@ -178,10 +191,6 @@ def import_lessons(
             else:
                 groups = course_classes
 
-            # Create new lesson
-            date_start = untis_date_to_date(term.datefrom)
-            date_end = untis_date_to_date(term.dateto)
-
             # Get old lesson
             old_lesson_qs = chronos_models.Lesson.objects.filter(
                 lesson_id_untis=lesson_id, element_id_untis=i, term_untis=term.term_id
-- 
GitLab