Skip to content

Commit

Permalink
Fix crash when exporting data with media or images.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit Vermeulen committed Feb 13, 2024
1 parent 98c4393 commit 8cda1b9
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 9 deletions.
16 changes: 8 additions & 8 deletions home/export_content_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,24 +109,24 @@ def add_message_fields(self, msg_blocks: MsgBlocks) -> None:
# exporter if there's more than one.
if viber:
self.viber_body = viber.value["message"].strip()
if "image" in viber.value:
self.image_link = viber.value["image"]
if "image" in viber.value and viber.value["image"] is not None:
self.image_link = viber.value["image"].file.url
if messenger:
self.messenger_body = messenger.value["message"].strip()
if "image" in messenger.value:
self.image_link = messenger.value["image"]
if "image" in messenger.value and messenger.value["image"] is not None:
self.image_link = messenger.value["image"].file.url
if sms:
self.sms_body = sms.value["message"].strip()
if ussd:
self.ussd_body = ussd.value["message"].strip()
if whatsapp:
self.whatsapp_body = whatsapp.value["message"].strip()
if "image" in whatsapp.value:
self.image_link = whatsapp.value["image"]
if "image" in whatsapp.value and whatsapp.value["image"] is not None:
self.image_link = whatsapp.value["image"].file.url
if "document" in whatsapp.value:
self.doc_link = whatsapp.value["document"]
if "media" in whatsapp.value:
self.media_link = whatsapp.value["media"]
if "media" in whatsapp.value and whatsapp.value["media"] is not None:
self.media_link = whatsapp.value["media"].file.url
if "next_prompt" in whatsapp.value:
self.next_prompt = whatsapp.value["next_prompt"]
if "buttons" in whatsapp.value:
Expand Down
1 change: 1 addition & 0 deletions home/tests/page_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ class WABlk(ContentBlock):
example_values: list[str] = field(default_factory=list)
buttons: list[Btn] = field(default_factory=list)
list_items: list[str] = field(default_factory=list)
media: int | None = None

def to_dict(self) -> dict[str, Any]:
varmsgs = [vm.to_dict() for vm in self.variation_messages]
Expand Down
85 changes: 84 additions & 1 deletion home/tests/test_content_import_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@

import pytest
from django.core import serializers # type: ignore
from django.core.files.base import File
from django.core.files.images import ImageFile # type: ignore
from openpyxl import load_workbook
from pytest_django.fixtures import SettingsWrapper
from wagtail.images.models import Image # type: ignore
from wagtail.models import Locale, Page # type: ignore
from wagtailmedia.models import Media

from home.content_import_export import import_content
from home.import_content_pages import ImportException
Expand Down Expand Up @@ -910,6 +912,82 @@ def test_ContentPage_required_fields(self, csv_impexp: ImportExport) -> None:

assert src == dst

@pytest.mark.django_db
class TestExport:
"""
Test that the export is valid.
NOTE: This is not a Django (or even unittest) TestCase. It's just a
container for related tests.
"""
def test_export_wa_with_image(self, impexp: ImportExport):
img_path = Path("home/tests/test_static") / "test.jpeg"
img_wa = mk_img(img_path, "wa_image")

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("HA menu", [WABlk("Welcome WA", image=img_wa.id)]),
],
)
content = impexp.export_content(locale="en")
assert True

def test_export_viber_with_image(self, impexp: ImportExport):
img_path = Path("home/tests/test_static") / "test.jpeg"
img_v = mk_img(img_path, "v_image")

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=[
VBody("HA menu", [VBlk("Welcome V", image=img_v.id)]),
],
)
content = impexp.export_content(locale="en")
assert True

def test_export_messenger_with_image(self, impexp: ImportExport):
img_path = Path("home/tests/test_static") / "test.jpeg"
img_m = mk_img(img_path, "m_image")

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=[
MBody("HA menu", [MBlk("Welcome M", image=img_m.id)]),
],
)
content = impexp.export_content(locale="en")
assert True

def test_export_wa_with_media(self, impexp: ImportExport):
media_path = Path("home/tests/test_static") / "test.mp4"
media_wa = mk_media(media_path, "wa_media")

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("HA menu", [WABlk("Welcome WA", media=media_wa.id)]),
],
)
content = impexp.export_content(locale="en")
assert True


@pytest.fixture(params=["csv", "xlsx"])
def impexp(request: Any, admin_client: Any) -> ImportExport:
Expand All @@ -926,13 +1004,18 @@ def mk_img(img_path: Path, title: str) -> Image:
img.save()
return img

def mk_media(media_path: Path, title: str) -> File:
media = Media(title=title, file=File(media_path.open("rb"), name=media_path.name))
media.save()
return media


@pytest.mark.usefixtures("tmp_media_path")
@pytest.mark.django_db
class TestExportImportRoundtrip:
"""
Test that the db state after exporting and reimporting content is
equilavent to what it was before.
equivalent to what it was before.
NOTE: This is not a Django (or even unittest) TestCase. It's just a
container for related tests.
Expand Down
Binary file added home/tests/test_static/test.mp4
Binary file not shown.

0 comments on commit 8cda1b9

Please sign in to comment.