Tweak subpage_types logic so that subpage_types=None (or undefined) means 'no restriction on subpages', freeing up [] to mean 'no subpages allowed'

This commit is contained in:
Matt Westcott 2014-06-17 21:48:48 +01:00
parent 8d3ac405c4
commit 9071a8e004
3 changed files with 32 additions and 22 deletions

View file

@ -306,4 +306,4 @@ class BusinessIndex(Page):
subpage_types = ['tests.BusinessChild']
class BusinessChild(Page):
pass
subpage_types = []

View file

@ -688,6 +688,12 @@ class TestSubpageBusinessRules(TestCase, WagtailTestUtils):
self.business_index.slug = "business-index"
self.root_page.add_child(instance=self.business_index)
# Add business child
self.business_child = BusinessChild()
self.business_child.title = "Business Child"
self.business_child.slug = "business-child"
self.business_index.add_child(instance=self.business_child)
# Login
self.login()
@ -702,3 +708,9 @@ class TestSubpageBusinessRules(TestCase, WagtailTestUtils):
self.assertEqual(response.status_code, 200)
self.assertNotContains(response, 'Standard Child')
self.assertContains(response, 'Business Child')
def test_business_child_subpage(self):
response = self.client.get(reverse('wagtailadmin_pages_add_subpage', args=(self.business_child.id, )))
self.assertEqual(response.status_code, 200)
self.assertNotContains(response, 'Standard Child')
self.assertEqual(0, len(response.context['page_types']))

View file

@ -253,9 +253,6 @@ class Page(MP_Node, ClusterableModel, Indexed):
def __unicode__(self):
return self.title
# by default pages do not allow any kind of subpages
subpage_types = []
is_abstract = True # don't offer Page in the list of page types a superuser can create
def set_url_path(self, parent):
@ -478,29 +475,30 @@ class Page(MP_Node, ClusterableModel, Indexed):
where required
"""
if cls._clean_subpage_types is None:
if len(cls.subpage_types) == 0:
subpage_types = getattr(cls, 'subpage_types', None)
if subpage_types is None:
# if subpage_types is not specified on the Page class, allow all page types as subpages
res = get_page_types()
else:
res = []
for page_type in cls.subpage_types:
if isinstance(page_type, basestring):
try:
app_label, model_name = page_type.split(".")
except ValueError:
# If we can't split, assume a model in current app
app_label = cls._meta.app_label
model_name = page_type
for page_type in cls.subpage_types:
if isinstance(page_type, basestring):
try:
app_label, model_name = page_type.split(".")
except ValueError:
# If we can't split, assume a model in current app
app_label = cls._meta.app_label
model_name = page_type
model = get_model(app_label, model_name)
if model:
res.append(ContentType.objects.get_for_model(model))
else:
raise NameError(_("name '{0}' (used in subpage_types list) is not defined.").format(page_type))
model = get_model(app_label, model_name)
if model:
res.append(ContentType.objects.get_for_model(model))
else:
raise NameError(_("name '{0}' (used in subpage_types list) is not defined.").format(page_type))
else:
# assume it's already a model class
res.append(ContentType.objects.get_for_model(page_type))
# assume it's already a model class
res.append(ContentType.objects.get_for_model(page_type))
cls._clean_subpage_types = res