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.

44 lines
1.6 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. from celery.result import AsyncResult
  2. from django.http import FileResponse
  3. from django.shortcuts import get_object_or_404
  4. from rest_framework import status
  5. from rest_framework.permissions import IsAuthenticated
  6. from rest_framework.response import Response
  7. from rest_framework.views import APIView
  8. from .celery_tasks import export_dataset
  9. from .pipeline.catalog import Options
  10. from projects.models import Project
  11. from projects.permissions import IsProjectAdmin
  12. class DatasetCatalog(APIView):
  13. permission_classes = [IsAuthenticated & IsProjectAdmin]
  14. def get(self, request, *args, **kwargs):
  15. project_id = kwargs["project_id"]
  16. project = get_object_or_404(Project, pk=project_id)
  17. options = Options.filter_by_task(project.project_type)
  18. return Response(data=options, status=status.HTTP_200_OK)
  19. class DatasetExportAPI(APIView):
  20. permission_classes = [IsAuthenticated & IsProjectAdmin]
  21. def get(self, request, *args, **kwargs):
  22. task_id = request.GET["taskId"]
  23. task = AsyncResult(task_id)
  24. ready = task.ready()
  25. if ready:
  26. filename = task.result
  27. return FileResponse(open(filename, mode="rb"), as_attachment=True)
  28. return Response({"status": "Not ready"})
  29. def post(self, request, *args, **kwargs):
  30. project_id = self.kwargs["project_id"]
  31. file_format = request.data.pop("format")
  32. export_approved = request.data.pop("exportApproved", False)
  33. task = export_dataset.delay(
  34. project_id=project_id, file_format=file_format, export_approved=export_approved, **request.data
  35. )
  36. return Response({"task_id": task.task_id})