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

Merge branch '35-disambiguate-long-group-names' into 'master'

Resolve "Disambiguate long group names"

Closes #35

See merge request !103
parents 7d215d70 c8d01034
No related branches found
No related tags found
1 merge request!103Resolve "Disambiguate long group names"
Pipeline #46348 canceled
......@@ -13,6 +13,7 @@ Changed
~~~~~~~
* Wrap all imports in complete revisions to make it possible to undo them completely and to track changes correctly.
* Group names are now optionally disambiguated on collisions in Untis
Fixed
~~~~~
......
......@@ -48,6 +48,14 @@ class UpdateGroupsName(BooleanPreference):
verbose_name = _("Update name of existing groups")
@site_preferences_registry.register
class DisambiguateGroupsName(BooleanPreference):
section = untis_mysql
name = "disambiguate_groups_name"
default = True
verbose_name = _("Disambiguate name of new groups")
@site_preferences_registry.register
class OverwriteGroupOwners(BooleanPreference):
section = untis_mysql
......
import logging
import re
from datetime import time
from enum import Enum
from typing import Dict
......@@ -191,6 +192,30 @@ def import_classes(
school_term__in=[None, validity_range.school_term],
)
except core_models.Group.DoesNotExist:
# Determine collisions of long name
while core_models.Group.objects.filter(
name=name, school_term=validity_range.school_term
).exists():
if not get_site_preferences()["untis_mysql__disambiguate_groups_name"]:
# Do not try to disambiguate it not enabled
break
suffix = f" ({short_name})"
if name.endswith(suffix):
# First try, add a counter
name = f"{name} (2)"
else:
# Second or more tries, determine last counter, if any
match = re.match(r"^(.*)(\(\d+\))$", name)
if match:
# Counter found, increase
prefix = match.group(1)
counter = int(match.group(2)) + 1
name = f"{prefix}({counter})"
else:
# Counter not found, add one
name = f"{name} (2)"
new_group = core_models.Group.objects.create(
short_name=short_name,
name=name,
......
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