NDOJ/judge/views/resolver.py

149 lines
5.8 KiB
Python
Raw Normal View History

2022-12-19 01:01:02 +00:00
from django.views.generic import TemplateView
2022-12-18 23:53:28 +00:00
from django.utils.translation import gettext as _
2022-12-19 00:22:08 +00:00
from django.http import HttpResponseForbidden
2022-12-20 11:27:04 +00:00
from judge.models import Contest
from django.utils.safestring import mark_safe
import json
2022-12-18 23:53:28 +00:00
2022-12-20 08:24:24 +00:00
2022-12-19 01:01:02 +00:00
class Resolver(TemplateView):
title = _("Resolver")
template_name = "resolver/resolver.html"
2022-12-20 11:27:04 +00:00
def get_contest_json(self):
problems = self.contest.contest_problems.values_list("order", "id")
order_to_id = {}
id_to_order = {}
for order, problem_id in problems:
id_to_order[str(problem_id)] = order
2023-01-02 23:22:45 +00:00
hidden_subtasks = self.contest.format.get_hidden_subtasks()
2022-12-20 11:27:04 +00:00
num_problems = len(problems)
problem_sub = [0] * num_problems
sub_frozen = [0] * num_problems
problems_json = {str(i): {} for i in range(1, num_problems + 1)}
users = {}
cnt_user = 0
total_subtask_points_map = {}
for participation in self.contest.users.filter(virtual=0):
cnt_user += 1
users[str(cnt_user)] = {
"username": participation.user.user.username,
"name": participation.user.user.first_name
or participation.user.user.username,
"school": participation.user.user.last_name,
2022-12-29 00:37:40 +00:00
"last_submission": participation.cumtime_final,
2022-12-20 11:27:04 +00:00
"problems": {},
}
for (
problem_id,
problem_points,
time,
subtask_points,
total_subtask_points,
subtask,
sub_id,
) in self.contest.format.get_results_by_subtask(participation, True):
problem_id = str(problem_id)
order = id_to_order[problem_id]
problem_sub[order - 1] = max(problem_sub[order - 1], subtask)
if total_subtask_points:
total_subtask_points_map[(order, subtask)] = total_subtask_points
cnt_user = 0
for participation in self.contest.users.filter(virtual=0):
cnt_user += 1
total_points = {}
points_map = {}
frozen_points_map = {}
problem_points_map = {}
for (
problem_id,
problem_points,
time,
subtask_points,
total_subtask_points,
subtask,
sub_id,
) in self.contest.format.get_results_by_subtask(participation, True):
problem_id = str(problem_id)
order = id_to_order[problem_id]
points_map[(order, subtask)] = subtask_points
if order not in total_points:
total_points[order] = 0
total_points[order] += total_subtask_points
problem_points_map[order] = problem_points
for (
problem_id,
problem_points,
time,
subtask_points,
total_subtask_points,
subtask,
sub_id,
) in self.contest.format.get_results_by_subtask(participation, False):
problem_id = str(problem_id)
order = id_to_order[problem_id]
frozen_points_map[(order, subtask)] = subtask_points
for order in range(1, num_problems + 1):
for subtask in range(1, problem_sub[order - 1] + 1):
if not total_points.get(order, 0):
continue
if str(order) not in users[str(cnt_user)]["problems"]:
users[str(cnt_user)]["problems"][str(order)] = {
"points": {},
"frozen_points": {},
}
problems_json[str(order)][str(subtask)] = round(
total_subtask_points_map[(order, subtask)]
/ total_points[order]
* problem_points_map[order],
self.contest.points_precision,
)
users[str(cnt_user)]["problems"][str(order)]["points"][
str(subtask)
] = round(
points_map.get((order, subtask), 0)
/ total_points[order]
* problem_points_map[order],
self.contest.points_precision,
)
users[str(cnt_user)]["problems"][str(order)]["frozen_points"][
str(subtask)
] = round(
frozen_points_map.get((order, subtask), 0)
/ total_points[order]
* problem_points_map[order],
self.contest.points_precision,
)
2023-01-02 23:22:45 +00:00
for i in hidden_subtasks:
2022-12-20 11:27:04 +00:00
order = id_to_order[i]
2023-01-02 23:22:45 +00:00
if hidden_subtasks[i]:
sub_frozen[order - 1] = min(hidden_subtasks[i])
2022-12-20 11:27:04 +00:00
else:
sub_frozen[order - 1] = problem_sub[order - 1] + 1
return {
"problem_sub": problem_sub,
"sub_frozen": sub_frozen,
"problems": problems_json,
"users": users,
}
2022-12-19 01:01:02 +00:00
def get_context_data(self, **kwargs):
context = super(Resolver, self).get_context_data(**kwargs)
2022-12-20 11:27:04 +00:00
context["contest_json"] = mark_safe(json.dumps(self.get_contest_json()))
2022-12-19 01:01:02 +00:00
return context
def get(self, request, *args, **kwargs):
if request.user.is_superuser:
2022-12-20 11:27:04 +00:00
self.contest = Contest.objects.get(key=kwargs.get("contest"))
2023-01-02 23:22:45 +00:00
if self.contest.format.has_hidden_subtasks:
2022-12-20 11:27:04 +00:00
return super(Resolver, self).get(request, *args, **kwargs)
2022-12-19 01:01:02 +00:00
return HttpResponseForbidden()