diff --git a/aleksis/apps/untis/settings.py b/aleksis/apps/untis/settings.py index 4806f1c28e751a29109452006b7e6f6f828d4ed5..cbe02a622a62fb7dbccbf4cefe26c3aac1b260bb 100644 --- a/aleksis/apps/untis/settings.py +++ b/aleksis/apps/untis/settings.py @@ -1,4 +1,5 @@ from aleksis.core.util.core_helpers import lazy_config +from django.utils.translation import gettext_lazy as _ DATABASES = { 'untis': { @@ -12,13 +13,25 @@ DATABASES = { } CONSTANCE_CONFIG = { - "UNTIS_NAME": ("untis", _("Name of database"), "char_field"), - "UNTIS_USER": ("aleksis", _("Database user"), "char_field"), - "UNTIS_PASSWORD": ("aleksis", _("Database password"), "char_field"), - "UNTIS_HOST": ("127.0.0.1", _("Database host"), "char_field"), - "UNTIS_PORT": ("3306", _("Database port"), "char_field"), + "UNTIS_DB_NAME": ("untis", _("Name of database"), "char_field"), + "UNTIS_DB_USER": ("aleksis", _("Database user"), "char_field"), + "UNTIS_DB_PASSWORD": ("aleksis", _("Database password"), "char_field"), + "UNTIS_DB_HOST": ("127.0.0.1", _("Database host"), "char_field"), + "UNTIS_DB_PORT": ("3306", _("Database port"), "char_field"), + "UNTIS_IMPORT_MYSQL_UPDATE_SUBJECTS": ( + True, + _("Update values of existing subjects?"), + bool, + ), } CONSTANCE_CONFIG_FIELDSETS = { - "Untis Database Settings": ("UNTIS_NAME", "UNTIS_USER", "UNTIS_PASSWORD", "UNTIS_HOST", "UNTIS_PORT"), + "UNTIS import via MySQL: Database Settings": ( + "UNTIS_DB_NAME", + "UNTIS_DB_USER", + "UNTIS_DB_PASSWORD", + "UNTIS_DB_HOST", + "UNTIS_DB_PORT", + ), + "UNTIS import via MySQL: Common Settings": ("UNTIS_IMPORT_MYSQL_UPDATE_SUBJECTS",), } diff --git a/aleksis/apps/untis/util/mysql/importers/common_data.py b/aleksis/apps/untis/util/mysql/importers/common_data.py index 8c3f8786007e4e1747b8557a6da1ea4960e11350..98dbb7d15fe03212a09fe72d7801cd8f03703155 100644 --- a/aleksis/apps/untis/util/mysql/importers/common_data.py +++ b/aleksis/apps/untis/util/mysql/importers/common_data.py @@ -1,35 +1,70 @@ +import logging from datetime import time from typing import List, Dict +from constance import config + from aleksis.apps.chronos import models as chronos_models from aleksis.core import models as core_models from .... import models as mysql_models from ..util import run_default_filter, untis_colour_to_hex, untis_split_first +logger = logging.getLogger(__name__) + def import_subjects() -> Dict[int, chronos_models.Subject]: """ Import subjects """ subjects_ref = {} + + # Get subjects subjects = run_default_filter(mysql_models.Subjects.objects, filter_term=False) + for subject in subjects: + # Check if needed data are provided if not subject.name: - raise Exception("Short name needed.") + logger.error( + "Subject ID {}: Cannot import subject without short name.".format( + subject.subject_id + ) + ) + continue + # Build values short_name = subject.name[:10] - name = subject.longname if subject.longname else short_name + name = subject.longname[:30] if subject.longname else short_name + colour_fg = untis_colour_to_hex(subject.forecolor) + colour_bg = untis_colour_to_hex(subject.backcolor) + import_ref = subject.subject_id + # Get or create subject object by short name new_subject, created = chronos_models.Subject.objects.get_or_create( - abbrev=short_name, defaults={"name": name} + abbrev=short_name, + defaults={ + "name": name, + "colour_fg": colour_fg, + "colour_bg": colour_bg, + "import_ref_untis": import_ref, + }, ) - new_subject.name = name - new_subject.colour_fg = untis_colour_to_hex(subject.forecolor) - new_subject.colour_bg = untis_colour_to_hex(subject.backcolor) - new_subject.save() - - subjects_ref[subject.subject_id] = new_subject + # Force sync + if config.UNTIS_IMPORT_MYSQL_UPDATE_SUBJECTS and ( + new_subject.name != name + or new_subject.colour_fg != colour_fg + or new_subject.colour_bg != colour_bg + or new_subject.import_ref_untis != import_ref + ): + new_subject.name = name + new_subject.colour_fg = untis_colour_to_hex(subject.forecolor) + new_subject.colour_bg = untis_colour_to_hex(subject.backcolor) + new_subject.import_ref_untis = import_ref + new_subject.save() + + logger.info("Successfully imported subject {} ({})".format(short_name, "created" if created else "updated")) + + subjects_ref[import_ref] = new_subject return subjects_ref