Skip to content

Commit

Permalink
Adding list message items to whatsapp
Browse files Browse the repository at this point in the history
  • Loading branch information
Buhle79 committed Jan 30, 2024
1 parent 9d9cb68 commit 3c1ad4b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
12 changes: 12 additions & 0 deletions home/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,11 @@ class WhatsappBlock(blocks.StructBlock):
required=False,
max_num=3,
)
list_items = blocks.ListBlock(blocks.CharBlock(label="Title"),
default=[],
help_text="List item title, up to 24 characters.",
required=False, max_num=10,
validators=(MaxLengthValidator(24)))

class Meta:
icon = "user"
Expand Down Expand Up @@ -299,6 +304,13 @@ def clean(self, value):
f"{len(result['message'])} characters long"
)

list_items = result["list_items"]
for item in list_items:
if len(item) > 24:
errors["list_items"] = ValidationError(
"List item title maximum charactor is 24 "
)

if errors:
raise StructBlockValidationError(errors)
return result
Expand Down
20 changes: 20 additions & 0 deletions home/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ def create_message_value(
example_values=None,
next_prompt="",
buttons=None,
list_items=None,
):
return {
"image": image,
Expand All @@ -344,6 +345,7 @@ def create_message_value(
"variation_messages": variation_messages,
"next_prompt": next_prompt,
"buttons": buttons or [],
"list_items": list_items or [],
}

def create_image(self, width=0, height=0):
Expand Down Expand Up @@ -401,6 +403,24 @@ def test_buttons_char_limit(self):
GoToPageButton().clean({"title": "a" * 21})
self.assertEqual(list(e.exception.block_errors.keys()), ["title"])

def test_items_limit(self):
"""WhatsApp messages can only have up to 10 list items"""
list_item = WhatsappBlock().child_blocks["list_items"]
items = list_item.to_python(
[{"type": "item", "value": "test"} for _ in range(1)]
)
print("##############", items)
WhatsappBlock().clean(self.create_message_value(message="a", list_items=items))

with self.assertRaises(StructBlockValidationError) as e:
items = list_item.to_python(
[{"type": "next_message", "value": "test"} for _ in range(11)]
)
WhatsappBlock().clean(
self.create_message_value(message="a", list_items=items)
)
self.assertEqual(list(e.exception.block_errors.keys()), ["list_items"])


class USSDBlockTests(TestCase):
def create_message_value(
Expand Down
1 change: 1 addition & 0 deletions home/tests/test_page_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ def test_build_simple_pages() -> None:
"buttons": [],
"example_values": [],
"variation_messages": [],
"list_items": [],
},
),
]
Expand Down

0 comments on commit 3c1ad4b

Please sign in to comment.