Add custom module form with custom clean method

Relates to MS-758
This commit is contained in:
Ramon Wenger 2023-08-23 15:25:52 +02:00
parent dbec89ca8d
commit 2e5fa555d6
4 changed files with 66 additions and 38 deletions

View File

@ -1,8 +1,10 @@
from django.db import models
from django import forms
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from wagtail.admin.panels import FieldPanel, TabbedInterface, ObjectList
from wagtail.fields import RichTextField
from wagtail.admin.forms import WagtailAdminPageForm
from django.utils.translation import gettext as _
from core.constants import DEFAULT_RICH_TEXT_FEATURES
from core.wagtail_utils import StrictHierarchyPage, get_default_settings
@ -42,6 +44,22 @@ class ModuleCategory(models.Model):
return f"{self.name}"
class ModulePageForm(WagtailAdminPageForm):
def clean(self):
cleaned_data = super().clean()
if "slug" in self.cleaned_data:
page_slug = cleaned_data["slug"]
if not Module._slug_is_available(page_slug, self.instance):
self.add_error(
"slug",
forms.ValidationError(
_("The slug '%(page_slug)s' is already in use")
% {"page_slug": page_slug}
),
)
return cleaned_data
class Module(StrictHierarchyPage):
class Meta:
verbose_name = "Modul"
@ -81,6 +99,7 @@ class Module(StrictHierarchyPage):
FieldPanel("teaser"),
FieldPanel("intro"),
]
base_form_class = ModulePageForm
edit_handler = TabbedInterface(
[ObjectList(content_panels, heading="Content"), get_default_settings()]
@ -150,6 +169,13 @@ class Module(StrictHierarchyPage):
def get_admin_display_title(self):
return f"{self.meta_title} - {self.title}"
@staticmethod
def _slug_is_available(slug, page):
# modeled after `Page._slug_is_available`
modules = Module.objects.filter(slug=slug).not_page(page)
return not modules.exists()
class RecentModule(models.Model):
module = models.ForeignKey(

View File

@ -6,7 +6,6 @@ from wagtail.models import Locale
from api.utils import get_object
from core.logger import get_logger
from .connections import TopicConnection, ModuleConnection
<<<<<<< HEAD
from .nodes import (
ContentBlockNode,
ChapterNode,

View File

@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2023-04-12 13:50+0000\n"
"POT-Creation-Date: 2023-08-23 13:04+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
@ -22,7 +22,7 @@ msgstr ""
msgid "assignment"
msgstr "Auftrag"
#: assignments/models.py:52 books/models/module.py:49
#: assignments/models.py:52
msgid "assignments"
msgstr "Aufträge"
@ -56,45 +56,23 @@ msgstr "instrumente"
msgid "Instruments"
msgstr "Instrumente"
#: books/models/module.py:43
msgid "Assignment"
msgstr "Auftrag"
#: books/models/module.py:45
#| msgid "assignments"
msgid "linked assignments"
msgstr "Verlinkte Aufträge"
#: books/models/module.py:47 books/models/module.py:57
#: books/models/module.py:30
#, python-format
msgid ""
"These %s are automatically linked, they are shown here only to provide an "
"overview. Please don't change anything here."
msgstr ""
"Diese %s sind automatisch verlinkt, sie werden hier als Übersicht angezeigt. "
"Bitte hier nichts verändern."
msgid "The slug '%(page_slug)s' is already in use"
msgstr "Der Slug '%(page_slug)s' wird bereits verwendet"
#: books/models/module.py:53
#| msgid "surveys"
msgid "linked surveys"
msgstr "Verlinkte Surveys"
#: books/models/module.py:54
msgid "Survey"
msgstr "Survey"
#: books/models/module.py:59
msgid "surveys"
msgstr "Übungen"
#: core/settings.py:193
#: core/settings.py:197
msgid "German"
msgstr ""
#: core/settings.py:194
#: core/settings.py:198
msgid "English"
msgstr ""
#: core/settings.py:199
msgid "French"
msgstr ""
#: core/templates/registration/login.html:9
msgid "Melden Sie sich jetzt an."
msgstr ""
@ -300,11 +278,11 @@ msgstr ""
msgid "Drag"
msgstr ""
#: core/templates/wagtailadmin/pages/listing/_list.html:79
#: core/templates/wagtailadmin/pages/listing/_list.html:80
msgid "Edit this page"
msgstr ""
#: core/templates/wagtailadmin/pages/listing/_list.html:106
#: core/templates/wagtailadmin/pages/listing/_list.html:107
#, python-format
msgid ""
"%(time_period)s\n"
@ -327,7 +305,7 @@ msgstr ""
msgid "News Article URL"
msgstr ""
#: news/models.py:12
#: news/models.py:13
msgid "Image Source"
msgstr ""
@ -367,6 +345,31 @@ msgstr ""
msgid "Das alte Passwort muss angegeben werden"
msgstr ""
#~ msgid "Assignment"
#~ msgstr "Auftrag"
#~| msgid "assignments"
#~ msgid "linked assignments"
#~ msgstr "Verlinkte Aufträge"
#, python-format
#~ msgid ""
#~ "These %s are automatically linked, they are shown here only to provide an "
#~ "overview. Please don't change anything here."
#~ msgstr ""
#~ "Diese %s sind automatisch verlinkt, sie werden hier als Übersicht "
#~ "angezeigt. Bitte hier nichts verändern."
#~| msgid "surveys"
#~ msgid "linked surveys"
#~ msgstr "Verlinkte Surveys"
#~ msgid "Survey"
#~ msgstr "Survey"
#~ msgid "surveys"
#~ msgstr "Übungen"
#~ msgid "Assignments"
#~ msgstr "Aufträge"