Skip to content

Commit

Permalink
update api views for openapi support (#1951)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkonie committed Sep 11, 2024
1 parent 0875cd4 commit eee2608
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
13 changes: 13 additions & 0 deletions landingzones/views_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""REST API views for the landingzones app"""

import logging
import sys

from django.urls import reverse

Expand All @@ -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

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
15 changes: 15 additions & 0 deletions samplesheets/views_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import logging
import re
import sys

from irods.exception import CAT_NO_ROWS_FOUND
from irods.models import DataObject
Expand All @@ -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

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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'

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit eee2608

Please sign in to comment.