Skip to content
Snippets Groups Projects
Verified Commit 7de21a31 authored by Jonathan Weth's avatar Jonathan Weth :keyboard:
Browse files

Add hash to preference names to prevent double names

(cherry picked from commit 1459327b)
parent bb88b45d
No related branches found
No related tags found
No related merge requests found
from django.db import migrations
from aleksis.apps.ldap.util.ldap_sync import setting_name_from_field
from aleksis.core.models import Person
from django.contrib.sites.models import Site
_preference_suffixes = ["", "_re", "_replace"]
def _setting_name_old(model, field):
part_1 = model._meta.label_lower.replace(".", "_").replace("__", "_")
return f"additional_field_{part_1}_{field.name}".replace("__", "_")
def _migrate_preferences(apps, schema_editor):
SitePreferenceModel = apps.get_model("core", "SitePreferenceModel")
current_site = Site.objects.get_current()
for field in Person.syncable_fields():
old_setting_name = _setting_name_old(Person, field)
setting_name = setting_name_from_field(Person, field)
for suffix in _preference_suffixes:
old_pref_name = old_setting_name + suffix
new_pref_name = setting_name + suffix
qs = SitePreferenceModel.objects.filter(section="ldap", name=old_pref_name)
if qs.exists():
SitePreferenceModel.objects.update_or_create(
instance=current_site.pk,
section="ldap",
name=new_pref_name,
defaults={"raw_value": qs[0].raw_value},
)
class Migration(migrations.Migration):
initial = True
dependencies = [
("core", "0001_initial"),
("sites", "0002_alter_domain_unique"),
]
operations = [migrations.RunPython(_migrate_preferences)]
import io
import logging
import re
from hashlib import shake_256
from django.apps import apps
from django.conf import settings
......@@ -30,7 +31,9 @@ TQDM_DEFAULTS = {
def setting_name_from_field(model, field):
"""Generate a setting name from a model field."""
name = f"additional_field_{model._meta.label_lower}_{field.name}"
return re.sub(r"[\._]+", "_", name)
name_hash = shake_256(name.encode()).hexdigest(5)
cleaned_name = re.sub(r"[\._]+", "_", name)
return f"{cleaned_name}{name_hash}"
def ldap_field_to_filename(dn, fieldname):
......
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