def post(self, request): """ Create data Args: request: Returns: """ try: # Build serializer data_serializer = DataSerializer(data=request.data) # Validate data data_serializer.is_valid(True) # Save data data_serializer.save(user=request.user) # Return the serialized data return Response(data_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 exceptions.DoesNotExist: content = {'message': 'Template not found.'} 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)
def patch(self, request, pk): """ Update data Args: request: pk: Returns: """ try: # Get object data_object = self.get_object(request, pk) # Build serializer data_serializer = DataSerializer(instance=data_object, data=request.data, partial=True) # Validate data data_serializer.is_valid(True) # Save data data_serializer.save(user=request.user) return Response(data_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': 'Data 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)
def patch(self, request, pk): """ Update a Data Parameters: { "title": "new_title", "xml_content": "new_xml_content" } Args: request: HTTP request pk: ObjectId Returns: - code: 200 content: Updated data - code: 400 content: Validation error - code: 404 content: Object was not found - code: 500 content: Internal server error """ try: # Get object data_object = self.get_object(request, pk) # Build serializer data_serializer = DataSerializer(instance=data_object, data=request.data, partial=True) # Validate data data_serializer.is_valid(True) # Save data data_serializer.save(user=request.user) return Response(data_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': 'Data not found.'} return Response(content, status=status.HTTP_404_NOT_FOUND) except Exception as api_exception: content = {'message': str(api_exception)} return Response(content, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def post(self, request): """ Create a Data Parameters: { "title": "document_title", "template": "template_id", "xml_content": "document_content" } Args: request: HTTP request Returns: - code: 201 content: Created data - code: 400 content: Validation error - code: 404 content: Template was not found - code: 500 content: Internal server error """ try: # Build serializer data_serializer = DataSerializer(data=request.data) # Validate data data_serializer.is_valid(True) # Save data data_serializer.save(user=request.user) # Return the serialized data return Response(data_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 exceptions.DoesNotExist: content = {'message': 'Template not found.'} return Response(content, status=status.HTTP_404_NOT_FOUND) except Exception as api_exception: content = {'message': str(api_exception)} return Response(content, status=status.HTTP_500_INTERNAL_SERVER_ERROR)