Skip to content

Commit

Permalink
Merge branch 'fix-missing-related-page-export'
Browse files Browse the repository at this point in the history
  • Loading branch information
jerith committed Dec 7, 2023
2 parents b706302 + 1b1bee2 commit 11f11de
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
7 changes: 5 additions & 2 deletions home/export_content_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,10 @@ def _parent_title(page: Page) -> str:

@staticmethod
def _related_pages(page: Page) -> list[str]:
return [rp.value.slug for rp in page.related_pages]
# Ideally, all related page links would be removed when the page they
# link to is deleted. We don't currently do that, so for now we just
# make sure that we skip such links during export.
return [rp.value.slug for rp in page.related_pages if rp.value is not None]

@staticmethod
def _comma_sep_qs(unformatted_query: PageQuerySet) -> str:
Expand Down Expand Up @@ -369,4 +372,4 @@ def _set_xlsx_styles(wb: Workbook, sheet: Worksheet) -> None:
cell.font = general_font
alignment = copy.copy(cell.alignment)
alignment.wrapText = True
cell.alignment = alignment
cell.alignment = alignment # type: ignore # Broken typeshed update, maybe?
4 changes: 4 additions & 0 deletions home/tests/page_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,10 @@ def link_related(
page: ContentPage, related_pages: Iterable[Page], publish: bool = True
) -> ContentPage:
for related_page in related_pages:
# If we don't fetch all existing related pages before adding new
# ones, we get an inexplicable TypeError deep in the bowels of
# wagtail/blocks/stream_block.py. Good going, wagtail.
list(page.related_pages)
page.related_pages.append(("related_page", related_page))
rev = page.save_revision()
if publish:
Expand Down
53 changes: 50 additions & 3 deletions home/tests/test_content_import_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -711,9 +711,7 @@ def test_roundtrip_all_locales_split(self, csv_impexp: ImportExportFixture) -> N
(This uses translations.csv and the two language-specific subsets thereof.)
"""
if csv_impexp.importer == "old":
pytest.skip(
"Old importer can't handle translated pages with the same slug."
)
pytest.skip("Old importer can't handle non-unique slugs.")
# Create a new homepage for Portuguese.
pt, _created = Locale.objects.get_or_create(language_code="pt")
HomePage.add_root(locale=pt, title="Home (pt)", slug="home-pt")
Expand Down Expand Up @@ -1412,3 +1410,52 @@ def test_example_values(self, impexp: ImportExportFixture) -> None:
impexp.export_reimport()
imported = impexp.get_page_json()
assert imported == orig

def test_export_missing_related_page(self, impexp: ImportExportFixture) -> None:
"""
If a page has a related page that no longer exists, the missing related
page is skipped during export.
"""
home_page = HomePage.objects.first()
main_menu = PageBuilder.build_cpi(home_page, "main-menu", "Main Menu")
ha_menu = PageBuilder.build_cp(
parent=main_menu,
slug="ha-menu",
title="HealthAlert menu",
bodies=[WABody("HealthAlert menu", [WABlk("*Welcome to HealthAlert*")])],
)
health_info = PageBuilder.build_cp(
parent=ha_menu,
slug="health-info",
title="health info",
bodies=[WABody("health info", [WABlk("*Health information*")])],
)
self_help = PageBuilder.build_cp(
parent=ha_menu,
slug="self-help",
title="self-help",
bodies=[WABody("self-help", [WABlk("*Self-help programs*")])],
)
health_info = PageBuilder.link_related(health_info, [self_help])
# This is what we expect to see after export/import, so we fetch the
# JSON to compare with before adding the missing related page.
orig_without_self_help = impexp.get_page_json()

move_along = PageBuilder.build_cp(
parent=ha_menu,
slug="move-along",
title="move-along",
bodies=[WABody("move along", [WABlk("*Nothing to see here*")])],
)
PageBuilder.link_related(health_info, [move_along])
move_along.delete()

# Ideally, all related page links would be removed when the page they
# link to is deleted. We don't currently do that, so for now we just
# make sure that we skip such links during export.
sh_page = Page.objects.get(pk=self_help.id)
assert [rp.value for rp in health_info.related_pages] == [sh_page, None]

impexp.export_reimport()
imported = impexp.get_page_json()
assert imported == orig_without_self_help

0 comments on commit 11f11de

Please sign in to comment.