Merge pull request #3393 from AlanCoding/role_description_optimization

Avoid redundant lookups of content_type
This commit is contained in:
Alan Rominger 2016-09-13 09:56:03 -04:00 committed by GitHub
commit 2e3982e295
2 changed files with 10 additions and 5 deletions

View File

@ -327,7 +327,7 @@ class BaseSerializer(serializers.ModelSerializer):
roles[field.name] = {
'id': role.id,
'name': role.name,
'description': role.description,
'description': role.get_description(reference_content_object=obj),
}
if len(roles) > 0:
summary_fields['object_roles'] = roles

View File

@ -164,17 +164,22 @@ class Role(models.Model):
global role_names
return role_names[self.role_field]
@property
def description(self):
def get_description(self, reference_content_object=None):
global role_descriptions
description = role_descriptions[self.role_field]
if '%s' in description and self.content_type:
model = self.content_type.model_class()
if reference_content_object:
content_type = ContentType.objects.get_for_model(reference_content_object)
else:
content_type = self.content_type
if '%s' in description and content_type:
model = content_type.model_class()
model_name = re.sub(r'([a-z])([A-Z])', r'\1 \2', model.__name__).lower()
description = description % model_name
return description
description = property(get_description)
@staticmethod
def rebuild_role_ancestor_list(additions, removals):
'''