def build_response(self, data_list): """ Build the response. Args: data_list: List of data Returns: The response paginated """ if 'all' in self.request.data and to_bool(self.request.data['all']): # Serialize data list data_serializer = DataSerializer(data_list, many=True) # Return response return Response(data_serializer.data) else: # Get paginator paginator = StandardResultsSetPagination() # Get requested page from list of results page = paginator.paginate_queryset(data_list, self.request) # Serialize page data_serializer = DataSerializer(page, many=True) # Return paginated response return paginator.get_paginated_response(data_serializer.data)
def get(self, request, pk): """ Retrieve a data Args: request: HTTP request pk: ObjectId Returns: - code: 200 content: Data - code: 404 content: Object was not found - code: 500 content: Internal server error """ try: # Get object data_object = self.get_object(request, pk) # Serialize object serializer = DataSerializer(data_object) # Return response return Response(serializer.data) 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 get(self, request): """ Get all user data. Query Params: template: template id title: title Args: request: Returns: """ try: # Get object data_object_list = data_api.get_all_by_user(request.user) # Apply filters template = self.request.query_params.get('template', None) if template is not None: data_object_list = data_object_list.filter(template=template) title = self.request.query_params.get('title', None) if title is not None: data_object_list = data_object_list.filter(title=title) # Serialize object data_serializer = DataSerializer(data_object_list, many=True) # Return response return Response(data_serializer.data, status=status.HTTP_200_OK) except Exception as api_exception: content = {'message': api_exception.message} return Response(content, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def get(self, request, pk): """ Retrieve data Args: request: pk: Returns: """ try: # Get object data_object = self.get_object(request, pk) # Serialize object serializer = DataSerializer(data_object) # Return response return Response(serializer.data) 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 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 get(self, request): """ Get all user Data Url Parameters: template: template_id title: document_title Examples: ../data/ ../data?template=[template_id] ../data?title=[document_title] ../data?template=[template_id]&title=[document_title] Args: request: HTTP request Returns: - code: 200 content: List of data - code: 500 content: Internal server error """ try: # Get object data_object_list = data_api.get_all_by_user(request.user) # Apply filters template = self.request.query_params.get('template', None) if template is not None: data_object_list = data_object_list.filter(template=template) title = self.request.query_params.get('title', None) if title is not None: data_object_list = data_object_list.filter(title=title) # Serialize object data_serializer = DataSerializer(data_object_list, many=True) # Return response return Response(data_serializer.data, status=status.HTTP_200_OK) except Exception as api_exception: content = {'message': str(api_exception)} return Response(content, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def get(self, request, provider, record): """Retrieve the local data of a given handle record Args: request: provider: record: Returns: """ id_provider = self.provider_manager.get(provider) provider_response = id_provider.get(record) try: query_result = get_data_by_pid( json.loads(provider_response.content)["url"], request.user ) return Response( DataSerializer(query_result).data, status=status.HTTP_200_OK ) except DoesNotExist: # Try to find PID in blobs try: query_result = get_blob_by_pid( json.loads(provider_response.content)["url"], request.user ) return get_file_http_response(query_result.blob, query_result.filename) except AccessControlError as e: content = {"message": str(e)} return Response(content, status=status.HTTP_403_FORBIDDEN) except DoesNotExist: content = { "status": "error", "code": status.HTTP_404_NOT_FOUND, "message": "No document with specified handle found", } return Response(content, status=status.HTTP_404_NOT_FOUND) except Exception as ex: content = { "status": "error", "code": status.HTTP_500_INTERNAL_SERVER_ERROR, "message": str(ex), } return Response(content, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def get(self, request, pk): """ Get all user Data Url Parameters: template: template_id title: document_title Examples: ../data/ ../data?template=[template_id] ../data?title=[document_title] ../data?template=[template_id]&title=[document_title] Args: request: HTTP request pk: WorkspaceId Returns: - code: 200 content: List of data - code: 500 content: Internal server error """ try: # Get object workspace_object = workspace_api.get_by_id(pk) #data_object_list = data_api.get_all_by_user(request.user) data_object_list = data_api.get_all_by_workspace( workspace_object, request.user) #data_object_list = data_object_list.filter(workspace=pk) # Serialize object data_serializer = DataSerializer(data_object_list, many=True) # Return response return Response(data_serializer.data, status=status.HTTP_200_OK) 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)