Skip to content

Commit

Permalink
Optimize ranked submission page (DMOJ)
Browse files Browse the repository at this point in the history
  • Loading branch information
cuom1999 committed Dec 15, 2024
1 parent 0e88b56 commit bfe46c6
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 17 deletions.
4 changes: 2 additions & 2 deletions dmoj/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -1232,8 +1232,8 @@ def paged_list_view(view, name, **kwargs):
url(r"^upload/$", custom_file_upload.file_upload, name="custom_file_upload"),
] + url_static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

# if hasattr(settings, "INTERNAL_IPS"):
# urlpatterns.append(url("__debug__/", include("debug_toolbar.urls")))
if "debug_toolbar.middleware.DebugToolbarMiddleware" in settings.MIDDLEWARE:
urlpatterns.append(url("__debug__/", include("debug_toolbar.urls")))

favicon_paths = [
"apple-touch-icon-180x180.png",
Expand Down
92 changes: 77 additions & 15 deletions judge/views/ranked_submission.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,43 @@
from django.urls import reverse
from django.utils.html import format_html
from django.utils.html import escape, format_html
from django.utils.safestring import mark_safe
from django.utils.translation import gettext as _

from judge.models import Submission
from judge.models import Language, Submission
from judge.utils.problems import get_result_data
from judge.utils.raw_sql import join_sql_subquery
from judge.views.submission import ForceContestMixin, ProblemSubmissions

__all__ = ["RankedSubmissions"]
__all__ = ["RankedSubmissions", "ContestRankedSubmission"]


class RankedSubmissions(ProblemSubmissions):
tab = "best_submissions_list"
page_type = "best_submissions_list"
dynamic_update = False

def get_queryset(self):
params = [self.problem.id]
if self.in_contest:
contest_join = "INNER JOIN judge_contestsubmission AS cs ON (sub.id = cs.submission_id)"
points = "cs.points"
constraint = " AND sub.contest_object_id = %s"
params.append(self.contest.id)
else:
contest_join = ""
points = "sub.points"
constraint = ""

if self.selected_languages:
lang_ids = Language.objects.filter(
key__in=self.selected_languages
).values_list("id", flat=True)
if lang_ids:
constraint += (
f' AND sub.language_id IN ({", ".join(["%s"] * len(lang_ids))})'
)
params.extend(lang_ids)
self.selected_languages = set()

queryset = (
super(RankedSubmissions, self)
.get_queryset()
Expand Down Expand Up @@ -51,11 +65,7 @@ def get_queryset(self):
""".format(
points=points, contest_join=contest_join, constraint=constraint
),
params=(
[self.problem.id, self.contest.id] * 3
if self.in_contest
else [self.problem.id] * 3
),
params=params * 3,
alias="best_subs",
join_fields=[("id", "id")],
related_model=Submission,
Expand All @@ -70,11 +80,63 @@ def get_title(self):
return _("Best solutions for %s") % self.problem_name

def get_content_title(self):
return format_html(
_('Best solutions for <a href="{1}">{0}</a>'),
self.problem_name,
reverse("problem_detail", args=[self.problem.code]),
return mark_safe(
escape(_("Best solutions for %s"))
% (
format_html(
'<a href="{1}">{0}</a>',
self.problem_name,
reverse("problem_detail", args=[self.problem.code]),
),
)
)

def _get_result_data(self, queryset=None):
if queryset is None:
queryset = super(RankedSubmissions, self).get_queryset()
return get_result_data(queryset.order_by())


class ContestRankedSubmission(ForceContestMixin, RankedSubmissions):
def get_title(self):
if self.problem.is_accessible_by(self.request.user):
return _("Best solutions for %(problem)s in %(contest)s") % {
"problem": self.problem_name,
"contest": self.contest.name,
}
return _("Best solutions for problem %(number)s in %(contest)s") % {
"number": self.get_problem_number(self.problem),
"contest": self.contest.name,
}

def get_content_title(self):
if self.problem.is_accessible_by(self.request.user):
return mark_safe(
escape(_("Best solutions for %(problem)s in %(contest)s"))
% {
"problem": format_html(
'<a href="{1}">{0}</a>',
self.problem_name,
reverse("problem_detail", args=[self.problem.code]),
),
"contest": format_html(
'<a href="{1}">{0}</a>',
self.contest.name,
reverse("contest_view", args=[self.contest.key]),
),
}
)
return mark_safe(
escape(_("Best solutions for problem %(number)s in %(contest)s"))
% {
"number": self.get_problem_number(self.problem),
"contest": format_html(
'<a href="{1}">{0}</a>',
self.contest.name,
reverse("contest_view", args=[self.contest.key]),
),
}
)

def _get_result_data(self):
return get_result_data(super(RankedSubmissions, self).get_queryset().order_by())
def _get_queryset(self):
return super()._get_queryset().filter(contest_object=self.contest)
2 changes: 2 additions & 0 deletions robots.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ Disallow: /problem/*/raw
Disallow: /problem/*/submit
Disallow: /problem/*/resubmit/
Disallow: /problem/*/submissions/
Disallow: /problem/*/rank/*
Disallow: /user/*/submissions/
Disallow: /user/*/solved/
Disallow: /submission/
Disallow: /submissions/
Disallow: /single_submission
Expand Down

0 comments on commit bfe46c6

Please sign in to comment.