You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.6 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. import os
  2. from django.shortcuts import get_object_or_404
  3. from django_drf_filepond.api import store_upload
  4. from django_drf_filepond.models import TemporaryUpload
  5. from rest_framework import status
  6. from rest_framework.permissions import IsAuthenticated
  7. from rest_framework.response import Response
  8. from rest_framework.views import APIView
  9. from projects.models import Project
  10. from projects.permissions import IsProjectAdmin
  11. from .celery_tasks import import_dataset
  12. from .pipeline.catalog import Options
  13. class DatasetCatalog(APIView):
  14. permission_classes = [IsAuthenticated & IsProjectAdmin]
  15. def get(self, request, *args, **kwargs):
  16. project_id = kwargs["project_id"]
  17. project = get_object_or_404(Project, pk=project_id)
  18. options = Options.filter_by_task(project.project_type)
  19. return Response(data=options, status=status.HTTP_200_OK)
  20. class DatasetImportAPI(APIView):
  21. permission_classes = [IsAuthenticated & IsProjectAdmin]
  22. def post(self, request, *args, **kwargs):
  23. project_id = self.kwargs["project_id"]
  24. upload_ids = request.data.pop("uploadIds")
  25. file_format = request.data.pop("format")
  26. tus = [TemporaryUpload.objects.get(upload_id=upload_id) for upload_id in upload_ids]
  27. sus = [
  28. store_upload(tu.upload_id, destination_file_path=os.path.join(tu.file.name, tu.upload_name)) for tu in tus
  29. ]
  30. filenames = [su.file.path for su in sus]
  31. task = import_dataset.delay(
  32. user_id=request.user.id, project_id=project_id, filenames=filenames, file_format=file_format, **request.data
  33. )
  34. return Response({"task_id": task.task_id})