예제 #1
0
 def get(self, *args, **kwargs):
     resource = self.read(self.resource_id)
     include_fields = self.include_parser.parse()
     sparse_fields = self.sparse_fields_parser.parse()
     try:
         data, errors = self.schema(include_data=include_fields,
                                    only=sparse_fields).dump(resource)
     except marshmallow.ValidationError as e:
         raise exceptions.JsonApiException(
             detail='marshmallow.ValidationError', source=e.messages)
     except (AttributeError, KeyError, ValueError) as e:
         logger.error('Error Processing Request',
                      extra={
                          'status_code': http.HTTPStatus.BAD_REQUEST,
                          'request': request,
                          'exception': e
                      })
         raise exceptions.JsonApiException(detail=str(e),
                                           source={'component': 'schema'})
     else:
         if errors:
             return response.JsonApiErrorResponse.from_marshmallow_errors(
                 errors)
         else:
             return response.JsonApiResponse(data)
예제 #2
0
 def prepare_response(self, data):
     id_map = {}
     kwargs = {'id_map': id_map}
     object = self.create(data=data, **kwargs)
     return response.JsonApiResponse(
         self.schema().dump(object, **kwargs).data,
         status=http.HTTPStatus.CREATED,
     )
예제 #3
0
 def get(self, *args, **kwargs):
     resource = self.read(self.resource_id)
     include_fields = self._check_include_fields()
     try:
         data, errors = self.schema(
             include_data=include_fields).dump(resource)
     except marshmallow.ValidationError as e:
         return response.JsonApiErrorResponse.from_marshmallow_errors(
             e.messages)
     else:
         if errors:
             return response.JsonApiErrorResponse.from_marshmallow_errors(
                 errors)
         else:
             return response.JsonApiResponse(data)
예제 #4
0
 def patch(self, *args, **kwargs):
     computed_schema = self.schema(partial=True)
     try:
         data, errors = computed_schema.load(request.get_json())
     except marshmallow.ValidationError as e:
         return response.JsonApiErrorResponse.from_marshmallow_errors(
             e.messages)
     else:
         if errors:
             return response.JsonApiErrorResponse.from_marshmallow_errors(
                 errors)
         else:
             resource = self.update(self.resource_id, data)
             if resource:
                 return response.JsonApiResponse(
                     computed_schema.dump(resource).data)
             else:
                 return response.EmptyResponse()
예제 #5
0
 def prepare_response(self, data):
     object = self.create(data=data)
     return response.JsonApiResponse(
         self.schema().dump(object).data,
         status=http.HTTPStatus.CREATED,
     )