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.

21 lines
661 B

2 years ago
2 years ago
  1. from celery.result import AsyncResult
  2. from rest_framework.permissions import IsAuthenticated
  3. from rest_framework.response import Response
  4. from rest_framework.views import APIView
  5. class TaskStatus(APIView):
  6. permission_classes = (IsAuthenticated,)
  7. def get(self, request, *args, **kwargs):
  8. task = AsyncResult(kwargs["task_id"])
  9. ready = task.ready()
  10. error = ready and not task.successful()
  11. return Response(
  12. {
  13. "ready": ready,
  14. "result": task.result if ready and not error else None,
  15. "error": {"text": str(task.result)} if error else None,
  16. }
  17. )