Skip to content

Commit

Permalink
Add actions for contest locking; #595
Browse files Browse the repository at this point in the history
  • Loading branch information
WallE256 committed May 20, 2020
1 parent cfe694e commit fac4145
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
47 changes: 46 additions & 1 deletion judge/admin/contest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from reversion.admin import VersionAdmin

from django_ace import AceWidget
from judge.models import Contest, ContestProblem, ContestSubmission, Profile, Rating
from judge.models import Contest, ContestProblem, ContestSubmission, Profile, Rating, Submission
from judge.ratings import rate_contest
from judge.utils.views import NoBatchDeleteMixin
from judge.widgets import AdminHeavySelect2MultipleWidget, AdminHeavySelect2Widget, AdminMartorWidget, \
Expand Down Expand Up @@ -139,6 +139,10 @@ def get_actions(self, request):
for action in ('make_visible', 'make_hidden'):
actions[action] = self.get_action(action)

if request.user.has_perm('judge.contest_lock'):
for action in ('set_locked', 'set_unlocked'):
actions[action] = self.get_action(action)

return actions

def get_queryset(self, request):
Expand Down Expand Up @@ -212,13 +216,54 @@ def make_hidden(self, request, queryset):
count) % count)
make_hidden.short_description = _('Mark contests as hidden')

def set_locked(self, request, queryset):
for row in queryset:
self.set_is_locked(row, True)
count = queryset.count()
self.message_user(request, ungettext('%d contest successfully locked.',
'%d contests successfully locked.',
count) % count)
set_locked.short_description = _('Lock contest submissions')

def set_unlocked(self, request, queryset):
for row in queryset:
self.set_is_locked(row, False)
count = queryset.count()
self.message_user(request, ungettext('%d contest successfully unlocked.',
'%d contests successfully unlocked.',
count) % count)
set_unlocked.short_description = _('Unlock contest submissions')

def set_is_locked(self, contest, is_locked):
with transaction.atomic():
contest.is_locked = is_locked
contest.save()
Submission.objects.filter(contest_object=contest,
contest__participation__virtual=0).update(is_locked=is_locked)

def get_urls(self):
return [
url(r'^rate/all/$', self.rate_all_view, name='judge_contest_rate_all'),
url(r'^(\d+)/rate/$', self.rate_view, name='judge_contest_rate'),
url(r'^(\d+)/judge/(\d+)/$', self.rejudge_view, name='judge_contest_rejudge'),
url(r'^(\d+)/lock/$', self.lock_view, name='judge_contest_lock'),
url(r'^(\d+)/unlock/$', self.unlock_view, name='judge_contest_unlock'),
] + super(ContestAdmin, self).get_urls()

def lock_view(self, request, id):
if not request.user.has_perm('judge.contest_lock'):
raise PermissionDenied()
contest = get_object_or_404(Contest, id=id)
self.set_is_locked(contest, True)
return HttpResponseRedirect(reverse('admin:judge_contest_change', args=(id,)))

def unlock_view(self, request, id):
if not request.user.has_perm('judge.contest_lock'):
raise PermissionDenied()
contest = get_object_or_404(Contest, id=id)
self.set_is_locked(contest, False)
return HttpResponseRedirect(reverse('admin:judge_contest_change', args=(id,)))

def rejudge_view(self, request, contest_id, problem_id):
queryset = ContestSubmission.objects.filter(problem_id=problem_id).select_related('submission')
for model in queryset:
Expand Down
15 changes: 15 additions & 0 deletions templates/admin/judge/contest/change_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<script>
django.jQuery(function ($) {
$('.rerate-link').appendTo('div#bottombar').show();
$('.lock-link').appendTo('div#bottombar').show();
$('.rejudge-link').click(function () {
return confirm('{{ _('Are you sure you want to rejudge ALL the submissions?') }}');
});
Expand All @@ -20,4 +21,18 @@
<span class="text">{% trans "Rate" %}</span>
</a>
{% endif %}
{% if original and not original.is_locked and perms.judge.contest_lock %}
<a style="display: none" title="{% trans "Lock submissions" %}" href="{% url 'admin:judge_contest_lock' original.pk %}"
class="button lock-link">
<i class="fa fa-lg fa-lock"></i>
<span class="text">{% trans "Lock submissions" %}</span>
</a>
{% endif %}
{% if original and original.is_locked and perms.judge.contest_lock %}
<a style="display: none" title="{% trans "Unlock submissions" %}" href="{% url 'admin:judge_contest_unlock' original.pk %}"
class="button lock-link">
<i class="fa fa-lg fa-unlock"></i>
<span class="text">{% trans "Unlock submissions" %}</span>
</a>
{% endif %}
{% endblock %}

0 comments on commit fac4145

Please sign in to comment.