NDOJ/judge/admin/taxon.py

59 lines
2.1 KiB
Python
Raw Normal View History

2020-01-21 06:35:58 +00:00
from django.contrib import admin
from django.forms import ModelForm, ModelMultipleChoiceField
from django.utils.translation import gettext_lazy as _
from judge.models import Problem
from judge.widgets import AdminHeavySelect2MultipleWidget
class ProblemGroupForm(ModelForm):
problems = ModelMultipleChoiceField(
2022-05-14 17:57:27 +00:00
label=_("Included 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 included in this group of problems"),
widget=AdminHeavySelect2MultipleWidget(data_view="problem_select2"),
)
2020-01-21 06:35:58 +00:00
class ProblemGroupAdmin(admin.ModelAdmin):
2022-05-14 17:57:27 +00:00
fields = ("name", "full_name", "problems")
2020-01-21 06:35:58 +00:00
form = ProblemGroupForm
def save_model(self, request, obj, form, change):
super(ProblemGroupAdmin, self).save_model(request, obj, form, change)
2022-05-14 17:57:27 +00:00
obj.problem_set.set(form.cleaned_data["problems"])
2020-01-21 06:35:58 +00:00
obj.save()
def get_form(self, request, obj=None, **kwargs):
2022-05-14 17:57:27 +00:00
self.form.base_fields["problems"].initial = (
[o.pk for o in obj.problem_set.all()] if obj else []
)
2020-01-21 06:35:58 +00:00
return super(ProblemGroupAdmin, self).get_form(request, obj, **kwargs)
class ProblemTypeForm(ModelForm):
problems = ModelMultipleChoiceField(
2022-05-14 17:57:27 +00:00
label=_("Included 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 included in this type of problems"),
widget=AdminHeavySelect2MultipleWidget(data_view="problem_select2"),
)
2020-01-21 06:35:58 +00:00
class ProblemTypeAdmin(admin.ModelAdmin):
2022-05-14 17:57:27 +00:00
fields = ("name", "full_name", "problems")
2020-01-21 06:35:58 +00:00
form = ProblemTypeForm
def save_model(self, request, obj, form, change):
super(ProblemTypeAdmin, self).save_model(request, obj, form, change)
2022-05-14 17:57:27 +00:00
obj.problem_set.set(form.cleaned_data["problems"])
2020-01-21 06:35:58 +00:00
obj.save()
def get_form(self, request, obj=None, **kwargs):
2022-05-14 17:57:27 +00:00
self.form.base_fields["problems"].initial = (
[o.pk for o in obj.problem_set.all()] if obj else []
)
2020-01-21 06:35:58 +00:00
return super(ProblemTypeAdmin, self).get_form(request, obj, **kwargs)