Skip to content

Commit 308dcd6

Browse files
committed
Implement SonarCloud style suggestions
1 parent aace5e7 commit 308dcd6

File tree

6 files changed

+43
-263
lines changed

6 files changed

+43
-263
lines changed

ansible_base/rbac/management/__init__.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@ def create_dab_permissions(app_config, verbosity=2, interactive=True, using=DEFA
3131
return
3232

3333
try:
34-
DABContentType = apps.get_model("dab_rbac", "DABContentType")
34+
dab_ct_cls = apps.get_model("dab_rbac", "DABContentType")
3535
except LookupError:
3636
logger.warning('Skipping DAB RBAC type and permission creation since models are not available')
3737
return
3838

39-
if not router.allow_migrate_model(using, DABContentType):
39+
if not router.allow_migrate_model(using, dab_ct_cls):
4040
# Uncommon case, code logic is using a replica database or something, unlikely to be relevant
4141
return
4242

43-
sync_DAB_permissions(verbosity=verbosity, using=using, apps=global_apps)
43+
sync_dab_permissions(verbosity=verbosity, using=using, apps=global_apps)
4444

4545

4646
def is_safe_identifier(name: str) -> bool:
@@ -67,13 +67,13 @@ def reset_ct_sequence(ct_cls):
6767
)
6868

6969

70-
def sync_DAB_permissions(verbosity=2, using=DEFAULT_DB_ALIAS, apps=global_apps):
70+
def sync_dab_permissions(verbosity=2, using=DEFAULT_DB_ALIAS, apps=global_apps):
7171
"""Idepotent method to set database types and permissions for DAB RBAC
7272
7373
This should make the database content reflect the model Meta data and
7474
registrations in the permission_registry for that app.
7575
"""
76-
DABContentType = apps.get_model("dab_rbac", "DABContentType")
76+
dab_ct_cls = apps.get_model("dab_rbac", "DABContentType")
7777
Permission = apps.get_model("dab_rbac", "DABPermission")
7878

7979
new_cts = create_DAB_contenttypes(verbosity=verbosity, using=using, apps=apps)
@@ -86,7 +86,7 @@ def sync_DAB_permissions(verbosity=2, using=DEFAULT_DB_ALIAS, apps=global_apps):
8686
# Force looking up the content types in the current database
8787
# before creating foreign keys to them.
8888
service = get_resource_prefix(klass)
89-
ctype = DABContentType.objects.db_manager(using).get_for_model(klass, service=service, for_concrete_model=False)
89+
ctype = dab_ct_cls.objects.db_manager(using).get_for_model(klass, service=service, for_concrete_model=False)
9090

9191
ctypes.add(ctype)
9292

@@ -132,4 +132,4 @@ def sync_DAB_permissions(verbosity=2, using=DEFAULT_DB_ALIAS, apps=global_apps):
132132
# Reset the sequence to avoid PK collision later
133133
if connection.vendor == 'postgresql':
134134
if new_cts:
135-
reset_ct_sequence(DABContentType)
135+
reset_ct_sequence(dab_ct_cls)

ansible_base/rbac/management/create_types.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,31 +31,31 @@ def create_DAB_contenttypes(
3131
3232
Returns a list of the _new_ content types created
3333
"""
34-
DABContentType = apps.get_model("dab_rbac", "DABContentType")
35-
ContentType = apps.get_model("contenttypes", "ContentType")
34+
dab_ct_cls = apps.get_model("dab_rbac", "DABContentType")
35+
ct_cls = apps.get_model("contenttypes", "ContentType")
3636

37-
content_types = get_local_DAB_contenttypes(using, DABContentType)
37+
content_types = get_local_DAB_contenttypes(using, dab_ct_cls)
3838

3939
ct_data = []
4040
for model in permission_registry.all_registered_models:
4141
service = get_resource_prefix(model)
4242
if (service, model._meta.model_name) not in content_types:
4343
# The content type is not seen in existing entries, add to list for creation
44-
ct_item_data = dict(
45-
service=service,
46-
app_label=model._meta.app_label,
47-
model=model._meta.model_name,
48-
api_slug=f'{service}.{model._meta.model_name}',
49-
pk_field_type=model._meta.pk.db_type(connection),
50-
)
44+
ct_item_data = {
45+
'service': service,
46+
'app_label': model._meta.app_label,
47+
'model': model._meta.model_name,
48+
'api_slug': f'{service}.{model._meta.model_name}',
49+
'pk_field_type': model._meta.pk.db_type(connection),
50+
}
5151
# To make usage earier in a transitional period, we will set the content type
5252
# of any new entries created here to the id of its corresponding ContentType
5353
# from the actual contenttypes app, allowing many filters to work
54-
real_ct = ContentType.objects.get_for_model(model)
55-
if not DABContentType.objects.filter(id=real_ct.id).exists():
54+
real_ct = ct_cls.objects.get_for_model(model)
55+
if not dab_ct_cls.objects.filter(id=real_ct.id).exists():
5656
ct_item_data['id'] = real_ct.id
5757
else:
58-
current_max_id = DABContentType.objects.order_by('-id').values_list('id', flat=True).first() or 0
58+
current_max_id = dab_ct_cls.objects.order_by('-id').values_list('id', flat=True).first() or 0
5959
ct_item_data['id'] = current_max_id + 1
6060
ct_data.append(ct_item_data)
6161
if not ct_data:
@@ -66,19 +66,19 @@ def create_DAB_contenttypes(
6666
# from the actual contenttypes app, allowing many filters to work
6767
cts = []
6868
for ct_item_data in ct_data:
69-
cts.append(DABContentType.objects.create(**ct_item_data))
69+
cts.append(dab_ct_cls.objects.create(**ct_item_data))
7070

7171
if verbosity >= 2:
7272
for ct in cts:
7373
logger.debug("Adding DAB content type " f"'{ct.service}:{ct.app_label} | {ct.model}'")
7474

7575
updated_ct = 0
76-
for ct in DABContentType.objects.all():
76+
for ct in dab_ct_cls.objects.all():
7777
if not permission_registry.is_registered(ct.model_class()):
7878
logger.warning(f'{ct.model} is a stale content type in DAB RBAC')
7979
continue
8080
if parent_model := permission_registry.get_parent_model(ct.model_class()):
81-
parent_content_type = DABContentType.objects.get_for_model(parent_model)
81+
parent_content_type = dab_ct_cls.objects.get_for_model(parent_model)
8282
if ct.parent_content_type != parent_content_type:
8383
ct.parent_content_type = parent_content_type
8484
ct.save(update_fields=['parent_content_type'])

ansible_base/rbac/models/content_type.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,21 @@ def get_for_model(
6262
return ct
6363

6464
if service is None:
65-
service = get_resource_prefix(model)
65+
query_service = get_resource_prefix(model)
66+
else:
67+
query_service = service
68+
6669
opts = self._get_opts(model, for_concrete_model)
6770
try:
68-
return self._get_from_cache(opts, service)
71+
return self._get_from_cache(opts, query_service)
6972
except KeyError:
7073
pass
7174

7275
try:
73-
ct = self.get(service=service, app_label=opts.app_label, model=opts.model_name)
76+
ct = self.get(service=query_service, app_label=opts.app_label, model=opts.model_name)
7477
except self.model.DoesNotExist:
7578
raise RuntimeError(
76-
f'Could not find content type for {(service, opts.app_label, opts.model_name)}, '
79+
f'Could not find content type for {(query_service, opts.app_label, opts.model_name)}, '
7780
'and creating new objects via get_for_model is not allowed for DAB RBAC'
7881
)
7982
self._add_to_cache(self.db, ct)
@@ -147,9 +150,8 @@ def get_by_natural_key(self, *args: str) -> django_models.Model:
147150
kwargs = {'service__in': [get_local_resource_prefix(), 'shared'], 'app_label': app_label, 'model': model}
148151
# This ask here is actually ambiguous, so we try this extra lookup
149152
shared_key = ('shared', app_label, model)
150-
if self.db in self._cache:
151-
if shared_key in self._cache[self.db]:
152-
return self._cache[self.db][shared_key]
153+
if shared_key in self._cache.get(self.db, ()):
154+
return self._cache[self.db][shared_key]
153155
else:
154156
service, app_label, model = args
155157
kwargs = {'service': service, 'app_label': app_label, 'model': model}

0 commit comments

Comments
 (0)