Skip to content

Commit

Permalink
missed some
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonLovesDoggo committed Jan 6, 2024
1 parent 015a538 commit d69e319
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 34 deletions.
36 changes: 24 additions & 12 deletions judge/admin/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,19 @@ def has_add_permission(self, request, obj=None):


class ProfileAdmin(NoBatchDeleteMixin, VersionAdmin):
fields = ('user', 'display_rank', 'about', 'organizations', 'timezone', 'language', 'ace_theme',
'math_engine', 'last_access', 'ip', 'mute', 'is_unlisted', 'is_banned_from_problem_voting',
'username_display_override', 'notes', 'is_totp_enabled', 'user_script', 'current_contest')
fieldsets = (
(None, {'fields': ('user', 'display_rank')}),
(_('User Settings'), {'fields': ('organizations', 'timezone', 'language', 'ace_theme', 'math_engine')}),
(_('Administration'), {'fields': ('is_external_user', 'mute', 'is_unlisted', 'is_totp_enabled',
'last_access', 'ip', 'current_contest', 'notes',
'username_display_override')}),
(_('Text Fields'), {'fields': ('about', 'user_script')}),
)
readonly_fields = ('user',)
list_display = ('admin_user_admin', 'email', 'is_totp_enabled', 'timezone_full',
'date_joined', 'last_access', 'ip', 'show_public')
ordering = ('user__username',)
search_fields = ('user__username', 'ip', 'user__email')
search_fields = ('user__username', 'user__first_name', 'user__last_name', 'ip', 'user__email')
list_filter = ('language', TimezoneFilter)
actions = ('recalculate_points',)
actions_on_top = True
Expand All @@ -83,22 +88,29 @@ def render_change_form(self, request, context, **kwargs):

def get_queryset(self, request):
return super(ProfileAdmin, self).get_queryset(request).select_related('user')

def get_fields(self, request, obj=None):
def get_fieldsets(self, request, obj=None):
if request.user.has_perm('judge.totp'):
fields = list(self.fields)
fields.insert(fields.index('is_totp_enabled') + 1, 'totp_key')
fields.insert(fields.index('totp_key') + 1, 'scratch_codes')
return tuple(fields)
fieldsets = self.fieldsets[:]
fields = list(fieldsets[2][1]['fields'])
if 'totp_key' not in fields:
fields.insert(fields.index('is_totp_enabled') + 1, 'totp_key')
fields.insert(fields.index('totp_key') + 1, 'scratch_codes')
fieldsets[2][1]['fields'] = tuple(fields)
return fieldsets
else:
return self.fields
return self.fieldsets

def get_readonly_fields(self, request, obj=None):
fields = self.readonly_fields
if not request.user.has_perm('judge.totp'):
fields += ('is_totp_enabled',)
return fields


def full_name(self, obj):
return obj.user.get_full_name()

full_name.short_description = _('Name')
@admin.display(description='')
def show_public(self, obj):
return format_html('<a href="{0}" style="white-space:nowrap;">{1}</a>',
Expand Down
5 changes: 5 additions & 0 deletions judge/admin/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,8 @@ def has_delete_permission(self, request, obj=None):
if result and obj is not None:
return not obj.online
return result

def save_model(self, request, obj, form, change):
if obj.id and obj.is_blocked and obj.online:
obj.disconnect(force=False)
super(JudgeAdmin, self).save_model(request, obj, form, change)
23 changes: 19 additions & 4 deletions judge/admin/submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.core.cache import cache
from django.core.exceptions import PermissionDenied
from django.db.models import Q
from django.http import HttpResponseRedirect
from django.http import HttpResponseRedirect, Http404
from django.shortcuts import get_object_or_404
from django.urls import path, reverse
from django.utils.html import format_html
Expand Down Expand Up @@ -64,7 +64,8 @@ class SubmissionTestCaseInline(admin.TabularInline):


class ContestSubmissionInline(admin.StackedInline):
fields = ('problem', 'participation', 'points')
fields = ('problem', 'participation', 'points', 'bonus', 'is_disqualified', 'updated_frozen')
readonly_fields = ('updated_frozen', 'is_disqualified')
model = ContestSubmission

def get_formset(self, request, obj=None, **kwargs):
Expand Down Expand Up @@ -120,7 +121,7 @@ class SubmissionAdmin(VersionAdmin):
actions = ('judge', 'recalculate_score')
list_display = ('id', 'problem_code', 'problem_name', 'user_column', 'execution_time', 'pretty_memory',
'points', 'language_column', 'status', 'result', 'judge_column')
list_filter = ('language', SubmissionStatusFilter, SubmissionResultFilter)
list_filter = ('language', 'judged_on', SubmissionStatusFilter, SubmissionResultFilter)
search_fields = ('problem__code', 'problem__name', 'user__user__username')
actions_on_top = True
actions_on_bottom = True
Expand Down Expand Up @@ -149,7 +150,9 @@ def has_add_permission(self, request):
def has_change_permission(self, request, obj=None):
if not request.user.has_perm('judge.edit_own_problem'):
return False
if request.user.has_perm('judge.edit_all_problem') or obj is None:
if obj is None:
return True
if request.user.has_perm('judge.edit_all_problem'):
return True
return obj.problem.is_editor(request.profile)

Expand Down Expand Up @@ -248,6 +251,7 @@ def judge_column(self, obj):
def get_urls(self):
return [
path('<int:id>/judge/', self.judge_view, name='judge_submission_rejudge'),
path('<int:id>disqualify/', self.disqualify_view, name='judge_submission_disqualify'),
] + super(SubmissionAdmin, self).get_urls()

def judge_view(self, request, id):
Expand All @@ -259,3 +263,14 @@ def judge_view(self, request, id):
raise PermissionDenied()
submission.judge(rejudge=True, rejudge_user=request.user)
return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))

def disqualify_view(self, request, id):
if not request.user.has_perm('judge.disqualify_submission'):
raise PermissionDenied()
submission = get_object_or_404(Submission, id=id)
if submission.contest_or_none is None:
raise Http404()
submission.contest.is_disqualified = not submission.contest.is_disqualified
submission.contest.save()
submission.contest.participation.recompute_results()
return HttpResponseRedirect(reverse('admin:judge_submission_change', args=(id,)))
25 changes: 7 additions & 18 deletions judge/bridge/judge_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import threading
import time
from collections import deque, namedtuple
from hashlib import sha256
from operator import itemgetter

from django import db
Expand Down Expand Up @@ -91,23 +92,11 @@ def on_disconnect(self):
if self._working:
Submission.objects.filter(id=self._working).update(status='IE', result='IE', error='')
json_log.error(self._make_json_log(sub=self._working, action='close', info='IE due to shutdown on grading'))

def _authenticate(self, id, key):
try:
judge = Judge.objects.get(name=id)
except Judge.DoesNotExist:
return False

if not hmac.compare_digest(judge.auth_key, key):
logger.warning('Judge authentication failure: %s', self.client_address)
json_log.warning(self._make_json_log(action='auth', judge=id, info='judge failed authentication'))

def _authenticate(self, id):
if id != sha256(self.client_address[0].encode()).hexdigest()[:8]:
return False

if judge.is_blocked:
json_log.warning(self._make_json_log(action='auth', judge=id, info='judge authenticated but is blocked'))
return False

return True
return Judge.objects.filter(name=id, last_ip=self.client_address[0], is_blocked=False).exists()

def _connected(self):
judge = self.judge = Judge.objects.get(name=self.name)
Expand Down Expand Up @@ -150,12 +139,12 @@ def send(self, data):
super().send(json.dumps(data, separators=(',', ':')))

def on_handshake(self, packet):
if 'id' not in packet or 'key' not in packet:
if 'id' not in packet:
logger.warning('Malformed handshake: %s', self.client_address)
self.close()
return

if not self._authenticate(packet['id'], packet['key']):
if not self._authenticate(packet['id']):
self.close()
return

Expand Down

0 comments on commit d69e319

Please sign in to comment.