Skip to content

Commit ca7e1bb

Browse files
committed
Avoid content type model method in migrations
1 parent 65f2af0 commit ca7e1bb

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

ansible_base/rbac/management/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ def sync_dab_permissions(verbosity=2, using=DEFAULT_DB_ALIAS, apps=global_apps):
8080
This should make the database content reflect the model Meta data and
8181
registrations in the permission_registry for that app.
8282
"""
83-
dab_ct_cls = apps.get_model("dab_rbac", "DABContentType")
8483
Permission = apps.get_model("dab_rbac", "DABPermission")
84+
dab_ct_cls = Permission._meta.get_field('content_type').related_model
8585

8686
new_cts = create_DAB_contenttypes(verbosity=verbosity, using=using, apps=apps)
8787

ansible_base/rbac/management/create_types.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from django.db import DEFAULT_DB_ALIAS, connection, models
66

77
from ansible_base.rbac import permission_registry
8-
from ansible_base.rbac.remote import get_resource_prefix
8+
from ansible_base.rbac.remote import get_resource_prefix, get_local_resource_prefix, get_remote_standin_class
99

1010
logger = logging.getLogger(__name__)
1111

@@ -17,6 +17,14 @@ def get_local_dab_contenttypes(using: str, ct_model: Type[models.Model]) -> dict
1717
return {(ct.service, ct.model): ct for ct in ct_model.objects.using(using)}
1818

1919

20+
def model_class(apps, ct):
21+
"Model methods normally can not be used in migrations so this is a safer utility method"
22+
if ct.service not in ("shared", get_local_resource_prefix()):
23+
return get_remote_standin_class(ct)
24+
25+
return apps.get_model(ct.app_label, ct.model)
26+
27+
2028
def create_DAB_contenttypes(
2129
verbosity=2,
2230
using=DEFAULT_DB_ALIAS,
@@ -74,10 +82,11 @@ def create_DAB_contenttypes(
7482

7583
updated_ct = 0
7684
for ct in dab_ct_cls.objects.all():
77-
if not permission_registry.is_registered(ct.model_class()):
85+
ct_model_class = model_class(apps, ct)
86+
if not permission_registry.is_registered(ct_model_class):
7887
logger.warning(f'{ct.model} is a stale content type in DAB RBAC')
7988
continue
80-
if parent_model := permission_registry.get_parent_model(ct.model_class()):
89+
if parent_model := permission_registry.get_parent_model(ct_model_class):
8190
parent_content_type = dab_ct_cls.objects.get_for_model(parent_model)
8291
if ct.parent_content_type != parent_content_type:
8392
ct.parent_content_type = parent_content_type

0 commit comments

Comments
 (0)