mirror of
https://github.com/ansible/awx.git
synced 2026-05-10 02:47:36 -02:30
shortcut for generating role summary_fields
This commit is contained in:
@@ -331,13 +331,7 @@ class BaseSerializer(serializers.ModelSerializer):
|
|||||||
roles = {}
|
roles = {}
|
||||||
for field in obj._meta.get_fields():
|
for field in obj._meta.get_fields():
|
||||||
if type(field) is ImplicitRoleField:
|
if type(field) is ImplicitRoleField:
|
||||||
role = getattr(obj, field.name)
|
roles[field.name] = role_summary_fields_generator(obj, field.name)
|
||||||
#roles[field.name] = RoleSerializer(data=role).to_representation(role)
|
|
||||||
roles[field.name] = {
|
|
||||||
'id': role.id,
|
|
||||||
'name': role.name,
|
|
||||||
'description': role.get_description(reference_content_object=obj),
|
|
||||||
}
|
|
||||||
if len(roles) > 0:
|
if len(roles) > 0:
|
||||||
summary_fields['object_roles'] = roles
|
summary_fields['object_roles'] = roles
|
||||||
|
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ __all__ = [
|
|||||||
'get_roles_on_resource',
|
'get_roles_on_resource',
|
||||||
'ROLE_SINGLETON_SYSTEM_ADMINISTRATOR',
|
'ROLE_SINGLETON_SYSTEM_ADMINISTRATOR',
|
||||||
'ROLE_SINGLETON_SYSTEM_AUDITOR',
|
'ROLE_SINGLETON_SYSTEM_AUDITOR',
|
||||||
|
'role_summary_fields_generator'
|
||||||
]
|
]
|
||||||
|
|
||||||
logger = logging.getLogger('awx.main.models.rbac')
|
logger = logging.getLogger('awx.main.models.rbac')
|
||||||
@@ -165,13 +166,11 @@ class Role(models.Model):
|
|||||||
global role_names
|
global role_names
|
||||||
return role_names[self.role_field]
|
return role_names[self.role_field]
|
||||||
|
|
||||||
def get_description(self, reference_content_object=None):
|
@property
|
||||||
|
def description(self):
|
||||||
global role_descriptions
|
global role_descriptions
|
||||||
description = role_descriptions[self.role_field]
|
description = role_descriptions[self.role_field]
|
||||||
if reference_content_object:
|
content_type = self.content_type
|
||||||
content_type = ContentType.objects.get_for_model(reference_content_object)
|
|
||||||
else:
|
|
||||||
content_type = self.content_type
|
|
||||||
if '%s' in description and content_type:
|
if '%s' in description and content_type:
|
||||||
model = content_type.model_class()
|
model = content_type.model_class()
|
||||||
model_name = re.sub(r'([a-z])([A-Z])', r'\1 \2', model.__name__).lower()
|
model_name = re.sub(r'([a-z])([A-Z])', r'\1 \2', model.__name__).lower()
|
||||||
@@ -179,8 +178,6 @@ class Role(models.Model):
|
|||||||
|
|
||||||
return description
|
return description
|
||||||
|
|
||||||
description = property(get_description)
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def rebuild_role_ancestor_list(additions, removals):
|
def rebuild_role_ancestor_list(additions, removals):
|
||||||
'''
|
'''
|
||||||
@@ -474,3 +471,20 @@ def get_roles_on_resource(resource, accessor):
|
|||||||
object_id=resource.id
|
object_id=resource.id
|
||||||
).values_list('role_field', flat=True).distinct()
|
).values_list('role_field', flat=True).distinct()
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def role_summary_fields_generator(content_object, role_field):
|
||||||
|
global role_descriptions
|
||||||
|
global role_names
|
||||||
|
summary = {}
|
||||||
|
description = role_descriptions[role_field]
|
||||||
|
content_type = ContentType.objects.get_for_model(content_object)
|
||||||
|
if '%s' in description and content_type:
|
||||||
|
model = content_object.__class__
|
||||||
|
model_name = re.sub(r'([a-z])([A-Z])', r'\1 \2', model.__name__).lower()
|
||||||
|
description = description % model_name
|
||||||
|
|
||||||
|
summary['description'] = description
|
||||||
|
summary['name'] = role_names[role_field]
|
||||||
|
summary['id'] = getattr(content_object, '{}_id'.format(role_field))
|
||||||
|
return summary
|
||||||
|
|||||||
Reference in New Issue
Block a user