diff --git a/CHANGELOG.md b/CHANGELOG.md index 8366bf95..6130bed7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## v1.3.0 diff --git a/home/import_content_pages.py b/home/import_content_pages.py index afcab066..44faf376 100644 --- a/home/import_content_pages.py +++ b/home/import_content_pages.py @@ -697,7 +697,6 @@ def wagtail_format(self) -> dict[str, str]: @dataclass(slots=True, frozen=True) class ContentRow: slug: str - page_id: int | None = None parent: str = "" web_title: str = "" web_subtitle: str = "" @@ -763,7 +762,6 @@ def from_flat(cls, row: dict[str, str], row_num: int) -> "ContentRow": for item in deserialise_list(row_list_items) ] return cls( - page_id=to_int_or_none(row.pop("page_id", None)), variation_title=deserialise_dict(row.pop("variation_title", "")), tags=deserialise_list(row.pop("tags", "")), quick_replies=deserialise_list(row.pop("quick_replies", "")), @@ -859,4 +857,11 @@ def JSON_loader(row_num: int, value: str) -> list[dict[str, Any]]: def to_int_or_none(val: str | None) -> int | None: - return int(val) if val else None + if val is None: + return None + try: + return int(val) + except ValueError: + # If it's an excel document with number formatting, we get a float + # If it's not a valid number, then we let the exception bubble up + return int(float(val)) diff --git a/home/tests/import-export-data/contentpage_number_type.xlsx b/home/tests/import-export-data/contentpage_number_type.xlsx new file mode 100644 index 00000000..0a29c61b Binary files /dev/null and b/home/tests/import-export-data/contentpage_number_type.xlsx differ diff --git a/home/tests/test_content_import_export.py b/home/tests/test_content_import_export.py index 82e69033..7d94e571 100644 --- a/home/tests/test_content_import_export.py +++ b/home/tests/test_content_import_export.py @@ -1412,6 +1412,17 @@ def test_import_pages_xlsx(self, xlsx_impexp: ImportExport) -> None: content_pages = ContentPage.objects.all() assert len(content_pages) > 0 + def test_import_pages_number_type(self, xlsx_impexp: ImportExport) -> None: + """ + Importing an XLSX file where number fields have a number cell formatting + shouldn't break + """ + home_page = HomePage.objects.first() + PageBuilder.build_cpi(home_page, "main-menu", "main menu first time user") + xlsx_impexp.import_file("contentpage_number_type.xlsx", purge=False) + content_pages = ContentPage.objects.all() + assert len(content_pages) > 0 + def test_invalid_page(self, csv_impexp: ImportExport) -> None: """ Import an invalid page that matches a valid page already in the db