From 24d2a9c7db04e258dd48144f805db7157ca481dc Mon Sep 17 00:00:00 2001 From: Michael Herrmann Date: Tue, 21 Dec 2021 17:57:26 +0100 Subject: [PATCH] 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. --- tenants/apps.py | 24 ------------------------ 1 file changed, 24 deletions(-) diff --git a/tenants/apps.py b/tenants/apps.py index 2a5fe6f..71c4b9b 100644 --- a/tenants/apps.py +++ b/tenants/apps.py @@ -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)