From 197461ebfad743e20584980cfbea1e7d38309b39 Mon Sep 17 00:00:00 2001 From: Roman Donchenko Date: Wed, 5 Apr 2023 21:15:02 +0300 Subject: [PATCH] Move _tus_chunk_action to view_utils --- cvat/apps/engine/view_utils.py | 21 +++++++++++++++++++++ cvat/apps/engine/views.py | 33 +++++++-------------------------- 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/cvat/apps/engine/view_utils.py b/cvat/apps/engine/view_utils.py index 28a3b167db0b..9c36d94c2715 100644 --- a/cvat/apps/engine/view_utils.py +++ b/cvat/apps/engine/view_utils.py @@ -15,6 +15,7 @@ from rest_framework.reverse import reverse as _reverse from rest_framework.serializers import Serializer from rest_framework.viewsets import GenericViewSet +from drf_spectacular.utils import extend_schema def make_paginated_response( @@ -103,3 +104,23 @@ def list_action(serializer_class: Type[Serializer], **kwargs): params.update(kwargs) return action(**params) + +def tus_chunk_action(*, detail: bool, suffix_base: str): + def decorator(f): + f = action(detail=detail, methods=['HEAD', 'PATCH'], + url_path=f'{suffix_base}/{UploadMixin.file_id_regex}', + parser_classes=[TusUploadParser], + serializer_class=None, + )(f) + + # tus chunk endpoints are never accessed directly (the client must + # access them by following the Location header from the response to + # the creation endpoint). Moreover, the details of how these endpoints + # work are already described by the tus specification. Since we don't + # need to document either where these points are or how they work, + # they don't need to be in the schema. + f = extend_schema(exclude=True)(f) + + return f + + return decorator diff --git a/cvat/apps/engine/views.py b/cvat/apps/engine/views.py index a58dc4da653a..49cbcb9329c1 100644 --- a/cvat/apps/engine/views.py +++ b/cvat/apps/engine/views.py @@ -80,29 +80,10 @@ TaskPermission, UserPermission) from cvat.apps.engine.cache import MediaCache from cvat.apps.events.handlers import handle_annotations_patch +from cvat.apps.engine.view_utils import tus_chunk_action _UPLOAD_PARSER_CLASSES = api_settings.DEFAULT_PARSER_CLASSES + [MultiPartParser] -def _tus_chunk_action(*, detail: bool, suffix_base: str): - def decorator(f): - f = action(detail=detail, methods=['HEAD', 'PATCH'], - url_path=f'{suffix_base}/{UploadMixin.file_id_regex}', - parser_classes=[TusUploadParser], - serializer_class=None, - )(f) - - # tus chunk endpoints are never accessed directly (the client must - # access them by following the Location header from the response to - # the creation endpoint). Moreover, the details of how these endpoints - # work are already described by the tus specification. Since we don't - # need to document either where these points are or how they work, - # they don't need to be in the schema. - f = extend_schema(exclude=True)(f) - - return f - - return decorator - @extend_schema(tags=['server']) class ServerViewSet(viewsets.ViewSet): serializer_class = None @@ -382,7 +363,7 @@ def dataset(self, request, pk): callback=dm.views.export_project_as_dataset ) - @_tus_chunk_action(detail=True, suffix_base="dataset") + @tus_chunk_action(detail=True, suffix_base="dataset") def append_dataset_chunk(self, request, pk, file_id): self._object = self.get_object() return self.append_tus_chunk(request, file_id) @@ -519,7 +500,7 @@ def export_backup(self, request, pk=None): def import_backup(self, request, pk=None): return self.deserialize(request, backup.import_project) - @_tus_chunk_action(detail=False, suffix_base="backup") + @tus_chunk_action(detail=False, suffix_base="backup") def append_backup_chunk(self, request, file_id): return self.append_tus_chunk(request, file_id) @@ -742,7 +723,7 @@ def get_queryset(self): def import_backup(self, request, pk=None): return self.deserialize(request, backup.import_task) - @_tus_chunk_action(detail=False, suffix_base="backup") + @tus_chunk_action(detail=False, suffix_base="backup") def append_backup_chunk(self, request, file_id): return self.append_tus_chunk(request, file_id) @@ -933,7 +914,7 @@ def data(self, request, pk): return data_getter(request, self._object.data.start_frame, self._object.data.stop_frame, self._object.data) - @_tus_chunk_action(detail=True, suffix_base="data") + @tus_chunk_action(detail=True, suffix_base="data") def append_data_chunk(self, request, pk, file_id): self._object = self.get_object() return self.append_tus_chunk(request, file_id) @@ -1083,7 +1064,7 @@ def annotations(self, request, pk): return Response(data=str(e), status=status.HTTP_400_BAD_REQUEST) return Response(data) - @_tus_chunk_action(detail=True, suffix_base="annotations") + @tus_chunk_action(detail=True, suffix_base="annotations") def append_annotations_chunk(self, request, pk, file_id): self._object = self.get_object() return self.append_tus_chunk(request, file_id) @@ -1473,7 +1454,7 @@ def annotations(self, request, pk): return Response(data) - @_tus_chunk_action(detail=True, suffix_base="annotations") + @tus_chunk_action(detail=True, suffix_base="annotations") def append_annotations_chunk(self, request, pk, file_id): self._object = self.get_object() return self.append_tus_chunk(request, file_id)