NDOJ/judge/views/volunteer.py

34 lines
1.3 KiB
Python
Raw Normal View History

2022-05-03 02:44:14 +00:00
from django.http import HttpResponseBadRequest, JsonResponse
from django.db import transaction
from judge.models import VolunteerProblemVote, Problem, ProblemType
def vote_problem(request):
2022-05-14 17:57:27 +00:00
if not request.user or not request.user.has_perm("judge.suggest_problem_changes"):
2022-05-03 02:44:14 +00:00
return HttpResponseBadRequest()
2022-05-14 17:57:27 +00:00
if not request.method == "POST":
2022-05-03 02:44:14 +00:00
return HttpResponseBadRequest()
try:
2022-05-14 17:57:27 +00:00
types_id = request.POST.getlist("types[]")
2022-05-03 02:44:14 +00:00
types = ProblemType.objects.filter(id__in=types_id)
2022-05-14 17:57:27 +00:00
problem = Problem.objects.get(code=request.POST["problem"])
knowledge_points = request.POST["knowledge_points"]
thinking_points = request.POST["thinking_points"]
feedback = request.POST["feedback"]
2022-05-03 02:44:14 +00:00
except Exception as e:
return HttpResponseBadRequest()
with transaction.atomic():
vote, _ = VolunteerProblemVote.objects.get_or_create(
voter=request.profile,
problem=problem,
2022-05-14 17:57:27 +00:00
defaults={"knowledge_points": 0, "thinking_points": 0},
2022-05-03 02:44:14 +00:00
)
vote.knowledge_points = knowledge_points
vote.thinking_points = thinking_points
vote.feedback = feedback
vote.types.set(types)
vote.save()
return JsonResponse({})