Commit d8c73868 authored by Kenzo-Hugo Hillion's avatar Kenzo-Hugo Hillion
Browse files

Return allowed query params for 422

parent 6cf1eabb
from marshmallow import Schema, fields
class GeneLengthQueryParams(Schema):
window_size = fields.Integer()
......@@ -91,7 +91,11 @@ class BulkViewSet(ModelViewSet):
try:
query_params = self._get_qparams(request.query_params) # noqa
except ValidationError as validation_error:
return Response(validation_error.normalized_messages(), status=status.HTTP_422_UNPROCESSABLE_ENTITY)
error_message = validation_error.normalized_messages()
error_message.update({
'allowed_query_params': ', '.join(self.query_params_parser().declared_fields.keys())
})
return Response(error_message, status=status.HTTP_422_UNPROCESSABLE_ENTITY)
queryset = self.filter_queryset(self.get_queryset())
page = self.paginate_queryset(queryset)
......@@ -106,7 +110,11 @@ class BulkViewSet(ModelViewSet):
try:
query_params = self._get_qparams(request.query_params) # noqa
except ValidationError as validation_error:
return Response(validation_error.normalized_messages(), status=status.HTTP_422_UNPROCESSABLE_ENTITY)
error_message = validation_error.normalized_messages()
error_message.update({
'allowed_query_params': ', '.join(self.query_params_parser().declared_fields.keys())
})
return Response(error_message, status=status.HTTP_422_UNPROCESSABLE_ENTITY)
instance = self.get_object()
serializer = self.get_serializer(instance)
return Response(serializer.data)
......@@ -38,7 +38,11 @@ class FunctionViewSet(BulkViewSet):
try:
query_params = self._get_qparams(request.query_params)
except ValidationError as validation_error:
return Response(validation_error.normalized_messages(), status=HTTP_422_UNPROCESSABLE_ENTITY)
error_message = validation_error.normalized_messages()
error_message.update({
'allowed_query_params': ', '.join(self.query_params_parser().declared_fields.keys())
})
return Response(error_message, status=HTTP_422_UNPROCESSABLE_ENTITY)
instance = self.get_object()
serializer = self.get_serializer(instance)
returned_data = serializer.data
......
import pandas as pd
from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema
from rest_framework import filters, status
from marshmallow.exceptions import ValidationError
from rest_framework import filters
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.status import HTTP_204_NO_CONTENT, HTTP_422_UNPROCESSABLE_ENTITY
from metagenedb.common.utils.df_operations import get_mask
from metagenedb.apps.catalog.models import Gene
from metagenedb.api.catalog.qparams_validators.gene import GeneLengthQueryParams
from metagenedb.apps.catalog.serializers import GeneSerializer
from .bulk_viewset import BulkViewSet
......@@ -82,13 +85,21 @@ class GeneViewSet(BulkViewSet):
)
@action(methods=['get'], detail=False)
def gene_length(self, request, window_size=10000):
if 'window_size' in request.query_params:
window_size = int(request.query_params.get('window_size'))
try:
query_params = GeneLengthQueryParams().load(request.query_params)
except ValidationError as validation_error:
error_message = validation_error.normalized_messages()
error_message.update({
'allowed_query_params': ', '.join(GeneLengthQueryParams().declared_fields.keys())
})
return Response(error_message, status=HTTP_422_UNPROCESSABLE_ENTITY)
if 'window_size' in query_params:
window_size = query_params.get('window_size')
df = pd.DataFrame(list(self.queryset.values(self.GENE_LENGTH_COL)))
if df.empty:
return Response(
{'results': {}},
status=status.HTTP_204_NO_CONTENT
status=HTTP_204_NO_CONTENT
)
return Response(
{'results': self._count_windows(df, window_size)}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment