Skip to content

Instantly share code, notes, and snippets.

@tombola
Last active July 30, 2020 11:18
Show Gist options
  • Save tombola/cd92a6c5e0c373c41004ea945370079b to your computer and use it in GitHub Desktop.
Save tombola/cd92a6c5e0c373c41004ea945370079b to your computer and use it in GitHub Desktop.
"""
Example usage:
dj shell < wagtail_list_allowed_subpages.py
Returns markdown formatted list of which page types can be added under which.
"""
def list_valid_page_subpages(readable=False, shorten=False):
from wagtail.core.models import Page
def capitalise(string):
return " ".join([word.capitalize() for word in string.split(" ")])
parent_pages = [c for c in Page.clean_parent_page_models() if c is not Page]
if readable:
for page_class in parent_pages:
verbose_name = page_class._meta.verbose_name or page_class.__name__
if shorten:
parent_name = capitalise(verbose_name.replace("page", ""))
print(f"- {parent_name}")
for subpage_class in page_class.creatable_subpage_models():
subpage_name = capitalise(verbose_name.replace("page", ""))
print(f" - {subpage_name}")
else:
print(f"- {capitalise(verbose_name)}")
for subpage_class in page_class.creatable_subpage_models():
sub_verbose_name = (
page_class._meta.verbose_name or page_class.__name__
)
print(f" - {capitalise(sub_verbose_name)}")
else:
for page_class in parent_pages:
print(f"- {page_class.__name__}")
for subpage_class in page_class.creatable_subpage_models():
print(f" - {subpage_class.__name__}")
list_valid_page_subpages()
# Alternatively return as capitalised with spaces (eg for client)
# list_valid_page_subpages(readable=True)
# Alternatively return as capitalised with spaces, and word Page removed
# eg NewsIndexPage becomes 'News Index'
# list_valid_page_subpages(readable=True, shorten=True)
@tombola
Copy link
Author

tombola commented Jul 30, 2020

Example output:

  • EventPage
  • EventIndexPage
    • EventPage
  • FormPage
  • HomePage
    • EventIndexPage
    • FormPage
    • NewsIndex
    • InformationPage
    • IndexPage
  • NewsPage
  • NewsIndex
    • NewsPage
  • InformationPage
    • FormPage
    • InformationPage
    • IndexPage
  • IndexPage
    • FormPage
    • InformationPage
    • IndexPage

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment