Skip to content

Commit

Permalink
Merge pull request #127 from anhkha2003/master
Browse files Browse the repository at this point in the history
Fix contest rescore bug
  • Loading branch information
anhkha2003 authored Oct 3, 2024
2 parents 3d67fb2 + 18d23c7 commit f771aac
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 58 deletions.
41 changes: 6 additions & 35 deletions judge/admin/contest.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
HeavyPreviewAdminPageDownWidget,
)
from judge.views.contests import recalculate_contest_summary_result
from judge.utils.contest import maybe_trigger_contest_rescore


class AdminHeavySelect2Widget(AdminHeavySelect2Widget):
Expand Down Expand Up @@ -310,39 +311,14 @@ def save_model(self, request, obj, form, change):

super().save_model(request, obj, form, change)

# We need this flag because `save_related` deals with the inlines, but does not know if we have already rescored
self._rescored = False
if form.changed_data and any(
f in form.changed_data
for f in (
"start_time",
"end_time",
"time_limit",
"format_config",
"format_name",
"freeze_after",
)
):
self._rescore(obj.key)
self._rescored = True

if form.changed_data and any(
f in form.changed_data
for f in (
"authors",
"curators",
"testers",
)
):
Contest._author_ids.dirty(obj)
Contest._curator_ids.dirty(obj)
Contest._tester_ids.dirty(obj)

def save_related(self, request, form, formsets, change):
super().save_related(request, form, formsets, change)
# Only rescored if we did not already do so in `save_model`
if not self._rescored and any(formset.has_changed() for formset in formsets):
self._rescore(form.cleaned_data["key"])
formset_changed = False
if any(formset.has_changed() for formset in formsets):
formset_changed = True

maybe_trigger_contest_rescore(form, form.instance, formset_changed)

def has_change_permission(self, request, obj=None):
if not request.user.has_perm("judge.edit_own_contest"):
Expand All @@ -351,11 +327,6 @@ def has_change_permission(self, request, obj=None):
return True
return obj.is_editable_by(request.user)

def _rescore(self, contest_key):
from judge.tasks import rescore_contest

transaction.on_commit(rescore_contest.s(contest_key).delay)

def make_visible(self, request, queryset):
if not request.user.has_perm("judge.change_contest_visibility"):
queryset = queryset.filter(
Expand Down
23 changes: 13 additions & 10 deletions judge/utils/contest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@
)


def maybe_trigger_contest_rescore(form, contest):
if any(
f in form.changed_data
for f in (
"start_time",
"end_time",
"time_limit",
"format_config",
"format_name",
"freeze_after",
def maybe_trigger_contest_rescore(form, contest, force_rescore=False):
if (
any(
f in form.changed_data
for f in (
"start_time",
"end_time",
"time_limit",
"format_config",
"format_name",
"freeze_after",
)
)
or force_rescore
):
transaction.on_commit(rescore_contest.s(contest.key).delay)

Expand Down
6 changes: 5 additions & 1 deletion judge/views/course.py
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,10 @@ def get_form_kwargs(self):
def post(self, request, *args, **kwargs):
problem_formset = self.get_problem_formset(True)
if problem_formset.is_valid():
self.problem_form_changes = False
for problem_form in problem_formset:
if problem_form.has_changed():
self.problem_form_changes = True
if problem_form.cleaned_data.get("DELETE") and problem_form.instance.pk:
problem_form.instance.delete()

Expand All @@ -741,7 +744,8 @@ def form_valid(self, form):
revisions.set_comment(_("Edited from course") + " " + self.course.name)
revisions.set_user(self.request.user)

maybe_trigger_contest_rescore(form, self.contest)
if self.problem_form_changes:
maybe_trigger_contest_rescore(form, self.contest, True)

form.save()

Expand Down
2 changes: 1 addition & 1 deletion judge/views/organization.py
Original file line number Diff line number Diff line change
Expand Up @@ -1039,7 +1039,7 @@ def form_valid(self, form):
self.object.is_organization_private = True
self.object.save()

maybe_trigger_contest_rescore(form, self.object)
maybe_trigger_contest_rescore(form, self.object, True)

return res

Expand Down
4 changes: 4 additions & 0 deletions resources/base.scss
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,10 @@ noscript #noscript {
.nav-fa-icon {
display: none;
}

.page-title {
margin-left: 0.5em;
}
}

@media(min-width: 800px) {
Expand Down
2 changes: 1 addition & 1 deletion resources/table.scss
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
}
}

td:first-child {
th:first-child, td:first-child {
border-color: $border_gray;
border-width: 1px 1px 0 1px;
}
Expand Down
1 change: 1 addition & 0 deletions resources/users.scss
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ th.header.rank {
vertical-align: top;
width: 225px;
float: right;
margin-top: 1em;

.select2-selection__arrow {
display: none;
Expand Down
18 changes: 10 additions & 8 deletions templates/course/grades.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
var rows = $('#users-table tbody tr').get();
var sortBy = $(this).val();
rows.sort(function(a, b) {
var keyA = $(a).find(sortBy === 'username' ? '.user-name' : 'td:last-child').text().trim();
var keyB = $(b).find(sortBy === 'username' ? '.user-name' : 'td:last-child').text().trim();
var keyA = $(a).find(sortBy === 'username' ? '.user-name' : '.total-score').text().trim();
var keyB = $(b).find(sortBy === 'username' ? '.user-name' : '.total-score').text().trim();

if(sortBy === 'total') {
// Convert percentage string to number for comparison
Expand Down Expand Up @@ -76,11 +76,13 @@
{% block middle_content %}
<center><h2>{{content_title}}</h2></center>
{{_("Sort by")}}:
<select id="sortSelect">
<option value="username">{{_("Username")}}</option>
<option value="total">{{_("Score")}}</option>
</select>
<input type="text" id="search-input" placeholder="{{_('Search')}}" autofocus>
<div>
<select id="sortSelect">
<option value="username">{{_("Username")}}</option>
<option value="total">{{_("Score")}}</option>
</select>
<input type="text" id="search-input" placeholder="{{_('Search')}}" autofocus>
</div>
<div style="overflow-x: auto; margin-top: 1em">
<table class="table striped" id="users-table">
<thead>
Expand Down Expand Up @@ -119,7 +121,7 @@
{{student.first_name}}
</div>
</td>
<td style="font-weight: bold">
<td class="total-score" style="font-weight: bold">
{% if grade_total_student %}
{{ grade_total_student['percentage'] | floatformat(0) }}%
{% else %}
Expand Down
3 changes: 1 addition & 2 deletions templates/user/pp-row.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
</div>
<div class="score">{{ breakdown.sub_points|floatformat(0) }} / {{ breakdown.sub_total|floatformat(0) }}</div>
<div class="language">{{ breakdown.sub_lang }}</div>
<span class="time">{{ relative_time(breakdown.sub_date) }}</span>
</div>
</div>
<div class="sub-pp sub-usage">
Expand All @@ -19,7 +18,7 @@
</div>
<div class="pp-weighted">
{% trans weight=breakdown.weight|floatformat(0) %}
weighted <b>{{ weight }}%</b>
<b>{{ weight }}%</b>
{% endtrans %}

{% if breakdown.scaled_points < 10 %}
Expand Down

0 comments on commit f771aac

Please sign in to comment.