@@ -25,44 +25,17 @@ class Meta:
2525 fields = ['api_slug' , 'codename' , 'content_type' , 'name' ]
2626
2727
28- class ObjectIDAnsibleIDField (serializers .Field ):
29- "This is an ansible_id field intended to be used with source pointing to object_id, so, does conversion"
30-
31- def to_representation (self , value ):
32- "The source for this field is object_id, which is ignored, use content_object instead"
33- assignment = getattr (self , "_this_assignment" , None )
34- if not assignment :
35- return None
36- content_object = assignment .content_object
37- if isinstance (content_object , RemoteObject ):
38- return None
39- if hasattr (content_object , 'resource' ):
40- return str (content_object .resource .ansible_id )
41- return None
42-
43- def to_internal_value (self , value ):
44- "Targeting object_id, this converts ansible_id into object_id"
45- resource_cls = apps .get_model ('dab_resource_registry' , 'Resource' )
46- resource = resource_cls .objects .get (ansible_id = value )
47- return resource .object_id
48-
49-
5028assignment_common_fields = ('created' , 'created_by_ansible_id' , 'object_id' , 'object_ansible_id' , 'content_type' , 'role_definition' )
5129
5230
5331class BaseAssignmentSerializer (serializers .ModelSerializer ):
5432 content_type = serializers .SlugRelatedField (read_only = True , slug_field = 'api_slug' )
5533 role_definition = serializers .SlugRelatedField (slug_field = 'name' , queryset = RoleDefinition .objects .all ())
5634 created_by_ansible_id = ActorAnsibleIdField (source = 'created_by' , required = False , allow_null = True )
57- object_ansible_id = ObjectIDAnsibleIDField ( source = 'object_id' , required = False , allow_null = True )
35+ object_ansible_id = serializers . UUIDField ( read_only = True , allow_null = True )
5836 object_id = serializers .CharField (allow_blank = True , required = False , allow_null = True )
5937 from_service = serializers .CharField (write_only = True )
6038
61- def to_representation (self , instance ):
62- # hack to surface content_object for ObjectIDAnsibleIDField
63- self .fields ["object_ansible_id" ]._this_assignment = instance
64- return super ().to_representation (instance )
65-
6639 def get_created_by_ansible_id (self , obj ):
6740 return str (obj .created_by .resource .ansible_id )
6841
0 commit comments