Skip to content

Commit 8a82ece

Browse files
committed
Refactor
1 parent d73bf11 commit 8a82ece

File tree

6 files changed

+45
-27
lines changed

6 files changed

+45
-27
lines changed

oioioi/acm/controllers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ def can_see_round(self, request_or_context, round, no_admin=False):
201201
def get_default_safe_exec_mode(self):
202202
return 'cpu'
203203

204-
def calculate_score_diff(self):
204+
def display_score_change(self):
205205
return False
206206

207207

oioioi/contests/admin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ def get_list_display(self, request):
712712
]
713713
if request.contest:
714714
list_display.remove('contest_display')
715-
if request.contest.controller.calculate_score_diff():
715+
if request.contest.controller.display_score_change():
716716
list_display.append('score_diff_display')
717717
return list_display
718718

@@ -852,7 +852,7 @@ def score_display(self, instance):
852852

853853
def score_diff_display(self, instance):
854854
if instance.score_diff is not None:
855-
return format_html(instance.problem_instance.contest.controller.render_score_diff(instance.score_diff))
855+
return format_html(instance.problem_instance.contest.controller.render_score_change(instance.score_diff))
856856
return format_html('<span class="text-secondary">-</span>')
857857

858858
score_diff_display.short_description = _("Score change")

oioioi/contests/controllers.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -975,24 +975,32 @@ def _is_partial_score(self, test_report):
975975
def show_default_fields(self, problem_instance):
976976
return problem_instance.problem.controller.show_default_fields(problem_instance)
977977

978-
def calculate_score_diff(self):
978+
def display_score_change(self):
979+
"""
980+
Whether to display score change for a submission in submissions admin.
981+
"""
979982
return True
980983

981-
def score_diff(self, before, after):
982-
print(before, after)
984+
def calculate_score_change(self, before, after):
985+
"""
986+
Calculate score difference between two scores.
987+
"""
983988
if before is None:
984989
return after
985-
return IntegerScore(after.to_int() - before.to_int())
990+
cls = type(before)
991+
return cls(after.value - before.value)
986992

987-
def render_score_diff(self, diff):
988-
print(diff)
993+
def render_score_change(self, diff):
994+
"""
995+
Render score change.
996+
"""
989997
if diff is None:
990998
return '<span class="text-secondary">-</span>'
991-
if diff.to_int() == 0:
999+
if diff.value == 0:
9921000
return '<span class="text-secondary">0</span>'
993-
if diff.to_int() > 0:
994-
return f'<span class="text-success">+{diff.to_int()}</span>'
995-
return f'<span class="text-danger">-{diff.to_int()}</span>'
1001+
if diff.value > 0:
1002+
return f'<span class="text-success">+{diff.value}</span>'
1003+
return f'<span class="text-danger">{diff.value}</span>'
9961004

9971005

9981006
class PastRoundsHiddenContestControllerMixin(object):

oioioi/contests/tests/tests.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4210,29 +4210,29 @@ class TestScoreDiff(TestCase):
42104210
'test_submission',
42114211
]
42124212

4213+
submission_cls = IntegerScore
4214+
diff_cls = IntegerScore
4215+
4216+
def _set_contest_controller(self, name):
4217+
contest = Contest.objects.get()
4218+
contest.controller_name = name
4219+
contest.save()
4220+
42134221
def _test_diff(self, new_score, expected_diff):
42144222
pi = ProblemInstance.objects.get()
42154223
user = User.objects.get(username='test_user')
42164224
new_submission = Submission.objects.create(
42174225
problem_instance=pi,
42184226
user=user,
4219-
score=IntegerScore(new_score),
4227+
score=self.submission_cls(new_score),
42204228
status='OK',
42214229
)
42224230
pi.controller.update_user_results(user, pi, new_submission)
4223-
assert new_submission.score_diff == IntegerScore(expected_diff)
4231+
assert new_submission.score_diff == self.diff_cls(expected_diff)
42244232

4233+
class TestScoreDiffProgrammingContest(TestScoreDiff):
42254234
def test_simple_programming_contest(self):
42264235
# Simple programming contest takes last score as the final one.
42274236
self._test_diff(50, 50 - 34)
42284237
self._test_diff(100, 100 - 50)
42294238
self._test_diff(20, 20 - 100)
4230-
4231-
def test_oi_finals_contest(self):
4232-
# OI finals contest takes the best score as the final one.
4233-
contest = Contest.objects.get()
4234-
contest.controller_name = 'oioioi.oi.controllers.OIFinalOnsiteContestController'
4235-
contest.save()
4236-
self._test_diff(50, 50 - 34)
4237-
self._test_diff(100, 100 - 50)
4238-
self._test_diff(20, 0)

oioioi/oi/tests.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from oioioi.contests.current_contest import ContestMode
1313
from oioioi.contests.handlers import update_user_results
1414
from oioioi.contests.models import Contest, ProblemInstance, Round
15+
from oioioi.contests.tests.tests import TestScoreDiff
1516
from oioioi.evalmgr.tasks import create_environ
1617
from oioioi.oi.management.commands import import_schools
1718
from oioioi.oi.models import OIRegistration, School
@@ -616,3 +617,12 @@ def test_user_info_page(self):
616617
self.assertContains(response, reg_data[k])
617618
else:
618619
self.assertNotContains(response, reg_data[k], status_code=403)
620+
621+
622+
class TestScoreDiffOIContest(TestScoreDiff):
623+
def test_oi_finals_contest(self):
624+
# OI finals contest takes the best score as the final one.
625+
self._set_contest_controller('oioioi.oi.controllers.OIFinalOnsiteContestController')
626+
self._test_diff(50, 50 - 34)
627+
self._test_diff(100, 100 - 50)
628+
self._test_diff(20, 0)

oioioi/problems/controllers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -480,16 +480,16 @@ def update_user_results(self, user, problem_instance, submission):
480480
) = UserResultForProblem.objects.select_for_update().get_or_create(
481481
user=user, problem_instance=problem_instance
482482
)
483-
if problem_instance.controller.calculate_score_diff():
483+
if problem_instance.controller.display_score_change():
484484
if created:
485485
score_before = None
486486
else:
487487
score_before = result.score
488488
problem_instance.controller.update_user_result_for_problem(result)
489489
result.save()
490-
if problem_instance.controller.calculate_score_diff():
490+
if problem_instance.controller.display_score_change():
491491
score_after = result.score
492-
submission.score_diff = problem_instance.controller.score_diff(
492+
submission.score_diff = problem_instance.controller.calculate_score_change(
493493
score_before, score_after
494494
)
495495
submission.save()

0 commit comments

Comments
 (0)