From eee260812eb384df983976e101ff90af458c96a9 Mon Sep 17 00:00:00 2001 From: Mikko Nieminen Date: Wed, 11 Sep 2024 15:34:04 +0200 Subject: [PATCH] update api views for openapi support (#1951) --- landingzones/views_api.py | 13 +++++++++++++ samplesheets/views_api.py | 15 +++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/landingzones/views_api.py b/landingzones/views_api.py index 30c08918..f485a19f 100644 --- a/landingzones/views_api.py +++ b/landingzones/views_api.py @@ -1,6 +1,7 @@ """REST API views for the landingzones app""" import logging +import sys from django.urls import reverse @@ -15,6 +16,7 @@ from rest_framework.response import Response from rest_framework import status from rest_framework.serializers import ValidationError +from rest_framework.schemas.openapi import AutoSchema from rest_framework.versioning import AcceptHeaderVersioning from rest_framework.views import APIView @@ -138,6 +140,7 @@ class ZoneListAPIView( pagination_class = SODARPageNumberPagination permission_required = 'landingzones.view_zone_own' + schema = AutoSchema(operation_id_base='listZone') serializer_class = LandingZoneSerializer def get_queryset(self): @@ -187,6 +190,7 @@ class ZoneRetrieveAPIView( lookup_field = 'sodar_uuid' lookup_url_kwarg = 'landingzone' + schema = AutoSchema(operation_id_base='retrieveZone') serializer_class = LandingZoneSerializer def get_permission_required(self): @@ -300,6 +304,8 @@ class ZoneUpdateAPIView( def get_serializer_context(self, *args, **kwargs): context = super().get_serializer_context(*args, **kwargs) + if sys.argv[1:2] == ['generateschema']: + return context landing_zone = self.get_object() context['assay'] = landing_zone.assay.sodar_uuid return context @@ -385,6 +391,13 @@ class ZoneSubmitMoveAPIView(ZoneMoveMixin, ZoneSubmitBaseAPIView): **Methods:** ``POST`` """ + class ZoneSubmitMoveSchema(AutoSchema): + def get_operation_id_base(self, path, method, action): + if '/validate' in path: + return 'submitZoneValidate' + return 'submitZoneValidateMove' + + schema = ZoneSubmitMoveSchema() zone_action = 'move' def post(self, request, *args, **kwargs): diff --git a/samplesheets/views_api.py b/samplesheets/views_api.py index f2a35463..d0115329 100644 --- a/samplesheets/views_api.py +++ b/samplesheets/views_api.py @@ -2,6 +2,7 @@ import logging import re +import sys from irods.exception import CAT_NO_ROWS_FOUND from irods.models import DataObject @@ -27,6 +28,7 @@ from rest_framework.permissions import AllowAny, IsAuthenticated from rest_framework.renderers import JSONRenderer from rest_framework.response import Response +from rest_framework.schemas.openapi import AutoSchema from rest_framework.versioning import AcceptHeaderVersioning from rest_framework.views import APIView @@ -208,8 +210,15 @@ class SheetISAExportAPIView( **Methods:** ``GET`` """ + class SheetISAExportSchema(AutoSchema): + def get_operation_id_base(self, path, method, action): + if '/zip/' in path: + return 'retrieveSheetISAExportZip' + return 'retrieveSheetISAExportJSON' + http_method_names = ['get'] permission_required = 'samplesheets.export_sheet' + schema = SheetISAExportSchema() def get(self, request, *args, **kwargs): project = self.get_project() @@ -379,6 +388,7 @@ class IrodsAccessTicketRetrieveAPIView( lookup_field = 'sodar_uuid' lookup_url_kwarg = 'irodsaccessticket' permission_required = 'samplesheets.edit_sheet' + schema = AutoSchema(operation_id_base='retrieveIrodsAccessTicket') serializer_class = IrodsAccessTicketSerializer queryset_project_field = 'study__investigation__project' @@ -407,6 +417,7 @@ class IrodsAccessTicketListAPIView( pagination_class = SODARPageNumberPagination permission_required = 'samplesheets.edit_sheet' + schema = AutoSchema(operation_id_base='listIrodsAccessTicket') serializer_class = IrodsAccessTicketSerializer def get_queryset(self): @@ -448,6 +459,8 @@ class IrodsAccessTicketCreateAPIView( def get_serializer_context(self): context = super().get_serializer_context() + if sys.argv[1:2] == ['generateschema']: + return context context['project'] = self.get_project() context['user'] = self.request.user return context @@ -578,6 +591,7 @@ class IrodsDataRequestRetrieveAPIView( lookup_field = 'sodar_uuid' lookup_url_kwarg = 'irodsdatarequest' permission_required = 'samplesheets.edit_sheet' + schema = AutoSchema(operation_id_base='retrieveIrodsDataRequest') serializer_class = IrodsDataRequestSerializer @@ -608,6 +622,7 @@ class IrodsDataRequestListAPIView( pagination_class = SODARPageNumberPagination permission_required = 'samplesheets.edit_sheet' + schema = AutoSchema(operation_id_base='listIrodsDataRequest') serializer_class = IrodsDataRequestSerializer def get_queryset(self):