Skip to content

Commit

Permalink
Display score change in submissions admin
Browse files Browse the repository at this point in the history
  • Loading branch information
MasloMaslane committed Sep 17, 2024
1 parent 9d763fb commit fc02649
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
3 changes: 3 additions & 0 deletions oioioi/acm/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ def can_see_round(self, request_or_context, round, no_admin=False):
def get_default_safe_exec_mode(self):
return 'cpu'

def display_score_change(self):
return False


class ACMOpenContestController(ACMContestController):
description = _("ACM style contest (open)")
Expand Down
18 changes: 18 additions & 0 deletions oioioi/contests/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,8 @@ def get_list_display(self, request):
]
if request.contest:
list_display.remove('contest_display')
if request.contest.controller.display_score_change():
list_display.append('score_diff_display')
return list_display

def get_list_display_links(self, request, list_display):
Expand Down Expand Up @@ -848,6 +850,22 @@ def score_display(self, instance):
score_display.short_description = _("Score")
score_display.admin_order_field = 'score_with_nulls_smallest'

def score_diff_display(self, instance):
contest_controller = instance.problem_instance.contest.controller
if not contest_controller.display_score_change() or instance.kind != 'NORMAL':
return format_html('<span class="text-secondary">-</span>')

previous_submission = Submission.objects.filter(
user=instance.user,
problem_instance=instance.problem_instance,
kind='NORMAL',
date__lt=instance.date,
).order_by('-date').first()
return contest_controller.render_score_change(previous_submission, instance)

score_diff_display.short_description = _("Score change")
score_diff_display.admin_order_field = 'score'

def contest_display(self, instance):
return instance.problem_instance.contest

Expand Down
31 changes: 31 additions & 0 deletions oioioi/contests/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from django.db.models import Subquery
from django.template.loader import render_to_string
from django.urls import reverse
from django.utils.html import format_html
from django.utils.safestring import mark_safe
from django.utils.translation import gettext_lazy as _
from django.utils.translation import gettext_noop
Expand Down Expand Up @@ -974,6 +975,36 @@ def _is_partial_score(self, test_report):
def show_default_fields(self, problem_instance):
return problem_instance.problem.controller.show_default_fields(problem_instance)

def display_score_change(self):
"""
Whether to display score change for a submission in submissions admin.
"""
return True

def _calculate_score_change(self, before, after):
"""
Calculate score difference between two scores.
"""
if before is None or after is None:
return after
cls = type(before)
return cls(after.value - before.value)

def render_score_change(self, previous_submission, current_submission):
"""
Calculates and renders score change between two submissions.
"""
prev_score = previous_submission.score if previous_submission else None
curr_score = current_submission.score if current_submission else None
diff = self._calculate_score_change(prev_score, curr_score)
if diff is None:
return format_html('<span class="text-secondary">-</span>')
if diff.value == 0:
return format_html('<span class="text-secondary">0</span>')
if diff.value > 0:
return format_html('<span class="text-success">+{}</span>', diff.value)
return format_html('<span class="text-danger">{}</span>', diff.value)


class PastRoundsHiddenContestControllerMixin(object):
"""ContestController mixin that hides past rounds
Expand Down

0 comments on commit fc02649

Please sign in to comment.