From 097c4505814a8a12d73ba5a45c9f86458ed05e5c Mon Sep 17 00:00:00 2001 From: AlanCoding Date: Wed, 4 May 2016 10:39:12 -0400 Subject: [PATCH] new serializer to remove owner fields from credential detail views --- awx/api/serializers.py | 53 ++++++++++++++++++++++++++---------------- awx/api/views.py | 2 +- 2 files changed, 34 insertions(+), 21 deletions(-) diff --git a/awx/api/serializers.py b/awx/api/serializers.py index d679029057..c6d7b4f016 100644 --- a/awx/api/serializers.py +++ b/awx/api/serializers.py @@ -1583,18 +1583,6 @@ class ResourceAccessListElementSerializer(UserSerializer): class CredentialSerializer(BaseSerializer): # FIXME: may want to make some fields filtered based on user accessing - user = serializers.PrimaryKeyRelatedField( - queryset=User.objects.all(), required=False, default=None, write_only=True, - help_text='Write-only field used to add user to owner role. If provided, ' - 'do not give either team or organization. Only valid for creation.') - team = serializers.PrimaryKeyRelatedField( - queryset=Team.objects.all(), required=False, default=None, write_only=True, - help_text='Write-only field used to add team to owner role. If provided, ' - 'do not give either user or organization. Only valid for creation.') - organization = serializers.PrimaryKeyRelatedField( - queryset=Organization.objects.all(), required=False, default=None, write_only=True, - help_text='Write-only field used to add organization to owner role. If provided, ' - 'do not give either team or team. Only valid for creation.') class Meta: model = Credential @@ -1603,14 +1591,7 @@ class CredentialSerializer(BaseSerializer): 'ssh_key_data', 'ssh_key_unlock', 'become_method', 'become_username', 'become_password', 'vault_password', 'subscription', 'tenant', 'secret', 'client', - 'authorize', 'authorize_password', - 'user', 'team', 'organization') - - def create(self, validated_data): - # Remove the user, team, and organization processed in view - for field in ['user', 'team', 'organization']: - validated_data.pop(field, None) - return super(CredentialSerializer, self).create(validated_data) + 'authorize', 'authorize_password') def build_standard_field(self, field_name, model_field): field_class, field_kwargs = super(CredentialSerializer, self).build_standard_field(field_name, model_field) @@ -1647,6 +1628,38 @@ class CredentialSerializer(BaseSerializer): return summary_dict +class CredentialSerializerCreate(CredentialSerializer): + + user = serializers.PrimaryKeyRelatedField( + queryset=User.objects.all(), required=False, default=None, write_only=True, + help_text='Write-only field used to add user to owner role. If provided, ' + 'do not give either team or organization. Only valid for creation.') + team = serializers.PrimaryKeyRelatedField( + queryset=Team.objects.all(), required=False, default=None, write_only=True, + help_text='Write-only field used to add team to owner role. If provided, ' + 'do not give either user or organization. Only valid for creation.') + organization = serializers.PrimaryKeyRelatedField( + queryset=Organization.objects.all(), required=False, default=None, write_only=True, + help_text='Write-only field used to add organization to owner role. If provided, ' + 'do not give either team or team. Only valid for creation.') + + class Meta: + model = Credential + fields = ('*', 'user', 'team', 'organization') + + def create(self, validated_data): + ''' + Special cases are processed for creating a credential. Fields of + user, team, and organization are allowed, and if given, roles are + automatically created for these. This is only used for list-create + view. + ''' + # Remove the user, team, and organization processed in view + for field in ['user', 'team', 'organization']: + validated_data.pop(field, None) + return super(CredentialSerializer, self).create(validated_data) + + class JobOptionsSerializer(BaseSerializer): class Meta: diff --git a/awx/api/views.py b/awx/api/views.py index aa476f18c6..8ed1c30aed 100644 --- a/awx/api/views.py +++ b/awx/api/views.py @@ -1228,7 +1228,7 @@ class UserAccessList(ResourceAccessList): class CredentialList(ListCreateAPIView): model = Credential - serializer_class = CredentialSerializer + serializer_class = CredentialSerializerCreate def post(self, request, *args, **kwargs): for field in [x for x in ['user', 'team', 'organization'] if x in request.data and request.data[x] in ('', None)]: