Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions home/export_content_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class ExportRow:
web_body: str = ""
whatsapp_title: str = ""
whatsapp_body: str = ""
whatsapp_message_title: str = ""
whatsapp_template_slug: str = ""
variation_title: str = ""
variation_body: str = ""
Expand Down Expand Up @@ -126,6 +127,8 @@ def add_message_fields(self, msg_blocks: MsgBlocks) -> None:
self.whatsapp_template_slug = whatsapp.value.slug
else:
self.whatsapp_body = whatsapp.value["message"].strip()
# Per-message WhatsApp title (not the page-level whatsapp_title)
self.whatsapp_message_title = whatsapp.value.get("title", "")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since title should always exist, I think we want value["title"] here.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we've done a django migration, but not a streamfield migration, that field might not exist on messages that haven't been updated. I don't think adding a field requires us to add a streamfield migration, although we might have to deal with the field being missing on old messages: https://docs.wagtail.org/en/v4.2/advanced_topics/streamfield_migrations.html#streamfield-data-migrations

if "image" in whatsapp.value and whatsapp.value["image"] is not None:
self.image_link = whatsapp.value["image"].file.url
if (
Expand Down Expand Up @@ -344,6 +347,7 @@ def _set_xlsx_styles(wb: Workbook, sheet: Worksheet) -> None:
"web_body": 370,
"whatsapp_title": 118,
"whatsapp_body": 370,
"whatsapp_message_title": 118,
"whatsapp_template_slug": 118,
"variation_title": 118,
"variation_body": 370,
Expand Down
4 changes: 4 additions & 0 deletions home/import_content_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ def add_message_to_shadow_content_page_from_row(
page.whatsapp_body.append(
ShadowWhatsappBlock(
message=row.whatsapp_body,
title=row.whatsapp_message_title,
next_prompt=row.next_prompt,
example_values=row.example_values,
buttons=buttons,
Expand Down Expand Up @@ -685,6 +686,7 @@ def formatted_viber_body(self) -> list[StructValue]:
@dataclass(slots=True)
class ShadowWhatsappBlock:
message: str = ""
title: str = ""
next_prompt: str = ""
buttons: list[dict[str, Any]] = field(default_factory=list)
example_values: list[str] = field(default_factory=list)
Expand All @@ -699,6 +701,7 @@ def wagtail_format(
) -> dict[str, str | list[dict[str, str | list[dict[str, str]]]] | list[str]]:
return {
"message": self.message,
"title": self.title,
"next_prompt": self.next_prompt,
"example_values": self.example_values,
"buttons": self.buttons,
Expand Down Expand Up @@ -782,6 +785,7 @@ class ContentRow:
web_body: str = ""
whatsapp_title: str = ""
whatsapp_body: str = ""
whatsapp_message_title: str = ""
whatsapp_template_slug: str = ""
example_values: list[str] = field(default_factory=list)
variation_title: dict[str, str] = field(default_factory=dict)
Expand Down
319 changes: 319 additions & 0 deletions home/migrations/0105_alter_contentpage_whatsapp_body.py

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions home/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,12 @@ class WhatsappBlock(blocks.StructBlock):
image = ImageChooserBlock(required=False)
document = DocumentChooserBlock(icon="document", required=False)
media = MediaBlock(icon="media", required=False)
title = blocks.CharBlock(
required=False,
help_text="Short heading shown above the message body, up to 60 characters.",
max_length=60,
default="",
)
message = blocks.TextBlock(
help_text="each text message cannot exceed 4096 characters, messages with "
"media cannot exceed 1024 characters.",
Expand Down Expand Up @@ -1625,6 +1631,12 @@ def __str__(self) -> str:
"""String repr of this snippet."""
return self.slug

# Provide a minimal snippet_viewset interface for chooser widgets when the
# snippet isn't registered in the admin (feature-flagged). Wagtail's
# SnippetChooserBlock accesses `target_model.snippet_viewset.icon`.
class snippet_viewset:
icon = "order"

Comment on lines +1634 to +1639

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this related to the WA title?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no idea why it added this

def clean(self) -> None:
result = super().clean()
errors: dict[str, list[ValidationError]] = {}
Expand Down
3 changes: 3 additions & 0 deletions home/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ def format_whatsapp_template_message(message_index, content_page) -> dict[str, A
# to retain the v2 api behaviour. We also return some fields as null or empty values, for the same reason
"type": "Whatsapp_Message",
"value": {
"title": "",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to add this to the v2 API, especially just a hardcoded empty string, since we can keep the v2 API the same and just add this to the v3 API.

"image": wa_template.image.id if wa_template.image else "",
"media": None,
"footer": "",
Expand All @@ -145,6 +146,8 @@ def format_whatsapp_message(message_index, content_page, platform):
text = content_page.whatsapp_body._raw_data[message_index]

if str(text["type"]) == "Whatsapp_Message":
# Ensure the new title field always exists in API output
text["value"].setdefault("title", "")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, we don't need this in the v2 API. What we do need, however, is for this to be present in the new v3 API, in serializers_v3.py

# Flattens the variation_messages field in the whatsapp message
variation_messages = text["value"].get("variation_messages", [])
new_var_messages = []
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 @@ -169,6 +169,7 @@ class WABlk(ContentBlock):
BLOCK_TYPE = WhatsappBlock

# TODO: More body things.
title: str = ""
next_prompt: str | None = None
variation_messages: list[VarMsg] = field(default_factory=list)
buttons: list[Btn] = field(default_factory=list)
Expand Down
2 changes: 2 additions & 0 deletions home/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,7 @@ def test_detail_view_whatsapp_message(self, uclient: Any) -> None:
assert body["total_messages"] == 1
assert body["text"]["type"] == "Whatsapp_Message"
assert body["text"]["value"]["message"] == "*Default whatsapp Content 1* 🏥"
assert body["text"]["value"].get("title", "") == ""

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we expect this field to always be in the response, we should index it directly like we do with the other fields.


@pytest.mark.parametrize("platform", ALL_PLATFORMS)
def test_detail_view_platform_enabled_no_message(self, uclient, platform):
Expand Down Expand Up @@ -1221,6 +1222,7 @@ def test_empty_whatsapp(self, uclient: Any) -> None:
whatsapp_value = content["body"]["text"]["value"]

assert whatsapp_value == {
"title": "",
"image": None,
"media": None,
"footer": "",
Expand Down
1 change: 1 addition & 0 deletions home/tests/test_content_import_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def add_new_fields(entry: ExpDict) -> ExpDict:
return {
"whatsapp_template_slug": "",
**entry,
"whatsapp_message_title": entry.get("whatsapp_message_title") or "",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"whatsapp_message_title": entry.get("whatsapp_message_title") or "",
"whatsapp_message_title": entry.get("whatsapp_message_title", ""),

"whatsapp_template_category": entry.get("whatsapp_template_category")
or "UTILITY",
"example_values": entry.get("example_values") or "[]",
Expand Down
2 changes: 2 additions & 0 deletions home/tests/test_page_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def test_build_simple_pages() -> None:
(
"Whatsapp_Message",
{
"title": "",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need a test with a non-empty title?

"document": None,
"image": None,
"list_title": "",
Expand Down Expand Up @@ -289,6 +290,7 @@ def test_build_variations() -> None:
(
"Whatsapp_Message",
{
"title": "",
"document": None,
"image": None,
"list_title": "",
Expand Down
2 changes: 2 additions & 0 deletions home/tests/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ def test_happy_case_single_variation_message(self) -> None:
expected_result = {
"type": "Whatsapp_Message",
"value": {
"title": "",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need a test with a non-empty title?

"image": None,
"document": None,
"media": None,
Expand Down Expand Up @@ -499,6 +500,7 @@ def test_happy_case_no_variation_messages(self) -> None:
expected_result = {
"type": "Whatsapp_Message",
"value": {
"title": "",
"image": None,
"document": None,
"media": None,
Expand Down
Loading