Instead of exposing Role.content_type, create a new serializer field

called `object_type`, which is constructed based on manipulating the
string value of ActivityStream.object_relationship_type.  Since that
field does have the full class name, this manipulation should match
the manipulation that is done to construct the values of object1 and
object2 when ActivityStream is created.
This commit is contained in:
Jeff Bradberry 2019-05-10 17:31:33 -04:00
parent 250484339b
commit d6e89092d3
2 changed files with 20 additions and 6 deletions

View File

@ -128,7 +128,7 @@ SUMMARIZABLE_FK_FIELDS = {
'inventory_source': ('source', 'last_updated', 'status'),
'custom_inventory_script': DEFAULT_SUMMARY_FIELDS,
'source_script': ('name', 'description'),
'role': ('id', 'role_field', 'content_type'),
'role': ('id', 'role_field'),
'notification_template': DEFAULT_SUMMARY_FIELDS,
'instance_group': {'id', 'name', 'controller_id'},
'insights_credential': DEFAULT_SUMMARY_FIELDS,
@ -4956,6 +4956,7 @@ class ActivityStreamSerializer(BaseSerializer):
changes = serializers.SerializerMethodField()
object_association = serializers.SerializerMethodField()
object_type = serializers.SerializerMethodField()
@cached_property
def _local_summarizable_fk_fields(self):
@ -4980,8 +4981,8 @@ class ActivityStreamSerializer(BaseSerializer):
class Meta:
model = ActivityStream
fields = ('*', '-name', '-description', '-created', '-modified',
'timestamp', 'operation', 'changes', 'object1', 'object2', 'object_association')
fields = ('*', '-name', '-description', '-created', '-modified', 'timestamp', 'operation',
'changes', 'object1', 'object2', 'object_association', 'object_type')
def get_fields(self):
ret = super(ActivityStreamSerializer, self).get_fields()
@ -5024,6 +5025,21 @@ class ActivityStreamSerializer(BaseSerializer):
logger.debug('Failed to parse activity stream relationship type {}'.format(obj.object_relationship_type))
return ""
def get_object_type(self, obj):
if not obj.object_relationship_type:
return ""
elif obj.object_relationship_type.endswith('_role'):
return camelcase_to_underscore(obj.object_relationship_type.rsplit('.', 2)[-2])
# default case: these values look like
# "awx.main.models.organization.Organization_notification_templates_success"
# so we have to take after the last period but before the first underscore.
try:
cls = obj.object_relationship_type.rsplit('.', 1)[0]
return camelcase_to_underscore(cls.split('_', 1))
except Exception:
logger.debug('Failed to parse activity stream relationship type {}'.format(obj.object_relationship_type))
return ""
def get_related(self, obj):
rel = {}
if obj.actor is not None:
@ -5106,8 +5122,6 @@ class ActivityStreamSerializer(BaseSerializer):
for field in related_fields:
fval = getattr(thisItem, field, None)
if fval is not None:
if field == 'content_type':
fval = str(fval)
thisItemDict[field] = fval
summary_fields[fk].append(thisItemDict)
except ObjectDoesNotExist:

View File

@ -22,7 +22,7 @@ export default function BuildDescription(BuildAnchor, $log, i18n) {
// if object1 winds up being the role's resource, we need to swap the objects
// in order to make the sentence make sense.
if (activity.summary_fields.role[0].content_type === object1) {
if (activity.object_type === object1) {
object1 = activity.object2;
object2 = activity.object1;
}