From e26fc5677bc743df1417c83d2a72d961d13b7a53 Mon Sep 17 00:00:00 2001
From: Kenzo-Hugo Hillion <kenzo-hugo.hillion1@pasteur.fr>
Date: Thu, 9 Apr 2020 16:15:54 +0200
Subject: [PATCH] Add more documentation for metagenedb API

---
 backend/metagenedb/api/catalog/urls.py        |  2 +-
 backend/metagenedb/api/catalog/views/base.py  | 13 ++-
 .../metagenedb/api/catalog/views/function.py  | 88 +++++++++++++++++++
 backend/metagenedb/api/catalog/views/gene.py  | 32 +++++++
 .../api/catalog/views/statistics.py           | 14 ++-
 .../metagenedb/api/catalog/views/taxonomy.py  | 32 +++++++
 6 files changed, 175 insertions(+), 6 deletions(-)

diff --git a/backend/metagenedb/api/catalog/urls.py b/backend/metagenedb/api/catalog/urls.py
index 043fbbd..fa35c70 100644
--- a/backend/metagenedb/api/catalog/urls.py
+++ b/backend/metagenedb/api/catalog/urls.py
@@ -4,7 +4,7 @@ from rest_framework.routers import DefaultRouter, DynamicRoute, Route
 
 from metagenedb.api.catalog import views
 
-from metagenedb.api.catalog.views.celery_test import celery_test_view, test_task_view
+# from metagenedb.api.catalog.views.celery_test import celery_test_view, test_task_view
 
 
 class CustomRouter(DefaultRouter):
diff --git a/backend/metagenedb/api/catalog/views/base.py b/backend/metagenedb/api/catalog/views/base.py
index 62e2f0e..dbd21b2 100644
--- a/backend/metagenedb/api/catalog/views/base.py
+++ b/backend/metagenedb/api/catalog/views/base.py
@@ -1,13 +1,22 @@
 from marshmallow.exceptions import ValidationError
 from rest_framework import status
+from rest_framework.mixins import (
+    CreateModelMixin, ListModelMixin, RetrieveModelMixin, UpdateModelMixin
+)
 from rest_framework.response import Response
-from rest_framework.viewsets import ModelViewSet
+from rest_framework.viewsets import GenericViewSet
 
 from metagenedb.common.django_default.permissions import ListAndRetrieveAll
 from metagenedb.common.django_default.qparams_validators import PaginatedQueryParams
 
 
-class QParamsValidationViewSet(ModelViewSet):
+class QParamsValidationViewSet(
+    CreateModelMixin,
+    ListModelMixin,
+    RetrieveModelMixin,
+    UpdateModelMixin,
+    GenericViewSet
+):
     query_params_parser = PaginatedQueryParams
 
     def _get_qparams(self, raw_query_params):
diff --git a/backend/metagenedb/api/catalog/views/function.py b/backend/metagenedb/api/catalog/views/function.py
index ce21c8d..207efde 100644
--- a/backend/metagenedb/api/catalog/views/function.py
+++ b/backend/metagenedb/api/catalog/views/function.py
@@ -1,5 +1,6 @@
 import logging
 
+from drf_yasg.utils import swagger_auto_schema
 from marshmallow.exceptions import ValidationError
 from rest_framework.response import Response
 from rest_framework.status import HTTP_422_UNPROCESSABLE_ENTITY
@@ -25,6 +26,36 @@ class FunctionViewSet(BulkViewSet):
     filterset_class = FunctionFilter
     query_params_parser = FunctionQueryParams
 
+    @swagger_auto_schema(
+        tags=['Functions'],
+    )
+    def list(self, *args, **kwargs):
+        super().list(*args, **kwargs)
+
+    @swagger_auto_schema(
+        tags=['Functions'],
+    )
+    def retrieve(self, *args, **kwargs):
+        super().list(*args, **kwargs)
+
+    @swagger_auto_schema(
+        tags=['Functions'],
+    )
+    def create(self, *args, **kwargs):
+        super().list(*args, **kwargs)
+
+    @swagger_auto_schema(
+        tags=['Functions'],
+    )
+    def update(self, *args, **kwargs):
+        super().list(*args, **kwargs)
+
+    @swagger_auto_schema(
+        tags=['Functions'],
+    )
+    def partial_update(self, *args, **kwargs):
+        super().list(*args, **kwargs)
+
 
 class KeggOrthologyViewSet(QParamsValidationViewSet):
     queryset = KeggOrthology.objects.all()
@@ -42,6 +73,9 @@ class KeggOrthologyViewSet(QParamsValidationViewSet):
             detailed_data = db_data
         return detailed_data
 
+    @swagger_auto_schema(
+        tags=['KEGG'],
+    )
     def retrieve(self, request, *args, **kwargs):
         try:
             query_params = self.query_params_parser().load(request.query_params)
@@ -58,6 +92,30 @@ class KeggOrthologyViewSet(QParamsValidationViewSet):
             returned_data = self._get_external_info(returned_data)
         return Response(returned_data)
 
+    @swagger_auto_schema(
+        tags=['KEGG'],
+    )
+    def list(self, *args, **kwargs):
+        super().list(*args, **kwargs)
+
+    @swagger_auto_schema(
+        tags=['KEGG'],
+    )
+    def create(self, *args, **kwargs):
+        super().list(*args, **kwargs)
+
+    @swagger_auto_schema(
+        tags=['KEGG'],
+    )
+    def update(self, *args, **kwargs):
+        super().list(*args, **kwargs)
+
+    @swagger_auto_schema(
+        tags=['KEGG'],
+    )
+    def partial_update(self, *args, **kwargs):
+        super().list(*args, **kwargs)
+
 
 class EggNOGViewSet(QParamsValidationViewSet):
     queryset = EggNOG.objects.all()
@@ -65,3 +123,33 @@ class EggNOGViewSet(QParamsValidationViewSet):
     lookup_field = 'function_id'
     filterset_class = EggNOGFilter
     query_params_parser = EggNOGQueryParams
+
+    @swagger_auto_schema(
+        tags=['EggNOG'],
+    )
+    def list(self, *args, **kwargs):
+        super().list(*args, **kwargs)
+
+    @swagger_auto_schema(
+        tags=['EggNOG'],
+    )
+    def retrieve(self, *args, **kwargs):
+        super().list(*args, **kwargs)
+
+    @swagger_auto_schema(
+        tags=['EggNOG'],
+    )
+    def create(self, *args, **kwargs):
+        super().list(*args, **kwargs)
+
+    @swagger_auto_schema(
+        tags=['EggNOG'],
+    )
+    def update(self, *args, **kwargs):
+        super().list(*args, **kwargs)
+
+    @swagger_auto_schema(
+        tags=['EggNOG'],
+    )
+    def partial_update(self, *args, **kwargs):
+        super().list(*args, **kwargs)
diff --git a/backend/metagenedb/api/catalog/views/gene.py b/backend/metagenedb/api/catalog/views/gene.py
index 3d73654..04fd2b4 100644
--- a/backend/metagenedb/api/catalog/views/gene.py
+++ b/backend/metagenedb/api/catalog/views/gene.py
@@ -1,3 +1,5 @@
+from drf_yasg.utils import swagger_auto_schema
+
 from metagenedb.apps.catalog.models import Gene
 from metagenedb.api.catalog.filters import GeneFilter
 from metagenedb.api.catalog.qparams_validators.gene import GeneQueryParams
@@ -12,3 +14,33 @@ class GeneViewSet(BulkViewSet):
     filterset_class = GeneFilter
     query_params_parser = GeneQueryParams
     lookup_field = 'gene_id'
+
+    @swagger_auto_schema(
+        tags=['Genes'],
+    )
+    def list(self, *args, **kwargs):
+        super().list(*args, **kwargs)
+
+    @swagger_auto_schema(
+        tags=['Genes'],
+    )
+    def retrieve(self, *args, **kwargs):
+        super().list(*args, **kwargs)
+
+    @swagger_auto_schema(
+        tags=['Genes'],
+    )
+    def create(self, *args, **kwargs):
+        super().list(*args, **kwargs)
+
+    @swagger_auto_schema(
+        tags=['Genes'],
+    )
+    def update(self, *args, **kwargs):
+        super().list(*args, **kwargs)
+
+    @swagger_auto_schema(
+        tags=['Genes'],
+    )
+    def partial_update(self, *args, **kwargs):
+        super().list(*args, **kwargs)
diff --git a/backend/metagenedb/api/catalog/views/statistics.py b/backend/metagenedb/api/catalog/views/statistics.py
index f444816..f0e3cfd 100644
--- a/backend/metagenedb/api/catalog/views/statistics.py
+++ b/backend/metagenedb/api/catalog/views/statistics.py
@@ -1,4 +1,5 @@
 from django.shortcuts import get_object_or_404
+from drf_yasg.utils import swagger_auto_schema
 from rest_framework import viewsets
 from rest_framework.response import Response
 
@@ -7,16 +8,23 @@ from metagenedb.apps.catalog.serializers import StatisticsSerializer
 
 
 class StatisticsViewSet(viewsets.ViewSet):
-    """
-    API View that list or retrieve pre-computed statistics from the catalog.
-    """
     lookup_field = 'stats_id'
 
+    @swagger_auto_schema(
+        operation_description="List all pre-computed statistics",
+        operation_summary="API to list all pre-computed statistics.",
+        tags=['Statistics'],
+    )
     def list(self, request):
         queryset = Statistics.objects.all()
         serializer = StatisticsSerializer(queryset, many=True)
         return Response(serializer.data)
 
+    @swagger_auto_schema(
+        operation_description="Retrieve pre-computed statistics from an ID",
+        operation_summary="API to retrieve pre-computed statistics.",
+        tags=['Statistics'],
+    )
     def retrieve(self, request, stats_id=None):
         queryset = Statistics.objects.all()
         stats = get_object_or_404(queryset, stats_id=stats_id)
diff --git a/backend/metagenedb/api/catalog/views/taxonomy.py b/backend/metagenedb/api/catalog/views/taxonomy.py
index a05eeb5..86ed8ff 100644
--- a/backend/metagenedb/api/catalog/views/taxonomy.py
+++ b/backend/metagenedb/api/catalog/views/taxonomy.py
@@ -1,3 +1,5 @@
+from drf_yasg.utils import swagger_auto_schema
+
 from metagenedb.api.catalog.filters import TaxonomyFilter
 from metagenedb.api.catalog.qparams_validators.taxonomy import TaxonomyQueryParams
 from metagenedb.apps.catalog.models import Taxonomy
@@ -13,3 +15,33 @@ class TaxonomyViewSet(BulkViewSet):
     lookup_field = 'tax_id'
     filterset_class = TaxonomyFilter
     query_params_parser = TaxonomyQueryParams
+
+    @swagger_auto_schema(
+        tags=['Taxonomy'],
+    )
+    def list(self, *args, **kwargs):
+        super().list(*args, **kwargs)
+
+    @swagger_auto_schema(
+        tags=['Taxonomy'],
+    )
+    def retrieve(self, *args, **kwargs):
+        super().list(*args, **kwargs)
+
+    @swagger_auto_schema(
+        tags=['Taxonomy'],
+    )
+    def create(self, *args, **kwargs):
+        super().list(*args, **kwargs)
+
+    @swagger_auto_schema(
+        tags=['Taxonomy'],
+    )
+    def update(self, *args, **kwargs):
+        super().list(*args, **kwargs)
+
+    @swagger_auto_schema(
+        tags=['Taxonomy'],
+    )
+    def partial_update(self, *args, **kwargs):
+        super().list(*args, **kwargs)
-- 
GitLab