diff --git a/awx/api/serializers.py b/awx/api/serializers.py index 051496b91f..cf136bff89 100644 --- a/awx/api/serializers.py +++ b/awx/api/serializers.py @@ -6,6 +6,7 @@ import json import re import socket import urlparse +import logging # PyYAML import yaml @@ -27,7 +28,9 @@ from rest_framework import serializers # AWX from awx.main.models import * -from awx.main.utils import update_scm_url +from awx.main.utils import update_scm_url, camelcase_to_underscore + +logger = logging.getLogger('awx.api.serializers') BASE_FIELDS = ('id', 'url', 'related', 'summary_fields', 'created', 'modified', 'name', 'description') @@ -1008,22 +1011,31 @@ class ActivityStreamSerializer(BaseSerializer): def get_related(self, obj): if obj is None: return {} - # res = super(ActivityStreamSerializer, self).get_related(obj) - # res.update(dict( - # #audit_trail = reverse('api:organization_audit_trail_list', args=(obj.pk,)), - # projects = reverse('api:organization_projects_list', args=(obj.pk,)), - # inventories = reverse('api:organization_inventories_list', args=(obj.pk,)), - # users = reverse('api:organization_users_list', args=(obj.pk,)), - # admins = reverse('api:organization_admins_list', args=(obj.pk,)), - # #tags = reverse('api:organization_tags_list', args=(obj.pk,)), - # teams = reverse('api:organization_teams_list', args=(obj.pk,)), - # )) - # return res + rel = {} + if obj.user is not None: + rel['user'] = reverse('api:user_detail', args=(obj.user.pk,)) + obj1_resolution = camelcase_to_underscore(obj.object1_type.split(".")[-1]) + rel['object_1'] = reverse('api:' + obj1_resolution + '_detail', args=(obj.object1_id,)) + if obj.operation in ('associate', 'disassociate'): + obj2_resolution = camelcase_to_underscore(obj.object2_type.split(".")[-1]) + rel['object_2'] = reverse('api:' + obj2_resolution + '_detail', args(obj.object2_id,)) + return rel def get_summary_fields(self, obj): if obj is None: return {} d = super(ActivityStreamSerializer, self).get_summary_fields(obj) + try: + obj1 = eval(obj.object1_type + ".objects.get(id=" + str(obj.object1_id) + ")") + d['object1'] = {'name': obj1.name, 'description': obj1.description} + except Exception, e: + logger.error("Error getting object 1 summary: " + str(e)) + try: + if obj.operation in ('associate', 'disassociate'): + obj2 = eval(obj.object1_type + ".objects.get(id=" + str(obj.object2_id) + ")") + d['object2'] = {'name': obj2.name, 'description': obj2.description} + except Exception, e: + logger.error("Error getting object 2 summary: " + str(e)) return d diff --git a/awx/main/signals.py b/awx/main/signals.py index 5a6ae810c3..afab9f64e6 100644 --- a/awx/main/signals.py +++ b/awx/main/signals.py @@ -12,7 +12,7 @@ from django.dispatch import receiver # AWX from awx.main.models import * -from awx.main.utils import model_instance_diff +from awx.main.utils import model_instance_diff, model_to_dict __all__ = [] @@ -178,7 +178,8 @@ def activity_stream_create(sender, instance, created, **kwargs): activity_entry = ActivityStream( operation='create', object1_id=instance.id, - object1_type=str(instance.__class__)) + object1_type=instance.__class__.__module__ + "." + instance.__class__.__name__, + changes=json.dumps(model_to_dict(instance))) activity_entry.save() def activity_stream_update(sender, instance, **kwargs): @@ -192,7 +193,7 @@ def activity_stream_update(sender, instance, **kwargs): activity_entry = ActivityStream( operation='update', object1_id=instance.id, - object1_type=str(instance.__class__), + object1_type=instance.__class__.__module__ + "." + instance.__class__.__name__, changes=json.dumps(changes)) activity_entry.save() @@ -201,7 +202,7 @@ def activity_stream_delete(sender, instance, **kwargs): activity_entry = ActivityStream( operation='delete', object1_id=instance.id, - object1_type=str(instance.__class__)) + object1_type=instance.__class__.__module__ + "." + instance.__class__.__name__) activity_entry.save() def activity_stream_associate(sender, instance, **kwargs): @@ -214,15 +215,15 @@ def activity_stream_associate(sender, instance, **kwargs): return obj1 = instance obj1_id = obj1.id - obj_rel = str(sender) + obj_rel = sender.__module__ + "." + sender.__name__ for entity_acted in kwargs['pk_set']: obj2 = kwargs['model'] obj2_id = entity_acted activity_entry = ActivityStream( operation=action, object1_id=obj1_id, - object1_type=str(obj1.__class__), + object1_type=obj1.__class__.__module__ + "." + obj1.__class__.__name__, object2_id=obj2_id, - object2_type=str(obj2.__class__), + object2_type=obj2.__module__ + "." + obj2.__name__, object_relationship_type=obj_rel) activity_entry.save() diff --git a/awx/main/utils.py b/awx/main/utils.py index 3fdfa21aa2..7665508b27 100644 --- a/awx/main/utils.py +++ b/awx/main/utils.py @@ -255,3 +255,12 @@ def model_instance_diff(old, new): diff = None return diff + +def model_to_dict(obj): + """ + Serialize a model instance to a dictionary as best as possible + """ + attr_d = {} + for field in obj._meta.fields: + attr_d[field.name] = str(getattr(obj, field.name, None)) + return attr_d