NDOJ/judge/admin/runtime.py

165 lines
5.2 KiB
Python
Raw Normal View History

2020-01-21 06:35:58 +00:00
from django.conf.urls import url
from django.db.models import TextField
from django.forms import ModelForm, ModelMultipleChoiceField, TextInput
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404
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 reversion.admin import VersionAdmin
from django_ace import AceWidget
from judge.models import Judge, Problem
from judge.widgets import AdminHeavySelect2MultipleWidget, AdminPagedownWidget
class LanguageForm(ModelForm):
problems = ModelMultipleChoiceField(
2022-05-14 17:57:27 +00:00
label=_("Disallowed problems"),
2020-01-21 06:35:58 +00:00
queryset=Problem.objects.all(),
required=False,
2022-05-14 17:57:27 +00:00
help_text=_("These problems are NOT allowed to be submitted in this language"),
widget=AdminHeavySelect2MultipleWidget(data_view="problem_select2"),
)
2020-01-21 06:35:58 +00:00
class Meta:
if AdminPagedownWidget is not None:
2022-05-14 17:57:27 +00:00
widgets = {"description": AdminPagedownWidget}
2020-01-21 06:35:58 +00:00
class LanguageAdmin(VersionAdmin):
2022-05-14 17:57:27 +00:00
fields = (
"key",
"name",
"short_name",
"common_name",
"ace",
"pygments",
"info",
"description",
"template",
"problems",
)
list_display = ("key", "name", "common_name", "info")
2020-01-21 06:35:58 +00:00
form = LanguageForm
def save_model(self, request, obj, form, change):
super(LanguageAdmin, self).save_model(request, obj, form, change)
2022-05-14 17:57:27 +00:00
obj.problem_set.set(
Problem.objects.exclude(id__in=form.cleaned_data["problems"].values("id"))
)
2020-01-21 06:35:58 +00:00
def get_form(self, request, obj=None, **kwargs):
2022-05-14 17:57:27 +00:00
self.form.base_fields["problems"].initial = (
Problem.objects.exclude(id__in=obj.problem_set.values("id")).values_list(
"pk", flat=True
)
if obj
else []
)
2020-01-21 06:35:58 +00:00
form = super(LanguageAdmin, self).get_form(request, obj, **kwargs)
if obj is not None:
2022-05-14 17:57:27 +00:00
form.base_fields["template"].widget = AceWidget(
obj.ace, request.profile.ace_theme
)
2020-01-21 06:35:58 +00:00
return form
class GenerateKeyTextInput(TextInput):
def render(self, name, value, attrs=None, renderer=None):
text = super(TextInput, self).render(name, value, attrs)
2022-05-14 17:57:27 +00:00
return mark_safe(
text
+ format_html(
"""\
2020-01-21 06:35:58 +00:00
<a href="#" onclick="return false;" class="button" id="id_{0}_regen">Regenerate</a>
<script type="text/javascript">
django.jQuery(document).ready(function ($) {{
$('#id_{0}_regen').click(function () {{
var length = 100,
charset = "abcdefghijklnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789`~!@#$%^&*()_+-=|[]{{}};:,<>./?",
key = "";
for (var i = 0, n = charset.length; i < length; ++i) {{
key += charset.charAt(Math.floor(Math.random() * n));
}}
$('#id_{0}').val(key);
}});
}});
</script>
2022-05-14 17:57:27 +00:00
""",
name,
)
)
2020-01-21 06:35:58 +00:00
class JudgeAdminForm(ModelForm):
class Meta:
2022-05-14 17:57:27 +00:00
widgets = {"auth_key": GenerateKeyTextInput}
2020-01-21 06:35:58 +00:00
if AdminPagedownWidget is not None:
2022-05-14 17:57:27 +00:00
widgets["description"] = AdminPagedownWidget
2020-01-21 06:35:58 +00:00
class JudgeAdmin(VersionAdmin):
form = JudgeAdminForm
2022-05-14 17:57:27 +00:00
readonly_fields = (
"created",
"online",
"start_time",
"ping",
"load",
"last_ip",
"runtimes",
"problems",
)
2020-01-21 06:35:58 +00:00
fieldsets = (
2022-05-14 17:57:27 +00:00
(None, {"fields": ("name", "auth_key", "is_blocked")}),
(_("Description"), {"fields": ("description",)}),
(
_("Information"),
{"fields": ("created", "online", "last_ip", "start_time", "ping", "load")},
),
(_("Capabilities"), {"fields": ("runtimes", "problems")}),
2020-01-21 06:35:58 +00:00
)
2022-05-14 17:57:27 +00:00
list_display = ("name", "online", "start_time", "ping", "load", "last_ip")
ordering = ["-online", "name"]
2020-01-21 06:35:58 +00:00
def get_urls(self):
2022-05-14 17:57:27 +00:00
return [
url(
r"^(\d+)/disconnect/$",
self.disconnect_view,
name="judge_judge_disconnect",
),
url(
r"^(\d+)/terminate/$", self.terminate_view, name="judge_judge_terminate"
),
] + super(JudgeAdmin, self).get_urls()
2020-01-21 06:35:58 +00:00
def disconnect_judge(self, id, force=False):
judge = get_object_or_404(Judge, id=id)
judge.disconnect(force=force)
2022-05-14 17:57:27 +00:00
return HttpResponseRedirect(reverse("admin:judge_judge_changelist"))
2020-01-21 06:35:58 +00:00
def disconnect_view(self, request, id):
return self.disconnect_judge(id)
def terminate_view(self, request, id):
return self.disconnect_judge(id, force=True)
def get_readonly_fields(self, request, obj=None):
if obj is not None and obj.online:
2022-05-14 17:57:27 +00:00
return self.readonly_fields + ("name",)
2020-01-21 06:35:58 +00:00
return self.readonly_fields
def has_delete_permission(self, request, obj=None):
result = super(JudgeAdmin, self).has_delete_permission(request, obj)
if result and obj is not None:
return not obj.online
return result
if AdminPagedownWidget is not None:
formfield_overrides = {
2022-05-14 17:57:27 +00:00
TextField: {"widget": AdminPagedownWidget},
2020-01-21 06:35:58 +00:00
}