diff --git a/CHANGELOG.rst b/CHANGELOG.rst index ffffe52b1c73a52faeaafe3776c202e5ab66a3ab..c8810d42ead4882470d601a31f5243440bdef008 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -14,6 +14,7 @@ Fixed * Importer failed sometimes on progressing absences. * Exam import failed sometimes when data provided through Untis were incomplete. +* Importer now automatically fixes intersections of terms with previous terms. `2.3.2`_ - 2022-09-01 --------------------- diff --git a/aleksis/apps/untis/util/mysql/importers/terms.py b/aleksis/apps/untis/util/mysql/importers/terms.py index 179cfc58f3cd2ac225c3cba58e2831a50aa223e5..c2cefb9d7daef1b8b0e22b23774abdf3f1c03e47 100644 --- a/aleksis/apps/untis/util/mysql/importers/terms.py +++ b/aleksis/apps/untis/util/mysql/importers/terms.py @@ -1,5 +1,5 @@ import logging -from datetime import date +from datetime import date, timedelta from typing import Dict, Optional from django.db.models import Max, OuterRef, Q, QuerySet, Subquery @@ -71,7 +71,7 @@ def import_terms( if school_id is None: school_id = get_site_preferences()["untis_mysql__school_id"] - qs = qs.filter(school_id=school_id) + qs = qs.filter(school_id=school_id).order_by("datefrom") if version is None: # Select newest version per term / validity range @@ -142,6 +142,7 @@ def import_terms( school_term.date_start = date_start school_term.save() + school_terms[school_year_id] = school_term try: validity_range = chronos_models.ValidityRange.objects.get( @@ -151,13 +152,24 @@ def import_terms( except chronos_models.ValidityRange.DoesNotExist: try: validity_range = chronos_models.ValidityRange.objects.within_dates( - date_start, date_end + date_start__gte=date_start, date_end__lte=date_end ).get() logger.info(" Validity range found by time.") except chronos_models.ValidityRange.DoesNotExist: validity_range = chronos_models.ValidityRange() logger.info(" Validity range created newly.") + # In Untis, you can set all the end dates of the terms to the same date + # and despite that, the terms still end if a new one starts. + # If this case occurs, we have to set the end date of the previous term + # to the start date of the next one. + validity_range_with_possible_intersection = chronos_models.ValidityRange.objects.filter( + date_end__gte=date_start, date_start__lt=date_start + ).first() + if validity_range_with_possible_intersection: + validity_range_with_possible_intersection.date_end = date_start - timedelta(days=1) + validity_range_with_possible_intersection.save() + validity_range.import_ref_untis = term_id validity_range.date_start = date_start validity_range.date_end = date_end