Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add text only error to FreeText state #676

Merged
merged 2 commits into from
Aug 22, 2023
Merged
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
17 changes: 17 additions & 0 deletions vaccine/states.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,23 @@ async def display(self, message: Message):
return self.app.send_message(text, helper_metadata=helper_metadata)


class WhatsAppTextOnlyFreetext(FreeText):
def __init__(
self,
*args,
text_only_error: Optional[str] = None,
**kwargs,
):
super().__init__(*args, **kwargs)
self.text_only_error = text_only_error

async def process_message(self, message: Message):
if self.text_only_error is not None and message.transport_metadata:
if message.transport_metadata.get("message", {}).get("type") != "text":
return self.app.send_message(self.text_only_error)
return await super().process_message(message)


class BaseWhatsAppChoiceState(ChoiceState):
"""
Base class for all whatsapp choice states.
Expand Down
5 changes: 3 additions & 2 deletions yal/surveys/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
from vaccine.states import (
Choice,
EndState,
FreeText,
WhatsAppButtonState,
WhatsAppListState,
WhatsAppTextOnlyFreetext,
)
from vaccine.validators import nonempty_validator
from yal import config, contentrepo, rapidpro
Expand Down Expand Up @@ -258,11 +258,12 @@ async def state_location_name_city(self):
]
)
)
return FreeText(
return WhatsAppTextOnlyFreetext(
self,
question=question,
next="state_location_area_type",
check=nonempty_validator(question),
text_only_error=question,
)

async def state_location_area_type(self):
Expand Down
26 changes: 26 additions & 0 deletions yal/tests/surveys/test_location.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,32 @@ async def test_state_location_name_city(tester: AppTester):
)


@pytest.mark.asyncio
async def test_state_location_name_city_invalid(tester: AppTester):
tester.setup_state("state_location_name_city")
await tester.user_input(
"",
transport_metadata={
"message": {
"type": "image",
"image": {"id": "img1", "mime_type": "image/jpeg"},
}
},
)

tester.assert_state("state_location_name_city")
tester.assert_message(
"\n".join(
[
"*What is the name of the city or town you live in or live closest "
"to?*",
"",
"Please *TYPE* in the name of the city or town.",
]
)
)


@pytest.mark.asyncio
async def test_state_location_area_type(tester: AppTester):
tester.setup_state("state_location_name_city")
Expand Down
Loading