NDOJ/judge/models/submission.py

284 lines
8.8 KiB
Python
Raw Permalink Normal View History

2020-01-21 06:35:58 +00:00
import hashlib
import hmac
from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist
from django.db import models
from django.urls import reverse
from django.utils.functional import cached_property
from django.utils.translation import gettext_lazy as _
from judge.judgeapi import abort_submission, judge_submission
2022-11-01 01:43:06 +00:00
from judge.models.problem import Problem
2020-01-21 06:35:58 +00:00
from judge.models.profile import Profile
from judge.models.runtime import Language
from judge.utils.unicode import utf8bytes
2022-05-14 17:57:27 +00:00
__all__ = ["SUBMISSION_RESULT", "Submission", "SubmissionSource", "SubmissionTestCase"]
2020-01-21 06:35:58 +00:00
SUBMISSION_RESULT = (
2022-05-14 17:57:27 +00:00
("AC", _("Accepted")),
("WA", _("Wrong Answer")),
("TLE", _("Time Limit Exceeded")),
("MLE", _("Memory Limit Exceeded")),
("OLE", _("Output Limit Exceeded")),
("IR", _("Invalid Return")),
("RTE", _("Runtime Error")),
("CE", _("Compile Error")),
("IE", _("Internal Error")),
("SC", _("Short circuit")),
("AB", _("Aborted")),
2020-01-21 06:35:58 +00:00
)
class Submission(models.Model):
STATUS = (
2022-05-14 17:57:27 +00:00
("QU", _("Queued")),
("P", _("Processing")),
("G", _("Grading")),
("D", _("Completed")),
("IE", _("Internal Error")),
("CE", _("Compile Error")),
("AB", _("Aborted")),
)
IN_PROGRESS_GRADING_STATUS = ("QU", "P", "G")
2020-01-21 06:35:58 +00:00
RESULT = SUBMISSION_RESULT
USER_DISPLAY_CODES = {
2022-05-14 17:57:27 +00:00
"AC": _("Accepted"),
"WA": _("Wrong Answer"),
"SC": "Short Circuited",
"TLE": _("Time Limit Exceeded"),
"MLE": _("Memory Limit Exceeded"),
"OLE": _("Output Limit Exceeded"),
"IR": _("Invalid Return"),
"RTE": _("Runtime Error"),
"CE": _("Compile Error"),
"IE": _("Internal Error (judging server error)"),
"QU": _("Queued"),
"P": _("Processing"),
"G": _("Grading"),
"D": _("Completed"),
"AB": _("Aborted"),
2020-01-21 06:35:58 +00:00
}
user = models.ForeignKey(Profile, on_delete=models.CASCADE)
problem = models.ForeignKey(Problem, on_delete=models.CASCADE)
2022-05-14 17:57:27 +00:00
date = models.DateTimeField(
verbose_name=_("submission time"), auto_now_add=True, db_index=True
)
time = models.FloatField(verbose_name=_("execution time"), null=True, db_index=True)
memory = models.FloatField(verbose_name=_("memory usage"), null=True)
points = models.FloatField(
verbose_name=_("points granted"), null=True, db_index=True
)
language = models.ForeignKey(
Language, verbose_name=_("submission language"), on_delete=models.CASCADE
)
status = models.CharField(
verbose_name=_("status"),
max_length=2,
choices=STATUS,
default="QU",
db_index=True,
)
result = models.CharField(
verbose_name=_("result"),
max_length=3,
choices=SUBMISSION_RESULT,
default=None,
null=True,
blank=True,
db_index=True,
)
error = models.TextField(verbose_name=_("compile errors"), null=True, blank=True)
2020-01-21 06:35:58 +00:00
current_testcase = models.IntegerField(default=0)
2022-05-14 17:57:27 +00:00
batch = models.BooleanField(verbose_name=_("batched cases"), default=False)
case_points = models.FloatField(verbose_name=_("test case points"), default=0)
case_total = models.FloatField(verbose_name=_("test case total points"), default=0)
judged_on = models.ForeignKey(
"Judge",
verbose_name=_("judged on"),
null=True,
blank=True,
on_delete=models.SET_NULL,
)
judged_date = models.DateTimeField(
verbose_name=_("submission judge time"), default=None, null=True
)
was_rejudged = models.BooleanField(
verbose_name=_("was rejudged by admin"), default=False
)
is_pretested = models.BooleanField(
verbose_name=_("was ran on pretests only"), default=False
)
contest_object = models.ForeignKey(
"Contest",
verbose_name=_("contest"),
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name="+",
)
2020-01-21 06:35:58 +00:00
@classmethod
def result_class_from_code(cls, result, case_points, case_total):
2022-05-14 17:57:27 +00:00
if result == "AC":
2020-01-21 06:35:58 +00:00
if case_points == case_total:
2022-05-14 17:57:27 +00:00
return "AC"
return "_AC"
2020-01-21 06:35:58 +00:00
return result
@property
def result_class(self):
# This exists to save all these conditionals from being executed (slowly) in each row.jade template
2022-05-14 17:57:27 +00:00
if self.status in ("IE", "CE"):
2020-01-21 06:35:58 +00:00
return self.status
2022-05-14 17:57:27 +00:00
return Submission.result_class_from_code(
self.result, self.case_points, self.case_total
)
2020-01-21 06:35:58 +00:00
@property
def memory_bytes(self):
return self.memory * 1024 if self.memory is not None else 0
@property
def short_status(self):
return self.result or self.status
@property
def long_status(self):
2022-05-14 17:57:27 +00:00
return Submission.USER_DISPLAY_CODES.get(self.short_status, "")
2020-01-21 06:35:58 +00:00
2020-07-19 21:27:14 +00:00
def judge(self, *args, **kwargs):
judge_submission(self, *args, **kwargs)
2020-01-21 06:35:58 +00:00
judge.alters_data = True
def abort(self):
abort_submission(self)
abort.alters_data = True
def update_contest(self):
try:
contest = self.contest
except AttributeError:
return
contest_problem = contest.problem
2022-05-14 17:57:27 +00:00
contest.points = round(
self.case_points / self.case_total * contest_problem.points
if self.case_total > 0
else 0,
3,
)
2020-01-21 06:35:58 +00:00
if not contest_problem.partial and contest.points != contest_problem.points:
contest.points = 0
contest.save()
contest.participation.recompute_results()
update_contest.alters_data = True
@property
def is_graded(self):
2022-05-14 17:57:27 +00:00
return self.status not in ("QU", "P", "G")
2020-01-21 06:35:58 +00:00
@cached_property
def contest_key(self):
2022-05-14 17:57:27 +00:00
if hasattr(self, "contest"):
2020-01-21 06:35:58 +00:00
return self.contest_object.key
def __str__(self):
2022-05-14 17:57:27 +00:00
return "Submission %d of %s by %s" % (
self.id,
self.problem,
self.user.user.username,
)
2020-01-21 06:35:58 +00:00
def get_absolute_url(self):
2022-05-14 17:57:27 +00:00
return reverse("submission_status", args=(self.id,))
2020-01-21 06:35:58 +00:00
@cached_property
def contest_or_none(self):
try:
return self.contest
except ObjectDoesNotExist:
return None
@classmethod
def get_id_secret(cls, sub_id):
2022-05-14 17:57:27 +00:00
return (
hmac.new(
utf8bytes(settings.EVENT_DAEMON_SUBMISSION_KEY),
b"%d" % sub_id,
hashlib.sha512,
).hexdigest()[:16]
+ "%08x" % sub_id
)
2020-01-21 06:35:58 +00:00
@cached_property
def id_secret(self):
return self.get_id_secret(self.id)
class Meta:
permissions = (
2022-05-14 17:57:27 +00:00
("abort_any_submission", "Abort any submission"),
("rejudge_submission", "Rejudge the submission"),
("rejudge_submission_lot", "Rejudge a lot of submissions"),
("spam_submission", "Submit without limit"),
("view_all_submission", "View all submission"),
("resubmit_other", "Resubmit others' submission"),
2022-06-16 07:46:29 +00:00
("view_public_submission", "View public submissions"),
2020-01-21 06:35:58 +00:00
)
2022-05-14 17:57:27 +00:00
verbose_name = _("submission")
verbose_name_plural = _("submissions")
2020-01-21 06:35:58 +00:00
class SubmissionSource(models.Model):
2022-05-14 17:57:27 +00:00
submission = models.OneToOneField(
Submission,
on_delete=models.CASCADE,
verbose_name=_("associated submission"),
related_name="source",
)
source = models.TextField(verbose_name=_("source code"), max_length=65536)
2020-01-21 06:35:58 +00:00
def __str__(self):
2022-05-14 17:57:27 +00:00
return "Source of %s" % self.submission
2020-01-21 06:35:58 +00:00
class SubmissionTestCase(models.Model):
RESULT = SUBMISSION_RESULT
2022-05-14 17:57:27 +00:00
submission = models.ForeignKey(
Submission,
verbose_name=_("associated submission"),
related_name="test_cases",
on_delete=models.CASCADE,
)
case = models.IntegerField(verbose_name=_("test case ID"))
status = models.CharField(
max_length=3, verbose_name=_("status flag"), choices=SUBMISSION_RESULT
)
time = models.FloatField(verbose_name=_("execution time"), null=True)
memory = models.FloatField(verbose_name=_("memory usage"), null=True)
points = models.FloatField(verbose_name=_("points granted"), null=True)
total = models.FloatField(verbose_name=_("points possible"), null=True)
batch = models.IntegerField(verbose_name=_("batch number"), null=True)
feedback = models.CharField(
max_length=50, verbose_name=_("judging feedback"), blank=True
)
extended_feedback = models.TextField(
verbose_name=_("extended judging feedback"), blank=True
)
output = models.TextField(verbose_name=_("program output"), blank=True)
2020-01-21 06:35:58 +00:00
@property
def long_status(self):
2022-05-14 17:57:27 +00:00
return Submission.USER_DISPLAY_CODES.get(self.status, "")
2020-01-21 06:35:58 +00:00
class Meta:
2022-05-14 17:57:27 +00:00
unique_together = ("submission", "case")
verbose_name = _("submission test case")
verbose_name_plural = _("submission test cases")