feat: ensure languages exist

This commit is contained in:
moanos [he/him] 2024-04-14 13:23:35 +02:00
parent ab1246b6d0
commit 707719c299
2 changed files with 11 additions and 4 deletions

View File

@ -1,5 +1,5 @@
from django.apps import AppConfig from django.apps import AppConfig
from .tools.signals import ensure_groups from .tools.signals import ensure_groups, ensure_languages
from django.db.models.signals import post_migrate from django.db.models.signals import post_migrate
@ -14,3 +14,4 @@ class FellchensammlungConfig(AppConfig):
post_migrate.connect(ensure_groups, sender=self) post_migrate.connect(ensure_groups, sender=self)
except Permission.DoesNotExist: except Permission.DoesNotExist:
pass pass
post_migrate.connect(ensure_languages, sender=self)

View File

@ -1,20 +1,21 @@
import logging
def _get_perms(permission_name_list): def _get_perms(permission_name_list):
from django.contrib.auth.models import Permission from django.contrib.auth.models import Permission
permissions = [] permissions = []
for permission_name in permission_name_list: for permission_name in permission_name_list:
print(f"{permission_name}")
try: try:
permission = Permission.objects.get(codename=permission_name) permission = Permission.objects.get(codename=permission_name)
permissions.append(permission) permissions.append(permission)
except Permission.DoesNotExist: except Permission.DoesNotExist:
print("aaaaaa") logging.warning("Permissions not correctly migrated, please run the migration again")
return permissions return permissions
def ensure_groups(sender, **kwargs): def ensure_groups(sender, **kwargs):
from django.contrib.auth.models import Group from django.contrib.auth.models import Group
from django.contrib.auth.models import Permission from django.contrib.auth.models import Permission
print([permission.name for permission in Permission.objects.all()])
member_permissions = [] member_permissions = []
@ -33,3 +34,8 @@ def ensure_groups(sender, **kwargs):
members, created = Group.objects.get_or_create(name="Members") members, created = Group.objects.get_or_create(name="Members")
members.permissions.set(_get_perms(member_permissions)) members.permissions.set(_get_perms(member_permissions))
def ensure_languages(sender, **kwargs):
from fellchensammlung.models import Language
Language.objects.get_or_create(name="Deutsch", languagecode="de")
Language.objects.get_or_create(name="English", languagecode="en")