diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 154ec26849484a5d0bd8919c76c66e73377ca198..e7fb0a3332e44f75f730d057e3bbd508789c1c52 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -9,6 +9,11 @@ and this project adheres to `Semantic Versioning`_. Unreleased ---------- +Added +~~~~~ + +* Support for configuring the Untis school ID + Fixed ~~~~~ diff --git a/aleksis/apps/untis/commands.py b/aleksis/apps/untis/commands.py index 9f92f103cf033fb26dca4e9f6886f68e4d1f9595..479008ecea2c762f1697f6dff019ed6463d290f2 100644 --- a/aleksis/apps/untis/commands.py +++ b/aleksis/apps/untis/commands.py @@ -30,7 +30,12 @@ class ImportCommand: return None @classmethod - def run(cls, background: bool = False, version: Optional[int] = None): + def run( + cls, + background: bool = False, + school_id: Optional[int] = None, + version: Optional[int] = None, + ): """Run the import command (foreground/background).""" if background: from .tasks import TASKS @@ -38,7 +43,7 @@ class ImportCommand: task = TASKS[cls.task_name] task.delay(version=version) else: - _untis_import_mysql(cls.get_terms(), version=version) + _untis_import_mysql(cls.get_terms(), school_id=school_id, version=version) class CurrentImportCommand(ImportCommand): diff --git a/aleksis/apps/untis/management/commands/untis_import_mysql.py b/aleksis/apps/untis/management/commands/untis_import_mysql.py index e471b908f3665ba196c826b0c90f83a690279787..e4abbf5de78caf70e333237b5149588a4e62d6dd 100644 --- a/aleksis/apps/untis/management/commands/untis_import_mysql.py +++ b/aleksis/apps/untis/management/commands/untis_import_mysql.py @@ -17,9 +17,14 @@ class Command(BaseCommand): "--plan-version", help="Select explicit Untis plan version", ) + parser.add_argument( + "--school-id", + help="Select explicit Untis school ID", + ) def handle(self, *args, **options): command = COMMANDS_BY_NAME[options["command"]] background = options["background"] + school_id = options.get("school_id", None) version = options.get("plan_version", None) command.run(background=background, version=version) diff --git a/aleksis/apps/untis/migrations/0003_guess_school_id.py b/aleksis/apps/untis/migrations/0003_guess_school_id.py new file mode 100644 index 0000000000000000000000000000000000000000..79a2d2743d4e0dbfd6c1e496bf10ec72fa6284c3 --- /dev/null +++ b/aleksis/apps/untis/migrations/0003_guess_school_id.py @@ -0,0 +1,29 @@ +from django.db import migrations + +from aleksis.core.util.core_helpers import get_site_preferences + + +def guess_school_id(apps, schema_editor): + db_alias = schema_editor.connection.alias + + from aleksis.apps.chronos.models import ValidityRange + + try: + vr = ValidityRange.objects.using(db_alias).first() + except ValidityRange.DoesNotExist: + return + + school_id = vr.school_id_untis + if school_id: + get_site_preferences()["untis_mysql__school_id"] = school_id + + +class Migration(migrations.Migration): + + dependencies = [ + ('untis', '0002_auto_20200820_1542'), + ] + + operations = [ + migrations.RunPython(guess_school_id), + ] diff --git a/aleksis/apps/untis/preferences.py b/aleksis/apps/untis/preferences.py index d95f5bc30cb306d3bca40c0db60f682a26a86143..f61b4bb3fbded2b0a0ee61b0ba6bc92e85ec3359 100644 --- a/aleksis/apps/untis/preferences.py +++ b/aleksis/apps/untis/preferences.py @@ -1,13 +1,21 @@ from django.utils.translation import gettext_lazy as _ from dynamic_preferences.preferences import Section -from dynamic_preferences.types import BooleanPreference +from dynamic_preferences.types import BooleanPreference, IntegerPreference from aleksis.core.registries import site_preferences_registry untis_mysql = Section("untis_mysql", verbose_name=_("Untis: MySQL")) +@site_preferences_registry.register +class SchoolID(IntegerPreference): + section = untis_mysql + name = "school_id" + default = 0 + verbose_name = _("School ID in Untis database") + + @site_preferences_registry.register class UpdateSubjects(BooleanPreference): section = untis_mysql diff --git a/aleksis/apps/untis/util/mysql/importers/terms.py b/aleksis/apps/untis/util/mysql/importers/terms.py index 22e095477fd78f46bd8eb15be1d610cecbdd3397..179cfc58f3cd2ac225c3cba58e2831a50aa223e5 100644 --- a/aleksis/apps/untis/util/mysql/importers/terms.py +++ b/aleksis/apps/untis/util/mysql/importers/terms.py @@ -15,6 +15,7 @@ from aleksis.apps.untis.util.mysql.util import ( untis_date_to_date, ) from aleksis.core import models as core_models +from aleksis.core.util.core_helpers import get_site_preferences from .... import models as mysql_models @@ -59,6 +60,7 @@ logger = logging.getLogger(__name__) def import_terms( qs: Optional[QuerySet] = None, + school_id: Optional[int] = None, version: Optional[int] = None, ) -> Dict[int, chronos_models.ValidityRange]: """Import terms and school years as validity ranges and school terms.""" @@ -67,6 +69,10 @@ def import_terms( if not isinstance(qs, QuerySet): qs = run_using(mysql_models.Terms.objects) + if school_id is None: + school_id = get_site_preferences()["untis_mysql__school_id"] + qs = qs.filter(school_id=school_id) + if version is None: # Select newest version per term / validity range sub_qs = ( diff --git a/aleksis/apps/untis/util/mysql/main.py b/aleksis/apps/untis/util/mysql/main.py index 7ae5113f890f93a2314ea6cf84924c5d479c0958..5feb449c2195e7ec78b3d482cc52dc197f6c9bab 100644 --- a/aleksis/apps/untis/util/mysql/main.py +++ b/aleksis/apps/untis/util/mysql/main.py @@ -26,9 +26,11 @@ from .importers.lessons import import_lessons from .importers.substitutions import import_substitutions -def untis_import_mysql(terms: Optional[QuerySet] = None, version: Optional[int] = None): +def untis_import_mysql( + terms: Optional[QuerySet] = None, school_id: Optional[int] = None, version: Optional[int] = None +): # School terms and validity ranges - validity_ref = import_terms(terms, version=version) + validity_ref = import_terms(terms, school_id=school_id, version=version) for validity_range in tqdm( validity_ref.values(), desc="Import data for terms", **TQDM_DEFAULTS diff --git a/docs/admin/20_configuration.rst b/docs/admin/20_configuration.rst index 065dbf319fad22935f44ce1218d645ae9e2d7496..edf58c58308b2d397faee6a040c944b24ef40d5b 100644 --- a/docs/admin/20_configuration.rst +++ b/docs/admin/20_configuration.rst @@ -45,11 +45,29 @@ you have to set the following settings: host = "mysqlserver" port = 3306 +Preferences +----------- + +The preferences for the import can be set from the menu under +`Admin → Configuration → Untis`. + +Configure the school ID +~~~~~~~~~~~~~~~~~~~~~~~ + +The only required preference is the Untis school ID. You need to +provide this in all cases, even if your Untis database hosts only one +school. + +.. warning:: + + If your Untis database hosts several schools, but you forget to + configure the school ID, data corruption may occur! + Customise how data are imported -------------------------------- +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -The way data are imported can be configured from the menu under -`Admin → Configuration → Untis`. The following preferences are available: +The behaviour of the import can be customised in several ways. The +following preferences are available: * **Update values of existing subjects:** This will update the values of already existing subjects if Untis has different data.