diff --git a/apps/submissions/admin.py b/apps/submissions/admin.py new file mode 100644 index 0000000..19d5b95 --- /dev/null +++ b/apps/submissions/admin.py @@ -0,0 +1,32 @@ +from typing import TYPE_CHECKING + +from django.contrib.admin import ModelAdmin, register +from django.forms import CharField, ModelForm, Textarea +from django.utils.translation import gettext_lazy as _ + +from apps.submissions.models import Submission + +if TYPE_CHECKING: + SubmissionAdminBase = ModelAdmin[Submission] + SubmissionModelFormBase = ModelForm[Submission] +else: + SubmissionAdminBase = ModelAdmin + SubmissionModelFormBase = ModelForm + + +class SubmissionModelForm(SubmissionModelFormBase): + code = CharField(widget=Textarea(attrs={"rows": 20, "cols": 80})) + + class Meta: + model = Submission + fields = "__all__" + + +@register(Submission) +class SubmissionAdmin(SubmissionAdminBase): + form = SubmissionModelForm + + list_display = ("__str__", "author", "task") + list_filter = ("author", "task", "created_at") + + fieldsets = [(_("Details"), {"fields": ("author", "task", "code")})] diff --git a/apps/submissions/tests.py b/apps/submissions/tests.py index c17c6ec..4f680f2 100644 --- a/apps/submissions/tests.py +++ b/apps/submissions/tests.py @@ -1,10 +1,13 @@ from datetime import timedelta +from django.contrib.admin.sites import AdminSite from django.core.exceptions import ValidationError from django.test import TestCase from django.utils import timezone +from django.utils.translation import gettext as _ from apps.contests.models import Contest +from apps.submissions.admin import SubmissionAdmin from apps.submissions.models import Submission from apps.tasks.models import Task from apps.users.models import User @@ -71,3 +74,21 @@ def test_submission_code_min_length_validator(self) -> None: submission.full_clean() self.assertEqual(context.exception.messages, expected) + + +class SubmissionAdminTest(TestCase): + def setUp(self) -> None: + self.site = AdminSite() + self.submission_admin = SubmissionAdmin(Submission, self.site) + + def test_list_display(self) -> None: + expected = ("__str__", "author", "task") + self.assertEqual(self.submission_admin.list_display, expected) + + def test_list_filter(self) -> None: + expected = ("author", "task", "created_at") + self.assertEqual(self.submission_admin.list_filter, expected) + + def test_fieldsets(self) -> None: + expected = [(_("Details"), {"fields": ("author", "task", "code")})] + self.assertEqual(self.submission_admin.fieldsets, expected)