From 33fbbed19da9417266fb90340724296b15061ddf Mon Sep 17 00:00:00 2001 From: Rohit Sharma Date: Thu, 11 Jan 2024 18:01:00 +0000 Subject: [PATCH] Resolve error when specifying 'slug' as a read-only field (#11447) --- CHANGELOG.txt | 1 + docs/releases/6.0.md | 2 ++ wagtail/admin/panels/title_field_panel.py | 6 +++++- wagtail/admin/tests/test_edit_handlers.py | 19 +++++++++++++++++++ 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index ea4077192420..29359c789b07 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -57,6 +57,7 @@ Changelog * Fix: Ensure workflow dashboard panels work when the page/snippet is missing (Sage Abdullah) * Fix: Prevent a ValueError with `FormSubmissionsPanel` on Django 5.0 when creating a new form page (Matt Westcott) * Fix: Avoid duplicate entries in "Recent edits" panel when copying pages (Matt Westcott) + * Fix: Prevent TitleFieldPanel from raising an error when the slug field is missing or read-only (Rohit Sharma) * Docs: Document, for contributors, the use of translate string literals passed as arguments to tags and filters using `_()` within templates (Chiemezuo Akujobi) * Docs: Document all features for the Documents app in one location (Neeraj Yetheendran) * Docs: Add section to testing docs about creating pages and working with page content (Mariana Bedran Lesche) diff --git a/docs/releases/6.0.md b/docs/releases/6.0.md index e7da23010aac..1d476a7edba9 100644 --- a/docs/releases/6.0.md +++ b/docs/releases/6.0.md @@ -85,6 +85,8 @@ Thank you to Thibaud Colas and Badr Fourane for their work on this feature. * Prevent a ValueError with `FormSubmissionsPanel` on Django 5.0 when creating a new form page (Matt Westcott) * Add ability to [customise a page's copy form](custom_page_copy_form) including an auto-incrementing slug example (Neeraj Yetheendran) * Avoid duplicate entries in "Recent edits" panel when copying pages (Matt Westcott) + * Prevent TitleFieldPanel from raising an error when the slug field is missing or read-only (Rohit Sharma) + ### Documentation diff --git a/wagtail/admin/panels/title_field_panel.py b/wagtail/admin/panels/title_field_panel.py index e5298dfa9e5d..a906cadd2791 100644 --- a/wagtail/admin/panels/title_field_panel.py +++ b/wagtail/admin/panels/title_field_panel.py @@ -77,7 +77,11 @@ def get_attrs(self): actions = [widget.attrs.get("data-action", None)] + self.apply_actions attrs["data-action"] = " ".join(filter(None, actions)) - targets = [self.get_target_selector(target) for target in panel.targets] + targets = [ + self.get_target_selector(target) + for target in panel.targets + if target in self.form.fields + ] attrs["data-w-sync-target-value"] = ", ".join(filter(None, targets)) placeholder = self.get_placeholder() diff --git a/wagtail/admin/tests/test_edit_handlers.py b/wagtail/admin/tests/test_edit_handlers.py index 9074c6f999f2..ab3a33acfa8a 100644 --- a/wagtail/admin/tests/test_edit_handlers.py +++ b/wagtail/admin/tests/test_edit_handlers.py @@ -2258,6 +2258,25 @@ def test_default_title_field_panel(self): "focus->w-sync#check blur->w-sync#apply change->w-sync#apply keyup->w-sync#apply", ) + def test_form_without_slugfield(self): + html = self.get_edit_handler_html(ObjectList([TitleFieldPanel("title")])) + + self.assertIsNotNone(html.find(attrs={"class": "w-panel title"})) + + attrs = html.find("input").attrs + self.assertEqual(attrs["data-w-sync-target-value"], "") + + def test_form_with_readonly_slugfield(self): + html = self.get_edit_handler_html( + ObjectList([TitleFieldPanel("title"), FieldPanel("slug", read_only=True)]), + instance=EventPage(), + ) + + self.assertIsNotNone(html.find(attrs={"class": "w-panel title"})) + + attrs = html.find("input").attrs + self.assertEqual(attrs["data-w-sync-target-value"], "") + def test_not_using_apply_actions_if_live(self): """ If the Page (or any model) has `live = True`, do not apply the actions by default.