Skip to content
Snippets Groups Projects
Commit 856c8ce6 authored by Nik | Klampfradler's avatar Nik | Klampfradler
Browse files

Merge branch 'refactor/dynamic-preferences' into 'master'

Replace constance by dynamic-preferences

See merge request !18
parents 77b64a28 f4af48b1
No related branches found
No related tags found
1 merge request!18Replace constance by dynamic-preferences
from django import forms
from django.utils.translation import ugettext_lazy as _
from constance import config
from material import Fieldset
from aleksis.core.forms import EditGroupForm
from aleksis.core.models import Group
from aleksis.core.util.core_helpers import get_site_preferences
class UntisUploadForm(forms.Form):
......@@ -33,8 +33,5 @@ GroupSubjectFormset = forms.modelformset_factory(
Group, form=GroupSubjectForm, max_num=0, extra=0
)
if config.UNTIS_IMPORT_MYSQL_USE_COURSE_GROUPS:
if get_site_preferences()["untis_mysql__use_course_groups"]:
EditGroupForm.add_node_to_layout(Fieldset(_("UNTIS import"), "untis_subject"))
from django.utils.translation import gettext as _
from constance import config
from jsonstore import CharField, IntegerField
from aleksis.apps.chronos import models as chronos_models
from aleksis.core import models as core_models
# Core models
from aleksis.core.util.core_helpers import get_site_preferences
core_models.Person.field(
import_ref_untis=IntegerField(verbose_name=_("UNTIS import reference"), null=True, blank=True)
)
core_models.Group.field(
import_ref_untis=IntegerField(verbose_name=_("UNTIS import reference"), null=True, blank=True)
)
if config.UNTIS_IMPORT_MYSQL_USE_COURSE_GROUPS:
if get_site_preferences()["untis_mysql__use_course_groups"]:
core_models.Group.field(
untis_subject=CharField(
verbose_name=_("UNTIS subject"),
......
from django.utils.translation import gettext as _
from dynamic_preferences.preferences import Section
from dynamic_preferences.types import BooleanPreference
from aleksis.core.registries import site_preferences_registry
untis_mysql = Section("untis_mysql", verbose_name=_("UNTIS: MySQL"))
@site_preferences_registry.register
class UpdateSubjects(BooleanPreference):
section = untis_mysql
name = "update_subjects"
default = True
verbose_name = _("Update values of existing subjects")
@site_preferences_registry.register
class UpdatePersonsShortName(BooleanPreference):
section = untis_mysql
name = "update_persons_short_name"
default = False
verbose_name = _("Update short name of existing persons")
@site_preferences_registry.register
class UpdatePersonsName(BooleanPreference):
section = untis_mysql
name = "update_persons_name"
default = False
verbose_name = _("Update name of existing persons")
@site_preferences_registry.register
class UpdateGroupsShortName(BooleanPreference):
section = untis_mysql
name = "update_groups_short_name"
default = False
verbose_name = _("Update short name of existing groups")
@site_preferences_registry.register
class UpdateGroupsName(BooleanPreference):
section = untis_mysql
name = "update_groups_name"
default = False
verbose_name = _("Update name of existing groups")
@site_preferences_registry.register
class OverwriteGroupOwners(BooleanPreference):
section = untis_mysql
name = "overwrite_group_owners"
verbose_name = _("Overwrite group owners")
default = True
@site_preferences_registry.register
class UpdateRoomsName(BooleanPreference):
section = untis_mysql
name = "update_rooms_name"
default = True
verbose_name = _("Update name of existing rooms")
@site_preferences_registry.register
class UpdateSupervisionAreas(BooleanPreference):
section = untis_mysql
name = "update_supervision_areas"
default = True
verbose_name = _("Update existing supervision areas")
@site_preferences_registry.register
class UseCourseGroups(BooleanPreference):
section = untis_mysql
name = "use_course_groups"
default = True
verbose_name = _("Use course groups")
help_text = _(
"Build or search course groups for every course"
" instead of setting classes as groups."
)
from django.utils.translation import gettext_lazy as _
from aleksis.core.settings import _settings
DATABASES = {
......@@ -12,51 +10,3 @@ DATABASES = {
"PORT": _settings.get("untis.database.port", 3306),
}
}
CONSTANCE_CONFIG = {
"UNTIS_IMPORT_MYSQL_UPDATE_SUBJECTS": (True, _("Update values of existing subjects?"), 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,
),
"UNTIS_IMPORT_MYSQL_UPDATE_GROUPS_SHORT_NAME": (
False,
_("Update short name of existing groups?"),
bool,
),
"UNTIS_IMPORT_MYSQL_UPDATE_GROUPS_NAME": (False, _("Update name of existing groups?"), bool,),
"UNTIS_IMPORT_MYSQL_UPDATE_GROUPS_OVERWRITE_OWNERS": (
False,
_("Overwrite existing owners?"),
bool,
),
"UNTIS_IMPORT_MYSQL_UPDATE_ROOMS_NAME": (True, _("Update name of existing rooms?"), bool,),
"UNTIS_IMPORT_MYSQL_UPDATE_SUPERVISION_AREAS": (
True,
_("Update values of existing supervision areas?"),
),
"UNTIS_IMPORT_MYSQL_USE_COURSE_GROUPS": (
True,
_("Build or search course groups for every course instead of setting classes as groups."),
),
}
CONSTANCE_CONFIG_FIELDSETS = {
"UNTIS import via MySQL: Common Settings": (
"UNTIS_IMPORT_MYSQL_UPDATE_SUBJECTS",
"UNTIS_IMPORT_MYSQL_UPDATE_PERSONS_SHORT_NAME",
"UNTIS_IMPORT_MYSQL_UPDATE_PERSONS_NAME",
"UNTIS_IMPORT_MYSQL_UPDATE_GROUPS_SHORT_NAME",
"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",
"UNTIS_IMPORT_MYSQL_USE_COURSE_GROUPS",
),
}
......@@ -3,11 +3,11 @@ from datetime import time
from enum import Enum
from typing import Dict, List
from constance import config
from tqdm import tqdm
from aleksis.apps.chronos import models as chronos_models
from aleksis.core import models as core_models
from aleksis.core.util.core_helpers import get_site_preferences
from .... import models as mysql_models
from ..util import (
......@@ -67,7 +67,7 @@ def import_subjects() -> Dict[int, chronos_models.Subject]:
# Force sync
changed = False
if config.UNTIS_IMPORT_MYSQL_UPDATE_SUBJECTS and (
if get_site_preferences()["untis_mysql__update_subjects"] and (
new_subject.name != name
or new_subject.colour_fg != colour_fg
or new_subject.colour_bg != colour_bg
......@@ -118,20 +118,19 @@ def import_teachers() -> Dict[int, core_models.Person]:
logger.info("Import teacher {} (as person) …".format(short_name))
new_teacher, created = core_models.Person.objects.get_or_create(
short_name__iexact=short_name,
defaults={
"first_name": first_name,
"last_name": last_name,
"import_ref_untis": import_ref,
},
)
if created:
try:
new_teacher = core_models.Person.objects.get(short_name__iexact=short_name)
except core_models.Person.DoesNotExist:
new_teacher = core_models.Person.objects.create(
short_name=short_name,
first_name=first_name,
last_name=last_name,
import_ref_untis=import_ref,
)
logger.info(" New person created")
changed = False
if config.UNTIS_IMPORT_MYSQL_UPDATE_PERSONS_NAME and (
if get_site_preferences()["untis_mysql__update_persons_name"] and (
new_teacher.first_name != first_name or new_teacher.last_name != last_name
):
new_teacher.first_name = first_name
......@@ -140,7 +139,7 @@ def import_teachers() -> Dict[int, core_models.Person]:
logger.info(" First and last name updated")
if (
config.UNTIS_IMPORT_MYSQL_UPDATE_PERSONS_SHORT_NAME
get_site_preferences()["untis_mysql__update_persons_short_name"]
and new_teacher.short_name != short_name
):
new_teacher.short_name = short_name
......@@ -184,24 +183,25 @@ def import_classes(teachers_ref: Dict[int, core_models.Person]) -> Dict[int, cor
logger.info("Import class {} (as group) …".format(short_name))
new_group, created = core_models.Group.objects.get_or_create(
short_name__iexact=short_name, defaults={"name": name, "import_ref_untis": import_ref},
)
if created:
try:
new_group = core_models.Group.objects.get(short_name__iexact=short_name)
except core_models.Group.DoesNotExist:
new_group = core_models.Group.objects.create(
short_name=short_name, name=name, import_ref_untis=import_ref,
)
logger.info(" New person created")
changed = False
if (
config.UNTIS_IMPORT_MYSQL_UPDATE_GROUPS_SHORT_NAME
get_site_preferences()["untis_mysql__update_groups_short_name"]
and new_group.short_name != short_name
):
new_group.short_name = short_name
changed = True
logger.info(" Short name updated")
if config.UNTIS_IMPORT_MYSQL_UPDATE_GROUPS_NAME and new_group.name != name:
if get_site_preferences()["untis_mysql__update_groups_name"] and new_group.name != name:
new_group.name = name
changed = True
logger.info(" Name updated")
......@@ -214,7 +214,7 @@ def import_classes(teachers_ref: Dict[int, core_models.Person]) -> Dict[int, cor
if changed:
new_group.save()
if config.UNTIS_IMPORT_MYSQL_UPDATE_GROUPS_OVERWRITE_OWNERS:
if get_site_preferences()["untis_mysql__overwrite_group_owners"]:
new_group.owners.set(owners)
logger.info(" Group owners set")
else:
......@@ -256,7 +256,7 @@ def import_rooms() -> Dict[int, chronos_models.Room]:
changed = False
if config.UNTIS_IMPORT_MYSQL_UPDATE_ROOMS_NAME and new_room.name != name:
if get_site_preferences()["untis_mysql__update_rooms_name"] and new_room.name != name:
new_room.name = name
changed = True
logger.info(" Name updated")
......@@ -313,7 +313,7 @@ def import_supervision_areas(breaks_ref, teachers_ref) -> Dict[int, chronos_mode
changed = False
if config.UNTIS_IMPORT_MYSQL_UPDATE_SUPERVISION_AREAS and (
if get_site_preferences()["untis_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
......
......@@ -3,11 +3,11 @@ from datetime import timedelta
from django.utils.translation import gettext as _
from constance import config
from tqdm import tqdm
from aleksis.apps.chronos import models as chronos_models
from aleksis.core import models as core_models
from aleksis.core.util.core_helpers import get_site_preferences
from .... import models as mysql_models
from ..util import (
......@@ -115,7 +115,7 @@ def import_lessons(time_periods_ref, rooms_ref, subjects_ref, teachers_ref, clas
c = classes_ref[class_id]
course_classes.append(c)
if config.UNTIS_IMPORT_MYSQL_USE_COURSE_GROUPS:
if get_site_preferences()["untis_mysql__use_course_groups"]:
# Negative import_ref denotes a course group
group_import_ref = -int("{}{}".format(lesson_id, i))
subject_ref = subject.short_name
......@@ -261,7 +261,5 @@ def import_lessons(time_periods_ref, rooms_ref, subjects_ref, teachers_ref, clas
else:
# Create new lesson period
lesson.periods.add(
time_period, through_defaults={"room": room, "element_id_untis": j},
)
chronos_models.LessonPeriod.objects.create(lesson=lesson, period=time_period, room=room, element_id_untis=j)
logger.info(" New time period added")
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