Don't auto-create Tenants group

Django makes it impossibly difficult to manipulate groups and
permissions during initialization (eg. from migrations).
See eg. https://stackoverflow.com/q/67232243/1839209.
This commit is contained in:
Michael Herrmann
2021-12-21 17:57:26 +01:00
parent 917bc45047
commit 24d2a9c7db
-24
View File
@@ -4,27 +4,3 @@ from django.apps import AppConfig
class TenantsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'tenants'
def ready(self):
super().ready()
_ensure_tenants_group_exists()
def _ensure_tenants_group_exists():
"""
Make sure the "Tenants" group exists so users can be assigned to it.
Normally, we would want to do this in a migration. Unfortunately, Django
cannot currently (easily) assign permissions in a migration [1].
The `ready()` method above seems like a reasonable place to do it instead.
"""
# We cannot import the following at the module level because Django has not
# been initialized yet:
from django.contrib.auth.models import Group, Permission
from winget.models import Package, Version, Installer
group, created = Group.objects.get_or_create(name='Tenants')
if created:
for model in (Package, Version, Installer):
for action in ('view', 'add', 'change', 'delete'):
codename = f'{action}_{model._meta.model_name}'
permission = Permission.objects.get(codename=codename)
group.permissions.add(permission)