Skip to content

Commit

Permalink
Move _tus_chunk_action to view_utils
Browse files Browse the repository at this point in the history
  • Loading branch information
SpecLad committed Apr 5, 2023
1 parent e28d9b1 commit 197461e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
21 changes: 21 additions & 0 deletions cvat/apps/engine/view_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
33 changes: 7 additions & 26 deletions cvat/apps/engine/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

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

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

0 comments on commit 197461e

Please sign in to comment.