Skip to content

Commit

Permalink
Merge pull request #392 from praekeltfoundation/bugfix/DELTA-1419/imp…
Browse files Browse the repository at this point in the history
…ort-xlsx-number-field-type

Fix xlsx number field type on import
  • Loading branch information
rudigiesler authored Nov 28, 2024
2 parents 9e8ddd3 + ae5fc8b commit 287dd59
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

<!--
## Unreleased
### Fixed
- ContentPage import: Accept xlsx where field formatting is numeric.
-->

## v1.3.0
Expand Down
11 changes: 8 additions & 3 deletions home/import_content_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand Down Expand Up @@ -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", "")),
Expand Down Expand Up @@ -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))
Binary file not shown.
11 changes: 11 additions & 0 deletions home/tests/test_content_import_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 287dd59

Please sign in to comment.