Skip to content

add script to restructure RootPage/HomePage #1622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion books/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1183,7 +1183,7 @@ def books(self):

template = 'page.html'

parent_page_types = ['pages.HomePage']
parent_page_types = ['pages.HomePage', 'pages.RootPage']
subpage_types = ['books.Book']
max_count = 1

Expand Down
4 changes: 2 additions & 2 deletions news/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ class NewsIndex(Page):
]

subpage_types = ['news.NewsArticle']
parent_page_types = ['pages.HomePage']
parent_page_types = ['pages.HomePage', 'pages.RootPage']
max_count = 1

def get_url_parts(self, *args, **kwargs):
Expand Down Expand Up @@ -621,7 +621,7 @@ def releases(self):
]

subpage_types = ['news.PressRelease']
parent_page_types = ['pages.HomePage']
parent_page_types = ['pages.HomePage', 'pages.RootPage']
max_count = 1


Expand Down
54 changes: 54 additions & 0 deletions pages/management/commands/restructure_root_page.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from django.core.management.base import BaseCommand
from wagtail.models import Page, Site
from pages.models import HomePage, RootPage

Check warning on line 3 in pages/management/commands/restructure_root_page.py

View check run for this annotation

Codecov / codecov/patch

pages/management/commands/restructure_root_page.py#L1-L3

Added lines #L1 - L3 were not covered by tests

class Command(BaseCommand):
help = (

Check warning on line 6 in pages/management/commands/restructure_root_page.py

View check run for this annotation

Codecov / codecov/patch

pages/management/commands/restructure_root_page.py#L5-L6

Added lines #L5 - L6 were not covered by tests
"Move children of HomePage to RootPage, elevate RootPage to site root, and delete HomePage.\n"
"⚠️ DRY RUN BY DEFAULT — use --commit to make changes."
)

def add_arguments(self, parser):
parser.add_argument(

Check warning on line 12 in pages/management/commands/restructure_root_page.py

View check run for this annotation

Codecov / codecov/patch

pages/management/commands/restructure_root_page.py#L11-L12

Added lines #L11 - L12 were not covered by tests
'--commit',
action='store_true',
help="Apply the changes. Without this flag, the command runs in dry-run mode.",
)

def handle(self, *args, **options):
commit = options['commit']

Check warning on line 19 in pages/management/commands/restructure_root_page.py

View check run for this annotation

Codecov / codecov/patch

pages/management/commands/restructure_root_page.py#L18-L19

Added lines #L18 - L19 were not covered by tests

old_home = HomePage.objects.first()
new_root = RootPage.objects.first()

Check warning on line 22 in pages/management/commands/restructure_root_page.py

View check run for this annotation

Codecov / codecov/patch

pages/management/commands/restructure_root_page.py#L21-L22

Added lines #L21 - L22 were not covered by tests

if not old_home or not new_root:
self.stderr.write(self.style.ERROR("Missing HomePage or RootPage instance."))
return

Check warning on line 26 in pages/management/commands/restructure_root_page.py

View check run for this annotation

Codecov / codecov/patch

pages/management/commands/restructure_root_page.py#L24-L26

Added lines #L24 - L26 were not covered by tests

self.stdout.write(self.style.WARNING(

Check warning on line 28 in pages/management/commands/restructure_root_page.py

View check run for this annotation

Codecov / codecov/patch

pages/management/commands/restructure_root_page.py#L28

Added line #L28 was not covered by tests
f"{'[COMMIT MODE]' if commit else '[DRY RUN]'} Starting restructure..."
))

# Move children of HomePage (except RootPage) under RootPage
for child in old_home.get_children().exclude(id=new_root.id):
self.stdout.write(f" - Would move {child.title} (ID: {child.id}) under RootPage")
if commit:
child.move(new_root, pos='last-child')

Check warning on line 36 in pages/management/commands/restructure_root_page.py

View check run for this annotation

Codecov / codecov/patch

pages/management/commands/restructure_root_page.py#L33-L36

Added lines #L33 - L36 were not covered by tests

# Move RootPage to root
wagtail_root = Page.get_first_root_node()
self.stdout.write(f" - Would move RootPage (ID: {new_root.id}) to be child of root")
if commit:
new_root.move(wagtail_root, pos='last-child')

Check warning on line 42 in pages/management/commands/restructure_root_page.py

View check run for this annotation

Codecov / codecov/patch

pages/management/commands/restructure_root_page.py#L39-L42

Added lines #L39 - L42 were not covered by tests

# Update Site root
site = Site.objects.get(is_default_site=True)
self.stdout.write(f" - Would set Site.root_page to RootPage (ID: {new_root.id})")
if commit:
site.root_page = new_root
site.save()

Check warning on line 49 in pages/management/commands/restructure_root_page.py

View check run for this annotation

Codecov / codecov/patch

pages/management/commands/restructure_root_page.py#L45-L49

Added lines #L45 - L49 were not covered by tests

if commit:
self.stdout.write(self.style.SUCCESS("✅ Restructure complete. Site root updated."))

Check warning on line 52 in pages/management/commands/restructure_root_page.py

View check run for this annotation

Codecov / codecov/patch

pages/management/commands/restructure_root_page.py#L51-L52

Added lines #L51 - L52 were not covered by tests
else:
self.stdout.write(self.style.WARNING("Dry run complete. No changes were made."))

Check warning on line 54 in pages/management/commands/restructure_root_page.py

View check run for this annotation

Codecov / codecov/patch

pages/management/commands/restructure_root_page.py#L54

Added line #L54 was not covered by tests
43 changes: 21 additions & 22 deletions pages/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,6 @@ class RootPage(Page):
# TODO: we are allowing this to be built as a child of the homepage. Not ideal.
# Once the home page is released, use something to migrate homepage children to root page and remove this parent type.
parent_page_types = ['wagtailcore.Page', 'pages.HomePage']
subpage_types = ['pages.FlexPage'] # which might also require allowing all pages to be children.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it'll still be possible to create new flexpages after this right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, this is just saying that there are no restrictions on what can be a subpage of RootPage


def __str__(self):
return self.path
Expand Down Expand Up @@ -345,7 +344,7 @@ def get_where_map(self):
]

template = 'page.html'
parent_page_types = ['pages.HomePage']
parent_page_types = ['pages.HomePage', 'pages.RootPage']
max_count = 1


Expand Down Expand Up @@ -898,7 +897,7 @@ class Meta:

max_count = 1
template = 'page.html'
parent_page_types = ['pages.HomePage']
parent_page_types = ['pages.HomePage', 'pages.RootPage']
subpage_types = ['pages.K12Subject']


Expand Down Expand Up @@ -945,7 +944,7 @@ class ContactUs(Page):

template = 'page.html'

parent_page_types = ['pages.HomePage']
parent_page_types = ['pages.HomePage', 'pages.RootPage']
max_count = 1


Expand Down Expand Up @@ -1069,7 +1068,7 @@ class Supporters(Page):

template = 'page.html'

parent_page_types = ['pages.HomePage']
parent_page_types = ['pages.HomePage', 'pages.RootPage']
max_count = 1


Expand Down Expand Up @@ -1195,7 +1194,7 @@ def get_section_2_image_2(self):

template = 'page.html'

parent_page_types = ['pages.HomePage']
parent_page_types = ['pages.HomePage', 'pages.RootPage']
max_count = 1


Expand Down Expand Up @@ -1267,7 +1266,7 @@ class Give(Page):

template = 'page.html'

parent_page_types = ['pages.HomePage']
parent_page_types = ['pages.HomePage', 'pages.RootPage']
max_count = 1


Expand Down Expand Up @@ -1307,7 +1306,7 @@ class TermsOfService(Page):

template = 'page.html'

parent_page_types = ['pages.HomePage']
parent_page_types = ['pages.HomePage', 'pages.RootPage']
max_count = 1


Expand Down Expand Up @@ -1351,7 +1350,7 @@ class FAQ(Page):

template = 'page.html'

parent_page_types = ['pages.HomePage']
parent_page_types = ['pages.HomePage', 'pages.RootPage']


class GiveForm(Page):
Expand Down Expand Up @@ -1387,7 +1386,7 @@ class GiveForm(Page):

template = 'page.html'

parent_page_types = ['pages.HomePage']
parent_page_types = ['pages.HomePage', 'pages.RootPage']
max_count = 1


Expand Down Expand Up @@ -1427,7 +1426,7 @@ class Accessibility(Page):

template = 'page.html'

parent_page_types = ['pages.HomePage']
parent_page_types = ['pages.HomePage', 'pages.RootPage']
max_count = 1


Expand Down Expand Up @@ -1467,7 +1466,7 @@ class Licensing(Page):

template = 'page.html'

parent_page_types = ['pages.HomePage']
parent_page_types = ['pages.HomePage', 'pages.RootPage']
max_count = 1


Expand Down Expand Up @@ -1547,7 +1546,7 @@ class Technology(Page):
]

template = 'page.html'
parent_page_types = ['pages.HomePage']
parent_page_types = ['pages.HomePage', 'pages.RootPage']
max_count = 1


Expand Down Expand Up @@ -1599,7 +1598,7 @@ class ErrataList(Page):
]

template = 'page.html'
parent_page_types = ['pages.HomePage']
parent_page_types = ['pages.HomePage', 'pages.RootPage']
max_count = 1

def get_sitemap_urls(self, request=None):
Expand Down Expand Up @@ -1642,7 +1641,7 @@ class PrivacyPolicy(Page):

template = 'page.html'

parent_page_types = ['pages.HomePage']
parent_page_types = ['pages.HomePage', 'pages.RootPage']
max_count = 1


Expand Down Expand Up @@ -1699,7 +1698,7 @@ class PrintOrder(Page):

template = 'page.html'

parent_page_types = ['pages.HomePage']
parent_page_types = ['pages.HomePage', 'pages.RootPage']
max_count = 1


Expand Down Expand Up @@ -1840,7 +1839,7 @@ class LearningResearchPage(Page):
]

template = 'page.html'
parent_page_types = ['pages.HomePage']
parent_page_types = ['pages.HomePage', 'pages.RootPage']
max_count = 1


Expand Down Expand Up @@ -1879,7 +1878,7 @@ class Careers(Page):
]

template = 'page.html'
parent_page_types = ['pages.HomePage']
parent_page_types = ['pages.HomePage', 'pages.RootPage']
max_count = 1


Expand Down Expand Up @@ -2750,7 +2749,7 @@ def webinars(self):

template = 'page.html'

parent_page_types = ['pages.HomePage']
parent_page_types = ['pages.HomePage', 'pages.RootPage']
max_count = 1


Expand Down Expand Up @@ -2851,7 +2850,7 @@ def get_url_parts(self, *args, **kwargs):

template = 'page.html'

parent_page_types = ['pages.HomePage']
parent_page_types = ['pages.HomePage', 'pages.RootPage']
subpage_types = ['pages.Subject']
max_count = 1

Expand Down Expand Up @@ -3091,7 +3090,7 @@ class FormHeadings(Page):
]

template = 'page.html'
parent_page_types = ['pages.HomePage']
parent_page_types = ['pages.HomePage', 'pages.RootPage']
max_count = 1


Expand Down Expand Up @@ -3371,7 +3370,7 @@ class AllyLogos(Page):
]

template = 'page.html'
parent_page_types = ['pages.HomePage']
parent_page_types = ['pages.HomePage', 'pages.RootPage']


class Assignable(Page):
Expand Down