Skip to content
Snippets Groups Projects
Verified Commit 30622bea authored by Jonathan Weth's avatar Jonathan Weth :keyboard:
Browse files

Improve import functions for subjects and teachers

- Add configuration options
parent 3352c20b
No related branches found
No related tags found
1 merge request!13Resolve "Support import from MySQL"
...@@ -23,6 +23,16 @@ CONSTANCE_CONFIG = { ...@@ -23,6 +23,16 @@ CONSTANCE_CONFIG = {
_("Update values of existing subjects?"), _("Update values of existing subjects?"),
bool, bool,
), ),
"UNTIS_IMPORT_MYSQL_UPDATE_PERSONS_SHORT_NAME": (
False,
_("Update short name of existing persons?"),
bool,
),
"UNTIS_IMPORT_MYSQL_UPDATE_PERSONS_NAME": (
False,
_("Update first and last name of existing persons?"),
bool,
),
} }
CONSTANCE_CONFIG_FIELDSETS = { CONSTANCE_CONFIG_FIELDSETS = {
...@@ -33,5 +43,9 @@ CONSTANCE_CONFIG_FIELDSETS = { ...@@ -33,5 +43,9 @@ CONSTANCE_CONFIG_FIELDSETS = {
"UNTIS_DB_HOST", "UNTIS_DB_HOST",
"UNTIS_DB_PORT", "UNTIS_DB_PORT",
), ),
"UNTIS import via MySQL: Common Settings": ("UNTIS_IMPORT_MYSQL_UPDATE_SUBJECTS",), "UNTIS import via MySQL: Common Settings": (
"UNTIS_IMPORT_MYSQL_UPDATE_SUBJECTS",
"UNTIS_IMPORT_MYSQL_UPDATE_PERSONS_SHORT_NAME",
"UNTIS_IMPORT_MYSQL_UPDATE_PERSONS_NAME",
),
} }
...@@ -33,11 +33,13 @@ def import_subjects() -> Dict[int, chronos_models.Subject]: ...@@ -33,11 +33,13 @@ def import_subjects() -> Dict[int, chronos_models.Subject]:
# Build values # Build values
short_name = subject.name[:10] short_name = subject.name[:10]
name = subject.longname[:30] if subject.longname else short_name name = subject.longname if subject.longname else short_name
colour_fg = untis_colour_to_hex(subject.forecolor) colour_fg = untis_colour_to_hex(subject.forecolor)
colour_bg = untis_colour_to_hex(subject.backcolor) colour_bg = untis_colour_to_hex(subject.backcolor)
import_ref = subject.subject_id import_ref = subject.subject_id
logger.info("Import subject {} …".format(short_name))
# Get or create subject object by short name # Get or create subject object by short name
new_subject, created = chronos_models.Subject.objects.get_or_create( new_subject, created = chronos_models.Subject.objects.get_or_create(
abbrev=short_name, abbrev=short_name,
...@@ -49,20 +51,31 @@ def import_subjects() -> Dict[int, chronos_models.Subject]: ...@@ -49,20 +51,31 @@ def import_subjects() -> Dict[int, chronos_models.Subject]:
}, },
) )
if created:
logger.info(" New subject created")
# Force sync # Force sync
changed = False
if config.UNTIS_IMPORT_MYSQL_UPDATE_SUBJECTS and ( if config.UNTIS_IMPORT_MYSQL_UPDATE_SUBJECTS and (
new_subject.name != name new_subject.name != name
or new_subject.colour_fg != colour_fg or new_subject.colour_fg != colour_fg
or new_subject.colour_bg != colour_bg or new_subject.colour_bg != colour_bg
or new_subject.import_ref_untis != import_ref
): ):
new_subject.name = name new_subject.name = name
new_subject.colour_fg = untis_colour_to_hex(subject.forecolor) new_subject.colour_fg = untis_colour_to_hex(subject.forecolor)
new_subject.colour_bg = untis_colour_to_hex(subject.backcolor) new_subject.colour_bg = untis_colour_to_hex(subject.backcolor)
changed = True
logger.info(" Name, foreground and background colour updated")
if new_subject.import_ref_untis != import_ref:
new_subject.import_ref_untis = import_ref new_subject.import_ref_untis = import_ref
new_subject.save() changed = True
logger.info("Successfully imported subject {} ({})".format(short_name, "created" if created else "updated")) logger.info(" Import reference updated")
if changed:
new_subject.save()
subjects_ref[import_ref] = new_subject subjects_ref[import_ref] = new_subject
...@@ -73,26 +86,62 @@ def import_teachers() -> Dict[int, core_models.Person]: ...@@ -73,26 +86,62 @@ def import_teachers() -> Dict[int, core_models.Person]:
""" Import teachers """ """ Import teachers """
teachers_ref = {} teachers_ref = {}
# Get teachers
teachers = run_default_filter(mysql_models.Teacher.objects) teachers = run_default_filter(mysql_models.Teacher.objects)
for teacher in teachers: for teacher in teachers:
# Check if needed data are provided
if not teacher.name: if not teacher.name:
raise Exception("Short name needed.") logger.error(
"Teacher ID {}: Cannot import teacher without short name.".format(
teacher.teacher_id
)
)
continue
short_name = teacher.name[:5] # Build values
short_name = teacher.name
first_name = teacher.firstname if teacher.firstname else "?" first_name = teacher.firstname if teacher.firstname else "?"
last_name = teacher.longname if teacher.longname else teacher.name last_name = teacher.longname if teacher.longname else teacher.name
import_ref = teacher.teacher_id
logger.info("Import teacher {} (as person) …".format(short_name))
new_teacher, created = core_models.Person.objects.get_or_create( new_teacher, created = core_models.Person.objects.get_or_create(
short_name__iexact=short_name, short_name__iexact=short_name,
defaults={ defaults={
"first_name": first_name, "first_name": first_name,
"last_name": last_name, "last_name": last_name,
"import_ref": teacher.teacher_id, "import_ref_untis": import_ref,
}, },
) )
new_teacher.short_name = short_name if created:
new_teacher.save() logger.info(" New person created")
changed = False
if config.UNTIS_IMPORT_MYSQL_UPDATE_PERSONS_NAME and (
new_teacher.first_name != first_name or
new_teacher.last_name != last_name
):
new_teacher.first_name = first_name
new_teacher.last_name = last_name
changed = True
logger.info(" First and last name updated")
if config.UNTIS_IMPORT_MYSQL_UPDATE_PERSONS_SHORT_NAME and new_teacher.short_name != short_name:
new_teacher.short_name = short_name
changed = True
logger.info(" Short name updated")
if new_teacher.import_ref_untis != import_ref:
new_teacher.import_ref_untis = import_ref
changed = True
logger.info(" Import reference updated")
if changed:
new_teacher.save()
teachers_ref[teacher.teacher_id] = new_teacher teachers_ref[teacher.teacher_id] = new_teacher
......
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