Skip to content
Snippets Groups Projects
Commit c6d9d401 authored by Nik | Klampfradler's avatar Nik | Klampfradler
Browse files

Merge branch '56-use-teachers-only-as-fallback-for-course-group-matching' into 'master'

Resolve "Use teachers only as fallback for course group matching"

Closes #56

See merge request AlekSIS/official/AlekSIS-App-Untis!154
parents 6e377815 2a5e04ba
No related branches found
No related tags found
No related merge requests found
......@@ -17,6 +17,7 @@ Fixed
* Import failed sometimes if there were lessons with names.
* Import failed if there was a lesson without a teacher.
* Course group matching didn't work correctly with teachers.
`2.3`_ - 2022-06-25
-------------------
......
......@@ -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
......@@ -119,63 +119,90 @@ def import_lessons(
c = classes_ref[class_id]
course_classes.append(c)
logger.debug(
f" Teacher: {teacher} #{teacher_id}; Subject: {subject} #{subject_id}; "
f"Classes: {course_classes} #{class_ids}"
)
course_group_not_found_and_not_created = False
if get_site_preferences()["untis_mysql__use_course_groups"]:
# Negative import_ref denotes a course group
group_import_ref = -int("{}{}".format(lesson_id, i))
# 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,
owners__in=[t.id for t in teachers],
).filter(Q(school_term__isnull=True) | Q(school_term=validity_range.school_term))
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()
)
if not qs.exists():
logger.warning(" No matching course group found")
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()) 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"
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 len(possible_groups) == 1:
course_group = possible_groups[0]
logger.info(
" 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 by searching by parent groups, "
" 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:
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, "
f"teachers (owners) and subject (fuzzy matching mode): {course_group}"
)
changed = False
register_data_check = get_site_preferences()[
"untis_mysql__data_check_for_not_found_course_groups"
]
if not course_group and get_site_preferences()["untis_mysql__create_course_groups"]:
# No matching group found
logger.info(" No matching course group found, generate one")
# Build names and refs for course groups
group_short_name = "{}-{}".format(
......@@ -195,10 +222,11 @@ def import_lessons(
# Log
if created:
logger.info(" Course group created")
else:
logger.info(" Existing course group found")
# Update parent groups
course_group.parent_groups.set(course_classes)
logger.info(" Parent groups set")
# Update name
if course_group.name != group_name:
......@@ -330,4 +358,3 @@ def import_lessons(
with reversion.create_revision():
set_comment(_("Deleted by Untis import"))
lesson.save()
lesson.delete()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment