From c025e1d3e5b0332a0aef66b2721d96459591dd35 Mon Sep 17 00:00:00 2001 From: Matt Westcott Date: Fri, 16 Oct 2015 15:45:27 +0100 Subject: [PATCH] Add a can_choose_root flag to PageChooserPanel. This restores the previous behaviour, since there are plausible scenarios where choosing the root might be appropriate. --- docs/reference/pages/panels.rst | 7 +++++-- wagtail/wagtailadmin/edit_handlers.py | 6 ++++-- wagtail/wagtailadmin/tests/test_edit_handlers.py | 14 ++++++++++++++ 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/docs/reference/pages/panels.rst b/docs/reference/pages/panels.rst index 105f8ae7a..6d16f6057 100644 --- a/docs/reference/pages/panels.rst +++ b/docs/reference/pages/panels.rst @@ -94,7 +94,7 @@ FieldRowPanel PageChooserPanel ---------------- -.. class:: PageChooserPanel(field_name, page_type=None) +.. class:: PageChooserPanel(field_name, page_type=None, can_choose_root=False) You can explicitly link :class:`~wagtail.wagtailcore.models.Page`-derived models together using the :class:`~wagtail.wagtailcore.models.Page` model and ``PageChooserPanel``. @@ -117,10 +117,13 @@ PageChooserPanel PageChooserPanel('related_page', 'demo.PublisherPage'), ] - ``PageChooserPanel`` takes two arguments: a field name and an optional page type. Specifying a page type (in the form of an ``"appname.modelname"`` string) will filter the chooser to display only pages of that type. A list or tuple of page types can also be passed in, to allow choosing a page that matches any of those page types:: + ``PageChooserPanel`` takes one required argument, the field name. Optionally, specifying a page type (in the form of an ``"appname.modelname"`` string) will filter the chooser to display only pages of that type. A list or tuple of page types can also be passed in, to allow choosing a page that matches any of those page types:: PageChooserPanel('related_page', ['demo.PublisherPage', 'demo.AuthorPage']) + Passing ``can_choose_root=True`` will allow the editor to choose the tree root as a page. Normally this would be undesirable, since the tree root is never a usable page, but in some specialised cases it may be appropriate; for example, a page with an automatic "related articles" feed could use a PageChooserPanel to select which subsection articles will be taken from, with the root corresponding to 'everywhere'. + + ImageChooserPanel ----------------- diff --git a/wagtail/wagtailadmin/edit_handlers.py b/wagtail/wagtailadmin/edit_handlers.py index 9dcce9ddc..522b62cb5 100644 --- a/wagtail/wagtailadmin/edit_handlers.py +++ b/wagtail/wagtailadmin/edit_handlers.py @@ -552,7 +552,7 @@ class BasePageChooserPanel(BaseChooserPanel): @classmethod def widget_overrides(cls): return {cls.field_name: widgets.AdminPageChooser( - content_type=cls.target_content_type())} + content_type=cls.target_content_type(), can_choose_root=cls.can_choose_root)} @classmethod def target_content_type(cls): @@ -579,7 +579,7 @@ class BasePageChooserPanel(BaseChooserPanel): class PageChooserPanel(object): - def __init__(self, field_name, page_type=None): + def __init__(self, field_name, page_type=None, can_choose_root=False): self.field_name = field_name if page_type: @@ -590,12 +590,14 @@ class PageChooserPanel(object): page_type = [] self.page_type = page_type + self.can_choose_root = can_choose_root def bind_to_model(self, model): return type(str('_PageChooserPanel'), (BasePageChooserPanel,), { 'model': model, 'field_name': self.field_name, 'page_type': self.page_type, + 'can_choose_root': self.can_choose_root, }) diff --git a/wagtail/wagtailadmin/tests/test_edit_handlers.py b/wagtail/wagtailadmin/tests/test_edit_handlers.py index f12fdeff7..c82f07524 100644 --- a/wagtail/wagtailadmin/tests/test_edit_handlers.py +++ b/wagtail/wagtailadmin/tests/test_edit_handlers.py @@ -370,6 +370,20 @@ class TestPageChooserPanel(TestCase): self.assertIn(expected_js, result) + def test_render_js_init_with_can_choose_root_true(self): + # construct an alternative page chooser panel object, with can_choose_root=True + MyPageChooserPanel = PageChooserPanel('page', can_choose_root=True).bind_to_model(PageChooserModel) + PageChooserForm = MyPageChooserPanel.get_form_class(PageChooserModel) + + form = PageChooserForm(instance=self.test_instance) + page_chooser_panel = MyPageChooserPanel(instance=self.test_instance, form=form) + result = page_chooser_panel.render_as_field() + + # the canChooseRoot flag on createPageChooser should now be true + expected_js = 'createPageChooser("{id}", ["{model}"], {parent}, true);'.format( + id="id_page", model="wagtailcore.page", parent=self.events_index_page.id) + self.assertIn(expected_js, result) + def test_get_chosen_item(self): result = self.page_chooser_panel.get_chosen_item() self.assertEqual(result, self.christmas_page)