diff --git a/aleksis/apps/untis/settings.py b/aleksis/apps/untis/settings.py index da7c74d822bdbab7b219cdb833be8357ee3753a5..5590c7dcddac0469b80258e9f357eb46cb320af2 100644 --- a/aleksis/apps/untis/settings.py +++ b/aleksis/apps/untis/settings.py @@ -53,6 +53,10 @@ CONSTANCE_CONFIG = { _("Update name of existing rooms?"), bool, ), + "UNTIS_IMPORT_MYSQL_UPDATE_SUPERVISION_AREAS": ( + True, + _("Update values of existing supervision areas?") + ) } CONSTANCE_CONFIG_FIELDSETS = { @@ -71,5 +75,6 @@ CONSTANCE_CONFIG_FIELDSETS = { "UNTIS_IMPORT_MYSQL_UPDATE_GROUPS_NAME", "UNTIS_IMPORT_MYSQL_UPDATE_GROUPS_OVERWRITE_OWNERS", "UNTIS_IMPORT_MYSQL_UPDATE_ROOMS_NAME", + "UNTIS_IMPORT_MYSQL_UPDATE_SUPERVISION_AREAS", ), } diff --git a/aleksis/apps/untis/util/mysql/importers/common_data.py b/aleksis/apps/untis/util/mysql/importers/common_data.py index c6a3ac89103639b006c7d796dbe6608f142c4848..f10afccf27bd12d724de9621a8ce69a5a06fb666 100644 --- a/aleksis/apps/untis/util/mysql/importers/common_data.py +++ b/aleksis/apps/untis/util/mysql/importers/common_data.py @@ -274,32 +274,63 @@ def import_rooms() -> Dict[int, chronos_models.Room]: def import_supervision_areas() -> Dict[int, chronos_models.SupervisionArea]: """ Import supervision areas """ - supervision_areas_ref = {} + ref = {} + + # Get supervision areas areas = run_default_filter(mysql_models.Corridor.objects, filter_term=False) + for area in areas: if not area.name: - raise Exception("Short name needed.") + logger.error( + "Supervision area ID {}: Cannot import supervision area without short name.".format( + area.corridor_id + ) + ) + continue short_name = area.name[:10] name = area.longname[:50] if area.longname else short_name colour_fg = untis_colour_to_hex(area.forecolor) colour_bg = untis_colour_to_hex(area.backcolor) + import_ref = area.corridor_id + + logger.info("Import supervision area {} …".format(short_name)) new_area, created = chronos_models.SupervisionArea.objects.get_or_create( short_name=short_name, - defaults={"name": name, "colour_fg": colour_fg, "colour_bg": colour_bg}, + defaults={"name": name, "colour_fg": colour_fg, "colour_bg": colour_bg, "import_ref_untis": import_ref}, ) - new_area.name = name - new_area.colour_fg = colour_fg - new_area.colour_bg = colour_bg - new_area.save() + if created: + logger.info(" New supervision area created") + + changed = False + + if config.UNTIS_IMPORT_MYSQL_UPDATE_SUPERVISION_AREAS and ( + new_area.name != new_area.name or + new_area.colour_fg != colour_fg or + new_area.colour_bg != colour_bg + ): + new_area.name = name + new_area.colour_fg = colour_fg + new_area.colour_bg = colour_bg + changed = True + + logger.info(" Name, foreground and background colour updated") + + if new_area.import_ref_untis != import_ref: + new_area.import_ref_untis = import_ref + changed = True + logger.info(" Import reference updated") + + if changed: + new_area.save() # TODO: Supervisions - supervision_areas_ref[area.corridor_id] = new_area + ref[import_ref] = new_area - return supervision_areas_ref + return ref def import_time_periods_and_breaks() -> List[List[chronos_models.TimePeriod]]: