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

Add validation for query params

parent d3823222
Pipeline #18488 passed with stages
in 2 minutes and 16 seconds
......@@ -57,6 +57,7 @@ drf-yasg = "*"
packaging = "*"
python-slugify = "*"
master = {git = "https://github.com/khillion/bioapi.git"}
marshmallow = "*"
[requires]
python_version = "3.7"
{
"_meta": {
"hash": {
"sha256": "f81cb1460e5f4b94a712ee3af4bd701917a32360529825962b4e146f0efbc9a4"
"sha256": "5998b6b97448fd635cc1b05787de28ac5ac3344ca6b8055831fca13790fc3f33"
},
"pipfile-spec": 6,
"requires": {
......@@ -170,6 +170,14 @@
],
"version": "==1.1.1"
},
"marshmallow": {
"hashes": [
"sha256:1a358beb89c2b4d5555272065a9533591a3eb02f1b854f3c4002d88d8f2a1ddb",
"sha256:eb97c42c5928b5720812c9268865fe863d4807bc1a8b48ddd7d5c9e1779a6af0"
],
"index": "pypi",
"version": "==3.2.2"
},
"master": {
"git": "https://github.com/khillion/bioapi.git",
"ref": "c01af30f9b60428c0d8d2aeae04e5adf1fadba36"
......
from marshmallow import Schema, fields
class FunctionRetrieveQueryParams(Schema):
detailed = fields.Boolean()
import logging
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 FunctionFilter
from metagenedb.api.catalog.qparams_validators.function import FunctionRetrieveQueryParams
from metagenedb.apps.catalog.models import Function
from metagenedb.apps.catalog.serializers import FunctionSerializer
from metagenedb.common.utils.external_api.togows import GetFunctionExternalInfo
......@@ -19,6 +22,9 @@ class FunctionViewSet(BulkViewSet):
lookup_field = 'function_id'
filterset_class = FunctionFilter
def _get_retrieve_qparams(self, raw_query_params):
return FunctionRetrieveQueryParams().load(raw_query_params)
def _get_external_info(self, db_data):
detailed_info_retriever = GetFunctionExternalInfo(db_data['function_id'],
db_data['source'])
......@@ -33,7 +39,11 @@ class FunctionViewSet(BulkViewSet):
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)
except ValidationError as validation_error:
return Response(validation_error.normalized_messages(), status=HTTP_422_UNPROCESSABLE_ENTITY)
returned_data = serializer.data
if request.query_params.get('detailed', None):
if query_params.get('detailed', False) is True:
returned_data = self._get_external_info(returned_data)
return Response(returned_data)
Markdown is supported
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