NDOJ/judge/views/tasks.py

91 lines
2.4 KiB
Python
Raw Permalink Normal View History

2020-01-21 06:35:58 +00:00
import json
from functools import partial
from uuid import UUID
from celery.result import AsyncResult
from django.core.exceptions import PermissionDenied
2022-05-14 17:57:27 +00:00
from django.http import (
Http404,
HttpResponseBadRequest,
HttpResponseRedirect,
JsonResponse,
)
2020-01-21 06:35:58 +00:00
from django.shortcuts import render
from django.urls import reverse
from django.utils.http import is_safe_url
from judge.tasks import failure, progress, success
from judge.utils.celery import redirect_to_task_status
from judge.utils.views import short_circuit_middleware
def get_task_status(task_id):
result = AsyncResult(task_id)
info = result.result
2022-05-14 17:57:27 +00:00
if result.state == "PROGRESS":
return {
"code": "PROGRESS",
"done": info["done"],
"total": info["total"],
"stage": info["stage"],
}
elif result.state == "SUCCESS":
return {"code": "SUCCESS"}
elif result.state == "FAILURE":
return {"code": "FAILURE", "error": str(info)}
2020-01-21 06:35:58 +00:00
else:
2022-05-14 17:57:27 +00:00
return {"code": "WORKING"}
2020-01-21 06:35:58 +00:00
def task_status(request, task_id):
try:
UUID(task_id)
except ValueError:
raise Http404()
2022-05-14 17:57:27 +00:00
redirect = request.GET.get("redirect")
2020-01-21 06:35:58 +00:00
if not is_safe_url(redirect, allowed_hosts={request.get_host()}):
redirect = None
status = get_task_status(task_id)
2022-05-14 17:57:27 +00:00
if status["code"] == "SUCCESS" and redirect:
2020-01-21 06:35:58 +00:00
return HttpResponseRedirect(redirect)
2022-05-14 17:57:27 +00:00
return render(
request,
"task_status.html",
{
"task_id": task_id,
"task_status": json.dumps(status),
"message": request.GET.get("message", ""),
"redirect": redirect or "",
},
)
2020-01-21 06:35:58 +00:00
@short_circuit_middleware
def task_status_ajax(request):
2022-05-14 17:57:27 +00:00
if "id" not in request.GET:
return HttpResponseBadRequest(
'Need to pass GET parameter "id"', content_type="text/plain"
)
return JsonResponse(get_task_status(request.GET["id"]))
2020-01-21 06:35:58 +00:00
def demo_task(request, task, message):
if not request.user.is_superuser:
raise PermissionDenied()
result = task.delay()
2022-05-14 17:57:27 +00:00
return redirect_to_task_status(result, message=message, redirect=reverse("home"))
2020-01-21 06:35:58 +00:00
2022-05-14 17:57:27 +00:00
demo_success = partial(
demo_task, task=success, message="Running example task that succeeds..."
)
demo_failure = partial(
demo_task, task=failure, message="Running example task that fails..."
)
demo_progress = partial(
demo_task, task=progress, message="Running example task that waits 10 seconds..."
)