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.

51 lines
1.7 KiB

3 years ago
3 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 ..models import Project
  10. from ..permissions import IsProjectAdmin
  11. from ..tasks import ingest_data
  12. from .upload.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 UploadAPI(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. 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(
  29. tu.upload_id,
  30. destination_file_path=os.path.join(tu.file.name, tu.upload_name)
  31. )
  32. for tu in tus
  33. ]
  34. filenames = [su.file.path for su in sus]
  35. task = ingest_data.delay(
  36. user_id=request.user.id,
  37. project_id=project_id,
  38. filenames=filenames,
  39. format=format,
  40. **request.data
  41. )
  42. return Response({'task_id': task.task_id})