|
9 | 9 | from django.db.models import Subquery
|
10 | 10 | from django.template.loader import render_to_string
|
11 | 11 | from django.urls import reverse
|
| 12 | +from django.utils.html import format_html |
12 | 13 | from django.utils.safestring import mark_safe
|
13 | 14 | from django.utils.translation import gettext_lazy as _
|
14 | 15 | from django.utils.translation import gettext_noop
|
@@ -974,6 +975,36 @@ def _is_partial_score(self, test_report):
|
974 | 975 | def show_default_fields(self, problem_instance):
|
975 | 976 | return problem_instance.problem.controller.show_default_fields(problem_instance)
|
976 | 977 |
|
| 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 | + |
977 | 1008 |
|
978 | 1009 | class PastRoundsHiddenContestControllerMixin(object):
|
979 | 1010 | """ContestController mixin that hides past rounds
|
|
0 commit comments