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.

47 lines
1.6 KiB

  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 ..models import Project
  9. from ..permissions import IsProjectAdmin
  10. from ..tasks import export_dataset
  11. from .download.catalog import Options
  12. class DownloadDatasetCatalog(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 DownloadAPI(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. format = request.data.pop('format')
  32. export_approved = request.data.pop('exportApproved', False)
  33. task = export_dataset.delay(
  34. project_id=project_id,
  35. format=format,
  36. export_approved=export_approved,
  37. **request.data
  38. )
  39. return Response({'task_id': task.task_id})