Skip to content

Commit fc02649

Browse files
committed
Display score change in submissions admin
1 parent 9d763fb commit fc02649

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

oioioi/acm/controllers.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ 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 display_score_change(self):
205+
return False
206+
204207

205208
class ACMOpenContestController(ACMContestController):
206209
description = _("ACM style contest (open)")

oioioi/contests/admin.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,8 @@ def get_list_display(self, request):
712712
]
713713
if request.contest:
714714
list_display.remove('contest_display')
715+
if request.contest.controller.display_score_change():
716+
list_display.append('score_diff_display')
715717
return list_display
716718

717719
def get_list_display_links(self, request, list_display):
@@ -848,6 +850,22 @@ def score_display(self, instance):
848850
score_display.short_description = _("Score")
849851
score_display.admin_order_field = 'score_with_nulls_smallest'
850852

853+
def score_diff_display(self, instance):
854+
contest_controller = instance.problem_instance.contest.controller
855+
if not contest_controller.display_score_change() or instance.kind != 'NORMAL':
856+
return format_html('<span class="text-secondary">-</span>')
857+
858+
previous_submission = Submission.objects.filter(
859+
user=instance.user,
860+
problem_instance=instance.problem_instance,
861+
kind='NORMAL',
862+
date__lt=instance.date,
863+
).order_by('-date').first()
864+
return contest_controller.render_score_change(previous_submission, instance)
865+
866+
score_diff_display.short_description = _("Score change")
867+
score_diff_display.admin_order_field = 'score'
868+
851869
def contest_display(self, instance):
852870
return instance.problem_instance.contest
853871

oioioi/contests/controllers.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from django.db.models import Subquery
1010
from django.template.loader import render_to_string
1111
from django.urls import reverse
12+
from django.utils.html import format_html
1213
from django.utils.safestring import mark_safe
1314
from django.utils.translation import gettext_lazy as _
1415
from django.utils.translation import gettext_noop
@@ -974,6 +975,36 @@ def _is_partial_score(self, test_report):
974975
def show_default_fields(self, problem_instance):
975976
return problem_instance.problem.controller.show_default_fields(problem_instance)
976977

978+
def display_score_change(self):
979+
"""
980+
Whether to display score change for a submission in submissions admin.
981+
"""
982+
return True
983+
984+
def _calculate_score_change(self, before, after):
985+
"""
986+
Calculate score difference between two scores.
987+
"""
988+
if before is None or after is None:
989+
return after
990+
cls = type(before)
991+
return cls(after.value - before.value)
992+
993+
def render_score_change(self, previous_submission, current_submission):
994+
"""
995+
Calculates and renders score change between two submissions.
996+
"""
997+
prev_score = previous_submission.score if previous_submission else None
998+
curr_score = current_submission.score if current_submission else None
999+
diff = self._calculate_score_change(prev_score, curr_score)
1000+
if diff is None:
1001+
return format_html('<span class="text-secondary">-</span>')
1002+
if diff.value == 0:
1003+
return format_html('<span class="text-secondary">0</span>')
1004+
if diff.value > 0:
1005+
return format_html('<span class="text-success">+{}</span>', diff.value)
1006+
return format_html('<span class="text-danger">{}</span>', diff.value)
1007+
9771008

9781009
class PastRoundsHiddenContestControllerMixin(object):
9791010
"""ContestController mixin that hides past rounds

0 commit comments

Comments
 (0)