Ensure the copy page form only allows choosing valid parents for the copy

Ensure the Page chooser disables the types of pages that do not follow the content hierarchy when trying to copy a page.

Fixes #11593
pull/12672/head
Mauro Soche 2024-02-06 14:30:16 -05:00 zatwierdzone przez LB (Ben Johnston)
rodzic 547e4d3731
commit f177c02da8
5 zmienionych plików z 37 dodań i 1 usunięć

Wyświetl plik

@ -12,6 +12,7 @@ Changelog
* Limit tags autocompletion to 10 items and add delay to avoid performance issues with large number of matching tags (Aayushman Singh)
* Add the ability to restrict what types of requests a Pages supports via `allowed_http_methods` (Andy Babic)
* Allow plain strings in panel definitions as shorthand for `FieldPanel` / `InlinePanel` (Matt Westcott)
* Only allow selection of valid new parents within the copy Page view (Mauro Soche)
* Fix: Improve handling of translations for bulk page action confirmation messages (Matt Westcott)
* Fix: Ensure custom rich text feature icons are correctly handled when provided as a list of SVG paths (Temidayo Azeez, Joel William, LB (Ben) Johnston)
* Fix: Ensure manual edits to `StreamField` values do not throw an error (Stefan Hammer)

Wyświetl plik

@ -859,6 +859,7 @@
* Strapchay
* Alex Fulcher
* Harsh Dange
* Mauro Soche
## Translators

Wyświetl plik

@ -21,6 +21,7 @@ depth: 1
* Limit tags autocompletion to 10 items and add delay to avoid performance issues with large number of matching tags (Aayushman Singh)
* Add the ability to restrict what types of requests a Pages supports via [`allowed_http_methods`](#wagtail.models.Page.allowed_http_methods) (Andy Babic)
* Allow plain strings in panel definitions as shorthand for `FieldPanel` / `InlinePanel` (Matt Westcott)
* Only allow selection of valid new parents within the copy Page view (Mauro Soche)
### Bug fixes

Wyświetl plik

@ -30,7 +30,11 @@ class CopyForm(forms.Form):
self.fields["new_parent_page"] = forms.ModelChoiceField(
initial=self.page.get_parent(),
queryset=Page.objects.all(),
widget=widgets.AdminPageChooser(can_choose_root=True, user_perms="copy_to"),
widget=widgets.AdminPageChooser(
target_models=self.page.specific_class.allowed_parent_page_models(),
can_choose_root=True,
user_perms="copy_to",
),
label=_("New parent page"),
help_text=_("This copy will be a child of this given parent page."),
)

Wyświetl plik

@ -249,6 +249,35 @@ class TestChooserBrowseChild(WagtailTestUtils, TestCase):
self.assertIn(event_page.id, pages)
self.assertTrue(pages[self.child_page.id].can_choose)
def test_with_multiple_specific_page_types_display_warning(self):
# Add a page that is not a SimplePage
event_page = EventPage(
title="event",
location="the moon",
audience="public",
cost="free",
date_from="2001-01-01",
)
self.root_page.add_child(instance=event_page)
# Send request
response = self.get({"page_type": "tests.simplepage,tests.eventpage"})
self.assertEqual(response.status_code, 200)
self.assertEqual(
response.context["page_type_names"], ["Simple page", "Event page"]
)
html = response.json().get("html")
expected = """
<p class="help-block help-warning">
<svg class="icon icon-warning icon" aria-hidden="true"><use href="#icon-warning"></use></svg>
Only the following page types may be chosen for this field: Simple page, Event page. Search results will exclude pages of other types.
</p>
"""
self.assertTagInHTML(expected, html)
def test_with_unknown_page_type(self):
response = self.get({"page_type": "foo.bar"})
self.assertEqual(response.status_code, 404)