From f355ca4751fa13334af61b515a86fd3713f948f0 Mon Sep 17 00:00:00 2001 From: Hironsan Date: Thu, 8 Apr 2021 09:41:27 +0900 Subject: [PATCH] Add catalog and upload APIs --- app/api/views/upload/view.py | 42 ++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 app/api/views/upload/view.py diff --git a/app/api/views/upload/view.py b/app/api/views/upload/view.py new file mode 100644 index 00000000..1fec1596 --- /dev/null +++ b/app/api/views/upload/view.py @@ -0,0 +1,42 @@ +from django.shortcuts import get_object_or_404 +from django_drf_filepond.api import store_upload +from django_drf_filepond.models import TemporaryUpload +from rest_framework import status +from rest_framework.permissions import IsAuthenticated +from rest_framework.response import Response +from rest_framework.views import APIView + +from .catalog import Options +from ...models import Project +from ...permissions import IsProjectAdmin +from ...tasks import injest_data + + +class DatasetCatalog(APIView): + permission_classes = [IsAuthenticated & IsProjectAdmin] + + def get(self, request, *args, **kwargs): + project_id = request.query_params['project_id'] + project = get_object_or_404(Project, pk=project_id) + options = Options.filter_by_task(project.project_type) + return Response(data=options, status=status.HTTP_200_OK) + + +class UploadAPI(APIView): + permission_classes = [IsAuthenticated & IsProjectAdmin] + + def post(self, request, *args, **kwargs): + project_id = self.kwargs['project_id'] + upload_ids = request.data('upload_ids') + format = request.data('format') + + tus = [TemporaryUpload.objects.get(upload_id=upload_id) for upload_id in upload_ids] + sus = [store_upload(tu.upload_id, destination_file_path=tu.upload_name) for tu in tus] + filenames = [su.file.path for su in sus] + task = injest_data.delay( + user_id=request.user.id, + project_id=project_id, + filenames=filenames, + format=format + ) + return Response({'task_id': task.task_id})