Merge pull request #1662 from AlanCoding/1407_credential_post_fields_rebased

Allow credentials to be created in UI touch up documentation for owners
This commit is contained in:
Alan Rominger 2016-04-29 12:33:59 -04:00
commit 293db554c5
2 changed files with 25 additions and 1 deletions

View File

@ -798,6 +798,7 @@ class OrganizationSerializer(BaseSerializer):
users = reverse('api:organization_users_list', args=(obj.pk,)),
admins = reverse('api:organization_admins_list', args=(obj.pk,)),
teams = reverse('api:organization_teams_list', args=(obj.pk,)),
credentials = reverse('api:organization_credential_list', args=(obj.pk,)),
activity_stream = reverse('api:organization_activity_stream_list', args=(obj.pk,)),
notifiers = reverse('api:organization_notifiers_list', args=(obj.pk,)),
notifiers_any = reverse('api:organization_notifiers_any_list', args=(obj.pk,)),
@ -1573,6 +1574,18 @@ 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
@ -1581,7 +1594,14 @@ class CredentialSerializer(BaseSerializer):
'ssh_key_data', 'ssh_key_unlock',
'become_method', 'become_username', 'become_password',
'vault_password', 'subscription', 'tenant', 'secret', 'client',
'authorize', 'authorize_password')
'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)
def build_standard_field(self, field_name, model_field):
field_class, field_kwargs = super(CredentialSerializer, self).build_standard_field(field_name, model_field)

View File

@ -1224,6 +1224,10 @@ class CredentialList(ListCreateAPIView):
serializer_class = CredentialSerializer
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)]:
request.data.pop(field)
kwargs.pop(field, None)
if not any([x in request.data for x in ['user', 'team', 'organization']]):
return Response({'detail': 'Missing user, team, or organization'}, status=status.HTTP_400_BAD_REQUEST)