Skip to content

Commit

Permalink
Merged main back in
Browse files Browse the repository at this point in the history
  • Loading branch information
fritzbrand committed Jan 23, 2024
2 parents 334bd15 + c4b71a5 commit 7ede68c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
17 changes: 13 additions & 4 deletions home/import_content_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from typing import Any
from uuid import uuid4

from django.core.exceptions import ValidationError # type: ignore
from openpyxl import load_workbook
from taggit.models import Tag # type: ignore
from treebeard.exceptions import NodeAlreadySaved # type: ignore
Expand Down Expand Up @@ -231,8 +232,12 @@ def create_content_page_index_from_row(self, row: "ContentRow") -> None:
if row.translation_tag or locale != self.default_locale():
index.translation_key = row.translation_tag
locale = self.locale_from_display_name(row.locale)
with contextlib.suppress(NodeAlreadySaved):
self.home_page(locale).add_child(instance=index)
try:
with contextlib.suppress(NodeAlreadySaved):
self.home_page(locale).add_child(instance=index)
except ValidationError as err:
# FIXME: Find a better way to represent this.
raise ImportException(f"Validation error: {err}")

index.save_revision().publish()

Expand Down Expand Up @@ -394,8 +399,12 @@ def save(self, parent: Page) -> None:
self.add_quick_replies_to_page(page)
self.add_triggers_to_page(page)

with contextlib.suppress(NodeAlreadySaved):
parent.add_child(instance=page)
try:
with contextlib.suppress(NodeAlreadySaved):
parent.add_child(instance=page)
except ValidationError as err:
# FIXME: Find a better way to represent this.
raise ImportException(f"Validation error: {err}", self.row_num)

page.save_revision().publish()

Expand Down
3 changes: 3 additions & 0 deletions home/tests/bad-whatsapp-template-category.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
structure,message,page_id,slug,parent,web_title,web_subtitle,web_body,whatsapp_title,whatsapp_body,whatsapp_template_name,whatsapp_template_category,example_values,variation_title,variation_body,sms_title,sms_body,ussd_title,ussd_body,messenger_title,messenger_body,viber_title,viber_body,translation_tag,tags,quick_replies,triggers,locale,next_prompt,buttons,image_link,doc_link,media_link,related_pages
Menu 1,0,659,ma_import-export,,MA_import export,,,,,,,,,,,,,,,,,,,,,,English,,,,,,
Sub 1.1,1,660,locale-import,MA_import export,Locale import,,,import per locale,this is the english message..edit,template,Marketing,,,,,,,,,,,,,,,,English,,[],,,,
22 changes: 12 additions & 10 deletions home/tests/test_content_import_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import pytest
from django.core import serializers # type: ignore
from django.core.exceptions import ValidationError # type: ignore
from django.core.files.images import ImageFile # type: ignore
from openpyxl import load_workbook
from pytest_django.fixtures import SettingsWrapper
Expand Down Expand Up @@ -737,13 +736,23 @@ def test_no_translation_key_nondefault(self, csv_impexp: ImportExport) -> None:
HomePage.add_root(locale=pt, title="Home (pt)", slug="home-pt")

# A ContentPageIndex without a translation key fails
with pytest.raises(ValidationError):
with pytest.raises(ImportException) as e:
csv_impexp.import_file("no-translation-key-cpi.csv")

assert e.value.row_num == 4
# FIXME: Find a better way to represent this.
assert "translation_key" in e.value.message
assert "“” is not a valid UUID." in e.value.message

# A ContentPage without a translation key fails
with pytest.raises(ValidationError):
with pytest.raises(ImportException) as e:
csv_impexp.import_file("no-translation-key-cp.csv")

assert e.value.row_num == 5
# FIXME: Find a better way to represent this.
assert "translation_key" in e.value.message
assert "“” is not a valid UUID." in e.value.message

def test_invalid_locale_name(self, csv_impexp: ImportExport) -> None:
"""
Importing pages with invalid locale names should raise an error that results
Expand Down Expand Up @@ -1055,9 +1064,6 @@ def test_tags_and_related(self, impexp: ImportExport) -> None:
"""
ContentPages with tags and related pages are preserved across
export/import.
NOTE: The old importer can't handle non-ContentPage related pages, so
it doesn't get one of those.
"""
home_page = HomePage.objects.first()
main_menu = PageBuilder.build_cpi(home_page, "main-menu", "Main Menu")
Expand Down Expand Up @@ -1312,8 +1318,6 @@ def test_translations_split(self, impexp: ImportExport) -> None:
ContentPages in multiple languages (with unique-per-locale slugs and
titles) are preserved across export/import with each language imported
separately.
NOTE: Old importer can't handle non-unique slugs.
"""
# Create a new homepage for Portuguese.
pt, _created = Locale.objects.get_or_create(language_code="pt")
Expand Down Expand Up @@ -1459,8 +1463,6 @@ def test_example_values(self, impexp: ImportExport) -> None:
"""
ContentPages with example values in whatsapp messages are preserved
across export/import.
NOTE: Old importer can't handle example values.
"""
home_page = HomePage.objects.first()
main_menu = PageBuilder.build_cpi(home_page, "main-menu", "Main Menu")
Expand Down

0 comments on commit 7ede68c

Please sign in to comment.