Exemplo n.º 1
0
    def patch(self, request, pk):
        """ Update bucket

        Parameters:

            {
                "label": "<label>"
            }

        Args:

            request: HTTP request
            pk: ObjectId

        Returns:

            - code: 200
              content: Updated bucket
            - code: 400
              content: Validation error
            - code: 404
              content: Object was not found
            - code: 500
              content: Internal server error
        """
        try:
            # Get object
            bucket = self.get_object(pk)

            # Build serializer
            bucket_serializer = BucketSerializer(instance=bucket,
                                                 data=request.data,
                                                 partial=True)

            # Validate data
            bucket_serializer.is_valid(True)
            # Save data
            bucket_serializer.save(user=request.user)

            return Response(bucket_serializer.data, status=status.HTTP_200_OK)
        except ValidationError as validation_exception:
            content = {'message': validation_exception.detail}
            return Response(content, status=status.HTTP_400_BAD_REQUEST)
        except Http404:
            content = {'message': 'Bucket not found.'}
            return Response(content, status=status.HTTP_404_NOT_FOUND)
        except Exception as api_exception:
            content = {'message': api_exception.message}
            return Response(content,
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Exemplo n.º 2
0
    def post(self, request):
        """ Create a bucket

        Parameters:

            {
                "label": "label",
                "types": ["<type_version_manager_id>"]
            }

        Args:

            request: HTTP request

        Returns:

            - code: 201
              content: Created bucket
            - code: 400
              content: Validation error
            - code: 500
              content: Internal server error
        """
        try:
            # Build serializer
            bucket_serializer = BucketSerializer(data=request.data)

            # Validate data
            bucket_serializer.is_valid(True)
            # Save data
            bucket_serializer.save()

            # Return the serialized data
            return Response(bucket_serializer.data,
                            status=status.HTTP_201_CREATED)
        except ValidationError as validation_exception:
            content = {'message': validation_exception.detail}
            return Response(content, status=status.HTTP_400_BAD_REQUEST)
        except Exception as api_exception:
            content = {'message': api_exception.message}
            return Response(content,
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)