mirror of
https://github.com/ansible/awx.git
synced 2026-05-08 01:47:35 -02:30
Make sure we show relative object information in the api
This commit is contained in:
@@ -6,6 +6,7 @@ import json
|
|||||||
import re
|
import re
|
||||||
import socket
|
import socket
|
||||||
import urlparse
|
import urlparse
|
||||||
|
import logging
|
||||||
|
|
||||||
# PyYAML
|
# PyYAML
|
||||||
import yaml
|
import yaml
|
||||||
@@ -27,7 +28,9 @@ from rest_framework import serializers
|
|||||||
|
|
||||||
# AWX
|
# AWX
|
||||||
from awx.main.models import *
|
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',
|
BASE_FIELDS = ('id', 'url', 'related', 'summary_fields', 'created', 'modified',
|
||||||
'name', 'description')
|
'name', 'description')
|
||||||
@@ -1008,22 +1011,31 @@ class ActivityStreamSerializer(BaseSerializer):
|
|||||||
def get_related(self, obj):
|
def get_related(self, obj):
|
||||||
if obj is None:
|
if obj is None:
|
||||||
return {}
|
return {}
|
||||||
# res = super(ActivityStreamSerializer, self).get_related(obj)
|
rel = {}
|
||||||
# res.update(dict(
|
if obj.user is not None:
|
||||||
# #audit_trail = reverse('api:organization_audit_trail_list', args=(obj.pk,)),
|
rel['user'] = reverse('api:user_detail', args=(obj.user.pk,))
|
||||||
# projects = reverse('api:organization_projects_list', args=(obj.pk,)),
|
obj1_resolution = camelcase_to_underscore(obj.object1_type.split(".")[-1])
|
||||||
# inventories = reverse('api:organization_inventories_list', args=(obj.pk,)),
|
rel['object_1'] = reverse('api:' + obj1_resolution + '_detail', args=(obj.object1_id,))
|
||||||
# users = reverse('api:organization_users_list', args=(obj.pk,)),
|
if obj.operation in ('associate', 'disassociate'):
|
||||||
# admins = reverse('api:organization_admins_list', args=(obj.pk,)),
|
obj2_resolution = camelcase_to_underscore(obj.object2_type.split(".")[-1])
|
||||||
# #tags = reverse('api:organization_tags_list', args=(obj.pk,)),
|
rel['object_2'] = reverse('api:' + obj2_resolution + '_detail', args(obj.object2_id,))
|
||||||
# teams = reverse('api:organization_teams_list', args=(obj.pk,)),
|
return rel
|
||||||
# ))
|
|
||||||
# return res
|
|
||||||
|
|
||||||
def get_summary_fields(self, obj):
|
def get_summary_fields(self, obj):
|
||||||
if obj is None:
|
if obj is None:
|
||||||
return {}
|
return {}
|
||||||
d = super(ActivityStreamSerializer, self).get_summary_fields(obj)
|
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
|
return d
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ from django.dispatch import receiver
|
|||||||
|
|
||||||
# AWX
|
# AWX
|
||||||
from awx.main.models import *
|
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__ = []
|
__all__ = []
|
||||||
|
|
||||||
@@ -178,7 +178,8 @@ def activity_stream_create(sender, instance, created, **kwargs):
|
|||||||
activity_entry = ActivityStream(
|
activity_entry = ActivityStream(
|
||||||
operation='create',
|
operation='create',
|
||||||
object1_id=instance.id,
|
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()
|
activity_entry.save()
|
||||||
|
|
||||||
def activity_stream_update(sender, instance, **kwargs):
|
def activity_stream_update(sender, instance, **kwargs):
|
||||||
@@ -192,7 +193,7 @@ def activity_stream_update(sender, instance, **kwargs):
|
|||||||
activity_entry = ActivityStream(
|
activity_entry = ActivityStream(
|
||||||
operation='update',
|
operation='update',
|
||||||
object1_id=instance.id,
|
object1_id=instance.id,
|
||||||
object1_type=str(instance.__class__),
|
object1_type=instance.__class__.__module__ + "." + instance.__class__.__name__,
|
||||||
changes=json.dumps(changes))
|
changes=json.dumps(changes))
|
||||||
activity_entry.save()
|
activity_entry.save()
|
||||||
|
|
||||||
@@ -201,7 +202,7 @@ def activity_stream_delete(sender, instance, **kwargs):
|
|||||||
activity_entry = ActivityStream(
|
activity_entry = ActivityStream(
|
||||||
operation='delete',
|
operation='delete',
|
||||||
object1_id=instance.id,
|
object1_id=instance.id,
|
||||||
object1_type=str(instance.__class__))
|
object1_type=instance.__class__.__module__ + "." + instance.__class__.__name__)
|
||||||
activity_entry.save()
|
activity_entry.save()
|
||||||
|
|
||||||
def activity_stream_associate(sender, instance, **kwargs):
|
def activity_stream_associate(sender, instance, **kwargs):
|
||||||
@@ -214,15 +215,15 @@ def activity_stream_associate(sender, instance, **kwargs):
|
|||||||
return
|
return
|
||||||
obj1 = instance
|
obj1 = instance
|
||||||
obj1_id = obj1.id
|
obj1_id = obj1.id
|
||||||
obj_rel = str(sender)
|
obj_rel = sender.__module__ + "." + sender.__name__
|
||||||
for entity_acted in kwargs['pk_set']:
|
for entity_acted in kwargs['pk_set']:
|
||||||
obj2 = kwargs['model']
|
obj2 = kwargs['model']
|
||||||
obj2_id = entity_acted
|
obj2_id = entity_acted
|
||||||
activity_entry = ActivityStream(
|
activity_entry = ActivityStream(
|
||||||
operation=action,
|
operation=action,
|
||||||
object1_id=obj1_id,
|
object1_id=obj1_id,
|
||||||
object1_type=str(obj1.__class__),
|
object1_type=obj1.__class__.__module__ + "." + obj1.__class__.__name__,
|
||||||
object2_id=obj2_id,
|
object2_id=obj2_id,
|
||||||
object2_type=str(obj2.__class__),
|
object2_type=obj2.__module__ + "." + obj2.__name__,
|
||||||
object_relationship_type=obj_rel)
|
object_relationship_type=obj_rel)
|
||||||
activity_entry.save()
|
activity_entry.save()
|
||||||
|
|||||||
@@ -255,3 +255,12 @@ def model_instance_diff(old, new):
|
|||||||
diff = None
|
diff = None
|
||||||
|
|
||||||
return diff
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user