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

add validation of qparams for all bulkviewset views

parent 81641e78
Pipeline #18549 passed with stages
in 2 minutes and 23 seconds
from marshmallow import Schema
class EmptyQueryParams(Schema):
pass
from marshmallow import Schema, fields
class FunctionRetrieveQueryParams(Schema):
class FunctionQueryParams(Schema):
detailed = fields.Boolean()
from marshmallow import Schema, fields
class TaxonomyQueryParams(Schema):
rank = fields.String()
name = fields.String()
from marshmallow.exceptions import ValidationError
from rest_framework import status
from rest_framework.viewsets import ModelViewSet
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from metagenedb.api.catalog.qparams_validators.empty import EmptyQueryParams
class BulkViewSet(ModelViewSet):
query_params_parser = EmptyQueryParams
def get_objects(self, instance_ids):
return self.queryset.in_bulk(instance_ids, field_name=self.lookup_field)
def _get_qparams(self, raw_query_params):
return self.query_params_parser().load(raw_query_params)
def _created_payload(self, serializer, request):
if isinstance(request.data, list):
return {
......@@ -79,3 +86,27 @@ class BulkViewSet(ModelViewSet):
self._updated_payload(create_serializer, update_serializer, request),
status=status.HTTP_201_CREATED, headers=headers
)
def list(self, request, *args, **kwargs):
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)
queryset = self.filter_queryset(self.get_queryset())
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
return self.get_paginated_response(serializer.data)
serializer = self.get_serializer(queryset, many=True)
return Response(serializer.data)
def retrieve(self, request, *args, **kwargs):
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)
instance = self.get_object()
serializer = self.get_serializer(instance)
return Response(serializer.data)
......@@ -5,7 +5,7 @@ from rest_framework.response import Response
from rest_framework.status import HTTP_422_UNPROCESSABLE_ENTITY
from metagenedb.api.catalog.filters import FunctionFilter
from metagenedb.api.catalog.qparams_validators.function import FunctionRetrieveQueryParams
from metagenedb.api.catalog.qparams_validators.function import FunctionQueryParams
from metagenedb.apps.catalog.models import Function
from metagenedb.apps.catalog.serializers import FunctionSerializer
from metagenedb.common.utils.external_api.togows import GetFunctionExternalInfo
......@@ -21,9 +21,7 @@ class FunctionViewSet(BulkViewSet):
serializer_class = FunctionSerializer
lookup_field = 'function_id'
filterset_class = FunctionFilter
def _get_retrieve_qparams(self, raw_query_params):
return FunctionRetrieveQueryParams().load(raw_query_params)
query_params_parser = FunctionQueryParams
def _get_external_info(self, db_data):
detailed_info_retriever = GetFunctionExternalInfo(db_data['function_id'],
......@@ -37,12 +35,12 @@ class FunctionViewSet(BulkViewSet):
return detailed_data
def retrieve(self, request, *args, **kwargs):
instance = self.get_object()
serializer = self.get_serializer(instance)
try:
query_params = self._get_retrieve_qparams(request.query_params)
query_params = self._get_qparams(request.query_params)
except ValidationError as validation_error:
return Response(validation_error.normalized_messages(), status=HTTP_422_UNPROCESSABLE_ENTITY)
instance = self.get_object()
serializer = self.get_serializer(instance)
returned_data = serializer.data
if query_params.get('detailed', False) is True:
returned_data = self._get_external_info(returned_data)
......
from marshmallow.exceptions import ValidationError
from rest_framework.response import Response
from rest_framework.status import HTTP_422_UNPROCESSABLE_ENTITY
from metagenedb.api.catalog.filters import TaxonomyFilter
from metagenedb.api.catalog.qparams_validators.taxonomy import TaxonomyQueryParams
from metagenedb.apps.catalog.models import Taxonomy
from metagenedb.apps.catalog.serializers import TaxonomySerializer
......@@ -13,8 +16,13 @@ class TaxonomyViewSet(BulkViewSet):
serializer_class = TaxonomySerializer
lookup_field = 'tax_id'
filterset_class = TaxonomyFilter
query_params_parser = TaxonomyQueryParams
def retrieve(self, request, *args, **kwargs):
try:
query_params = self._get_qparams(request.query_params) # noqa
except ValidationError as validation_error:
return Response(validation_error.normalized_messages(), status=HTTP_422_UNPROCESSABLE_ENTITY)
instance = self.get_object()
hierarchy = instance.parental_hierarchy # noqa
serializer = self.get_serializer(instance)
......
......@@ -5,7 +5,6 @@ import sys
from itertools import islice
from bioapi import MetageneDBCatalogTaxonomyAPI
from requests.exceptions import HTTPError
from metagenedb.common.utils.parsers import NCBITaxonomyLineParser
......
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