Skip to content
Snippets Groups Projects
Commit 8d157e0c authored by Jonathan Weth's avatar Jonathan Weth :keyboard:
Browse files

Merge branch '49-import-should-respect-school_id' into 'master'

Resolve "Import should respect SCHOOL_ID"

Closes #49

See merge request !138
parents ac1a8bf9 adfb3556
No related branches found
No related tags found
1 merge request!138Resolve "Import should respect SCHOOL_ID"
Pipeline #65496 failed
Pipeline: AlekSIS

#65500

    ......@@ -9,6 +9,11 @@ and this project adheres to `Semantic Versioning`_.
    Unreleased
    ----------
    Added
    ~~~~~
    * Support for configuring the Untis school ID
    Fixed
    ~~~~~
    ......
    ......@@ -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):
    ......
    ......@@ -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)
    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),
    ]
    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
    ......
    ......@@ -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 = (
    ......
    ......@@ -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
    ......
    ......@@ -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.
    ......
    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