Skip to content

Commit

Permalink
Merge pull request #521 from praekeltfoundation/feature/YAL-520-accom…
Browse files Browse the repository at this point in the history
…odate-variation-messages

Show variation messages to users with matching profiles
  • Loading branch information
KaitCrawford authored Jan 25, 2023
2 parents 3895033 + 1781c05 commit be55fb6
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 1 deletion.
10 changes: 10 additions & 0 deletions yal/contentrepo.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,16 @@ async def get_page_details(
page_details["title"] = response_body["title"]
page_details["body"] = response_body["body"]["text"]["value"]["message"]

variations: Dict[str, Any] = {}
for v in response_body["body"]["text"]["value"].get(
"variation_messages", []
):
if v["profile_field"] in variations:
variations[v["profile_field"]][v["value"]] = v["message"]
else:
variations[v["profile_field"]] = {v["value"]: v["message"]}
page_details["variations"] = variations

page_details["parent_id"] = response_body["meta"]["parent"]["id"]
page_details["parent_title"] = response_body["meta"]["parent"]["title"]

Expand Down
11 changes: 11 additions & 0 deletions yal/mainmenu.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ async def state_contentrepo_page(self):
self.save_metadata(
"feature_redirects", page_details.get("feature_redirects", [])
)
self.save_metadata("variations", page_details.get("variations", {}))

# If a user loads a content repo page from somewhere other than the main menu
# then we need to infer the menu level and the topics they've seen
Expand Down Expand Up @@ -433,6 +434,16 @@ async def next_(choice: Choice):
next_prompt = metadata.get("next_prompt")
quiz_tag = metadata.get("quiz_tag")
quick_replies = metadata.get("quick_replies", [])
variations = metadata.get("variations", {})

if variations:
for field, value in variations.items():
field_value = metadata.get(field)
if field_value == "non_binary":
field_value = "non-binary"
if field_value in value:
body = value[field_value]
break

parts = []

Expand Down
97 changes: 96 additions & 1 deletion yal/tests/test_mainmenu.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def build_message_detail(
message=1,
next_message=None,
previous_message=None,
variations=[],
):
return {
"id": id,
Expand All @@ -43,7 +44,12 @@ def build_message_detail(
"total_messages": total_messages,
"text": {
"type": "Whatsapp_Message",
"value": {"image": image, "message": content, "next_prompt": ""},
"value": {
"image": image,
"message": content,
"next_prompt": "",
"variation_messages": variations,
},
"id": "111b8f05-be1b-4461-ac85-562930747336",
},
"revision": id,
Expand Down Expand Up @@ -207,6 +213,23 @@ def get_page_detail_1112(request):
"Main Menu 1 💊",
"Message test content 1",
quick_replies=["No, thanks"],
variations=[
{
"profile_field": "gender",
"value": "female",
"message": "Message test content for females",
},
{
"profile_field": "gender",
"value": "male",
"message": "Message test content for males",
},
{
"profile_field": "gender",
"value": "non-binary",
"message": "Message test content for non-binary",
},
],
)
)

Expand Down Expand Up @@ -1482,6 +1505,78 @@ async def test_state_display_page_detail_related(
)


@pytest.mark.asyncio
async def test_state_display_page_detail_variations(
tester: AppTester, contentrepo_api_mock, rapidpro_mock
):
tester.setup_state("state_contentrepo_page")
tester.user.metadata["selected_page_id"] = "1112"
tester.user.metadata["current_message_id"] = 1
tester.user.metadata["current_menu_level"] = 3
tester.user.metadata["last_topic_viewed"] = "1"
tester.user.metadata["parent_title"] = "Test Back"
tester.user.metadata["suggested_content"] = {}
tester.user.metadata["gender"] = "female"

tester.user.session_id = 123
await tester.user_input(session=Message.SESSION_EVENT.NEW)

tester.assert_message(
"\n".join(
[
"Main Menu 1 💊",
"-----",
"",
"Message test content for females",
"",
"1. No, thanks",
"",
"-----",
"*Or reply:*",
"2. ⬅️ Parent Title",
BACK_TO_MAIN,
GET_HELP,
]
)
)


@pytest.mark.asyncio
async def test_state_display_page_detail_non_bin_variations(
tester: AppTester, contentrepo_api_mock, rapidpro_mock
):
tester.setup_state("state_contentrepo_page")
tester.user.metadata["selected_page_id"] = "1112"
tester.user.metadata["current_message_id"] = 1
tester.user.metadata["current_menu_level"] = 3
tester.user.metadata["last_topic_viewed"] = "1"
tester.user.metadata["parent_title"] = "Test Back"
tester.user.metadata["suggested_content"] = {}
tester.user.metadata["gender"] = "non_binary"

tester.user.session_id = 123
await tester.user_input(session=Message.SESSION_EVENT.NEW)

tester.assert_message(
"\n".join(
[
"Main Menu 1 💊",
"-----",
"",
"Message test content for non-binary",
"",
"1. No, thanks",
"",
"-----",
"*Or reply:*",
"2. ⬅️ Parent Title",
BACK_TO_MAIN,
GET_HELP,
]
)
)


@pytest.fixture
async def contentrepo_api_mock2():
Sanic.test_mode = True
Expand Down

0 comments on commit be55fb6

Please sign in to comment.