From b4dd1ad54e4a8e964d89df716a5a5855c3123533 Mon Sep 17 00:00:00 2001 From: Jonathan Weth <git@jonathanweth.de> Date: Tue, 23 Aug 2022 17:34:40 +0200 Subject: [PATCH] Use teachers only as fallback match for course groups --- .../untis/util/mysql/importers/absences.py | 1 + .../untis/util/mysql/importers/lessons.py | 47 ++++++++++--------- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/aleksis/apps/untis/util/mysql/importers/absences.py b/aleksis/apps/untis/util/mysql/importers/absences.py index 9d5e4e7..48ad395 100644 --- a/aleksis/apps/untis/util/mysql/importers/absences.py +++ b/aleksis/apps/untis/util/mysql/importers/absences.py @@ -183,5 +183,6 @@ def import_absences( if a.import_ref_untis and a.import_ref_untis not in existing_absences: logger.info("Absence {} deleted".format(a.id)) a.delete() + LessonSubstitution.objects.filter(absence_ref_untis=a.import_ref_untis).delete() return ref, created_substitutions diff --git a/aleksis/apps/untis/util/mysql/importers/lessons.py b/aleksis/apps/untis/util/mysql/importers/lessons.py index 7f73ef7..14420f4 100644 --- a/aleksis/apps/untis/util/mysql/importers/lessons.py +++ b/aleksis/apps/untis/util/mysql/importers/lessons.py @@ -128,46 +128,49 @@ def import_lessons( qs = core_models.Group.objects.filter( parent_groups__in=[c.id for c in course_classes], subject_id=subject.id, - owners__in=[t.id for t in teachers], ).filter(Q(school_term__isnull=True) | Q(school_term=validity_range.school_term)) + if not qs.exists(): + logger.warning(" Not matching course group not found") + # Check if found groups match + possible_groups = [] course_group = None for found_group in qs: if compare_m2m(course_classes, found_group.parent_groups.all()) and compare_m2m( teachers, found_group.owners.all() ): - course_group = found_group - logger.info( - " Course group found by searching by parent groups, " - "teachers (owners) and subject" - ) + possible_groups.append(found_group) + + if len(possible_groups) == 1: + course_group = possible_groups[0] + logger.info(" Course group found by searching by parent groups, and subject") + else: + for found_group in possible_groups: + if compare_m2m(teachers, found_group.owners.all()): + course_group = found_group + logger.info( + " Course group found by searching by parent groups, " + "teachers (owners) and subject" + ) if ( not course_group and get_site_preferences()["untis_mysql__course_groups_fuzzy_matching"] ): + qs = qs.filter(owners__in=[t.id for t in teachers]) if qs.count() != 1: logger.warning( - " Course group not found by searching by parent groups, " + " Course group not found or more than one found " + "by searching by parent groups, " "teachers (owners) and subject (fuzzy matching mode)" ) else: - for found_group in qs: - if compare_m2m(teachers, found_group.owners.all()): - if course_group: - logger.warning( - " More than one course group found " - "by searching by parent groups, " - "teachers (owners) and subject (fuzzy matching mode)" - ) - course_group = None - else: - course_group = found_group - logger.info( - " Course group found by searching by parent groups, " - "teachers (owners) and subject (fuzzy matching mode)" - ) + course_group = qs.first() + logger.info( + " Course group found by searching by parent groups, " + "teachers (owners) and subject (fuzzy matching mode)" + ) changed = False register_data_check = get_site_preferences()[ -- GitLab