diff --git a/aleksis/apps/untis/model_extensions.py b/aleksis/apps/untis/model_extensions.py index ca4366eb7d25d234612b41ca2224e5ddbb09559f..270b7738337ce00aff84c419290b16e1c39cb772 100644 --- a/aleksis/apps/untis/model_extensions.py +++ b/aleksis/apps/untis/model_extensions.py @@ -1,6 +1,6 @@ from django.utils.translation import gettext as _ -from jsonstore import IntegerField +from jsonstore import IntegerField, CharField from aleksis.apps.chronos import models as chronos_models from aleksis.core import models as core_models @@ -12,7 +12,7 @@ core_models.Person.field( import_ref_untis=IntegerField(verbose_name=_("Untis import reference"), null=True, blank=True) ) core_models.Group.field( - import_ref_untis=IntegerField(verbose_name=_("Untis import reference"), null=True, blank=True) + import_ref_untis=CharField(verbose_name=_("Untis import reference"), null=True, blank=True) ) # Chronos models diff --git a/aleksis/apps/untis/util/mysql/importers/lessons.py b/aleksis/apps/untis/util/mysql/importers/lessons.py index 4a50d6592090601eba35d281d67f87733f808f77..5f21385a63ed00901f9a20162c3574dfc38b1c62 100644 --- a/aleksis/apps/untis/util/mysql/importers/lessons.py +++ b/aleksis/apps/untis/util/mysql/importers/lessons.py @@ -94,6 +94,7 @@ def import_lessons( teacher_id = int(el[0]) if el[0] else 0 subject_id = int(el[2]) class_ids = untis_split_third(el[17], conv=int) + student_group_id = int(el[19]) if el[19] else 0 # Get teacher if teacher_id != 0: @@ -113,6 +114,16 @@ def import_lessons( if current_teacher_id and current_teacher_id != teacher_id: use_room_idx += 1 + # Get student group + student_group = None + if student_group_id != 0: + student_group = run_default_filter(validity_range, mysql_models.Studentgroup.objects, + filter_term=False).get(studentgroup_id=student_group_id) + if not student_group.name: + student_group = None + else: + class_ids = untis_split_third(student_group.classids, conv=int) + # Get classes course_classes = [] for class_id in class_ids: @@ -128,73 +139,94 @@ def import_lessons( if get_site_preferences()["untis_mysql__use_course_groups"]: # Negative import_ref denotes a course group group_import_ref = -int("{}{}".format(lesson_id, i)) + group_short_name = "{}-{}".format( + "".join([c.short_name for c in course_classes]), + subject.short_name, + ) + group_name = "{}: {}".format( + ", ".join([c.short_name for c in course_classes]), + subject.short_name, + ) - # Search by parent groups, teachers/owners and subject - qs = ( - core_models.Group.objects.filter( - parent_groups__in=[c.id for c in course_classes], - subject_id=subject.id, + if student_group: + # Search by student group + group_import_ref = f"sg_{student_group_id}" + group_short_name = student_group.name + group_name = student_group.longname if student_group.longname else group_short_name + qs = core_models.Group.objects.filter(Q(import_ref_untis=group_import_ref) | Q(short_name=group_short_name) | Q(name=group_name)).filter(school_term=validity_range.school_term) + else: + # Search by parent groups, teachers/owners and subject + qs = ( + core_models.Group.objects.filter( + parent_groups__in=[c.id for c in course_classes], + subject_id=subject.id, + ) + .filter(Q(school_term__isnull=True) | Q(school_term=validity_range.school_term)) + .distinct() ) - .filter(Q(school_term__isnull=True) | Q(school_term=validity_range.school_term)) - .distinct() - ) + + course_group = None if not qs.exists(): logger.warning(" No matching course group found") + elif student_group and qs.count() == 1: + logger.debug("Found exact match with student group") + course_group = qs.first() else: logger.debug(f"{qs.count()} possibly matching course groups found: {qs}") - # 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()): - possible_groups.append(found_group) - logger.debug( - f" {len(possible_groups)} possible groups found " - f"by searching by parent groups: {possible_groups}" - ) + if not course_group: + # Check if found groups match + possible_groups = [] + for found_group in qs: + if compare_m2m(course_classes, found_group.parent_groups.all()): + possible_groups.append(found_group) - if len(possible_groups) == 1: - course_group = possible_groups[0] - logger.info( - " Course group found by searching " - f"by parent groups, and subject: {course_group}" + logger.debug( + f" {len(possible_groups)} possible groups found " + f"by searching by parent groups: {possible_groups}" ) - 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: - logger.debug( - " No 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"] - ): - logger.debug(" Fuzzy matching mode used") - qs = qs.filter(owners__in=[t.id for t in teachers]) - if qs.count() != 1: - logger.warning( - " Course group not found or more than one found " - f"({qs.count()} groups found: {qs}) by searching by parent groups, " - "teachers (owners) and subject (fuzzy matching mode)" - ) - else: - course_group = qs.first() + if len(possible_groups) == 1: + course_group = possible_groups[0] logger.info( - " Course group found by searching by parent groups, " - f"teachers (owners) and subject (fuzzy matching mode): {course_group}" + " Course group found by searching " + f"by parent groups, and subject: {course_group}" ) + 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: + logger.debug( + " No 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"] + ): + logger.debug(" Fuzzy matching mode used") + qs = qs.filter(owners__in=[t.id for t in teachers]) + if qs.count() != 1: + logger.warning( + " Course group not found or more than one found " + f"({qs.count()} groups found: {qs}) by searching by parent groups, " + "teachers (owners) and subject (fuzzy matching mode)" + ) + else: + course_group = qs.first() + logger.info( + " Course group found by searching by parent groups, " + f"teachers (owners) and subject (fuzzy matching mode): {course_group}" + ) changed = False register_data_check = get_site_preferences()[ @@ -204,19 +236,9 @@ def import_lessons( # No matching group found logger.info(" No matching course group found, generate one") - # Build names and refs for course groups - group_short_name = "{}-{}".format( - "".join([c.short_name for c in course_classes]), - subject.short_name, - ) - group_name = "{}: {}".format( - ", ".join([c.short_name for c in course_classes]), - subject.short_name, - ) - # Get or create course group course_group, created = core_models.Group.objects.get_or_create( - short_name=group_short_name, defaults={"name": group_name} + school_term=validity_range.school_term, short_name=group_short_name, defaults={"name": group_name} ) # Log @@ -246,6 +268,9 @@ def import_lessons( # Update owners course_group.owners.set(teachers) + # Update parent groups + course_group.parent_groups.add(*course_classes) + # Update import ref if course_group.import_ref_untis != group_import_ref: course_group.import_ref_untis = group_import_ref