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.
pull/1837/head
Matt Westcott 2015-10-16 15:45:27 +01:00
rodzic b534d64d8c
commit c025e1d3e5
3 zmienionych plików z 23 dodań i 4 usunięć

Wyświetl plik

@ -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
-----------------

Wyświetl plik

@ -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,
})

Wyświetl plik

@ -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)