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.

45 lines
1.7 KiB

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. use_relation = getattr(project, "use_relation", False)
  18. options = Options.filter_by_task(project.project_type, use_relation)
  19. return Response(data=options, status=status.HTTP_200_OK)
  20. class DatasetExportAPI(APIView):
  21. permission_classes = [IsAuthenticated & IsProjectAdmin]
  22. def get(self, request, *args, **kwargs):
  23. task_id = request.GET["taskId"]
  24. task = AsyncResult(task_id)
  25. ready = task.ready()
  26. if ready:
  27. filename = task.result
  28. return FileResponse(open(filename, mode="rb"), as_attachment=True)
  29. return Response({"status": "Not ready"})
  30. def post(self, request, *args, **kwargs):
  31. project_id = self.kwargs["project_id"]
  32. file_format = request.data.pop("format")
  33. export_approved = request.data.pop("exportApproved", False)
  34. task = export_dataset.delay(
  35. project_id=project_id, file_format=file_format, confirmed_only=export_approved, **request.data
  36. )
  37. return Response({"task_id": task.task_id})