Skip to content

Commit 5579fa4

Browse files
committed
Use gross hack to satisfy DRF
1 parent 2dc3d2a commit 5579fa4

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

ansible_base/rbac/service_api/serializers.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ def to_representation(self, obj):
3434
return str(content_object.resource.ansible_id)
3535
return None
3636

37+
def get_attribute(self, instance):
38+
"""Override to return the full instance instead of a specific attribute"""
39+
return instance
40+
3741
def to_internal_value(self, value):
3842
"""Convert object_ansible_id to object_id for internal use"""
3943
if not value:
@@ -71,7 +75,7 @@ class BaseAssignmentSerializer(serializers.ModelSerializer):
7175
content_type = serializers.SlugRelatedField(read_only=True, slug_field='api_slug')
7276
role_definition = serializers.SlugRelatedField(slug_field='name', queryset=RoleDefinition.objects.all())
7377
created_by_ansible_id = ActorAnsibleIdField(source='created_by', required=False, allow_null=True)
74-
object_ansible_id = ObjectAnsibleIdField(source='object_id', required=False, allow_null=True)
78+
object_ansible_id = ObjectAnsibleIdField(required=False, allow_null=True)
7579
object_id = serializers.CharField(allow_blank=True, required=False, allow_null=True)
7680
from_service = serializers.CharField(write_only=True)
7781

@@ -85,12 +89,16 @@ def validate(self, attrs):
8589
"""
8690
rd = attrs['role_definition']
8791
has_object_id = 'object_id' in attrs and attrs['object_id']
92+
has_object_ansible_id = 'object_ansible_id' in attrs and attrs['object_ansible_id']
8893

8994
if rd.content_type_id:
90-
if not self.partial and not has_object_id:
95+
if not self.partial and not has_object_id and not has_object_ansible_id:
9196
raise serializers.ValidationError("You must provide either 'object_id' or 'object_ansible_id'.")
97+
# If object_ansible_id was provided and converted, use that for object_id
98+
if has_object_ansible_id and not has_object_id:
99+
attrs['object_id'] = attrs['object_ansible_id']
92100
else:
93-
if has_object_id:
101+
if has_object_id or has_object_ansible_id:
94102
raise serializers.ValidationError("Can not provide either 'object_id' or 'object_ansible_id' for system role")
95103

96104
return super().validate(attrs)

0 commit comments

Comments
 (0)