From 966fae63cea711dd2902525689490a710ad6ffed Mon Sep 17 00:00:00 2001 From: Erik Harding Date: Tue, 22 Aug 2023 12:06:02 +0200 Subject: [PATCH 1/2] Add text only error to FreeText state --- vaccine/states.py | 5 +++++ yal/surveys/location.py | 1 + yal/tests/surveys/test_location.py | 26 ++++++++++++++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/vaccine/states.py b/vaccine/states.py index 1c6f1229..f0f2cc85 100644 --- a/vaccine/states.py +++ b/vaccine/states.py @@ -216,6 +216,7 @@ def __init__( override_answer_name: Optional[str] = None, footer: Optional[str] = None, header: Optional[str] = None, + text_only_error: Optional[str] = None, ): self.app = app self.question = question @@ -225,8 +226,12 @@ def __init__( self.override_answer_name = override_answer_name self.footer = footer self.header = header + 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) if self.check is not None: if not isinstance(self.check, list): self.check = [self.check] diff --git a/yal/surveys/location.py b/yal/surveys/location.py index 69fec906..a5e4333f 100644 --- a/yal/surveys/location.py +++ b/yal/surveys/location.py @@ -263,6 +263,7 @@ async def state_location_name_city(self): question=question, next="state_location_area_type", check=nonempty_validator(question), + text_only_error=question, ) async def state_location_area_type(self): diff --git a/yal/tests/surveys/test_location.py b/yal/tests/surveys/test_location.py index 949612c4..e776f57c 100644 --- a/yal/tests/surveys/test_location.py +++ b/yal/tests/surveys/test_location.py @@ -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") From fc9497642809e359871a1191f45ffed485374f98 Mon Sep 17 00:00:00 2001 From: Erik Harding Date: Tue, 22 Aug 2023 12:42:59 +0200 Subject: [PATCH 2/2] create WhatsAppTextOnlyFreetext --- vaccine/states.py | 22 +++++++++++++++++----- yal/surveys/location.py | 4 ++-- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/vaccine/states.py b/vaccine/states.py index f0f2cc85..887cb787 100644 --- a/vaccine/states.py +++ b/vaccine/states.py @@ -216,7 +216,6 @@ def __init__( override_answer_name: Optional[str] = None, footer: Optional[str] = None, header: Optional[str] = None, - text_only_error: Optional[str] = None, ): self.app = app self.question = question @@ -226,12 +225,8 @@ def __init__( self.override_answer_name = override_answer_name self.footer = footer self.header = header - 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) if self.check is not None: if not isinstance(self.check, list): self.check = [self.check] @@ -259,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. diff --git a/yal/surveys/location.py b/yal/surveys/location.py index a5e4333f..a06d889f 100644 --- a/yal/surveys/location.py +++ b/yal/surveys/location.py @@ -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 @@ -258,7 +258,7 @@ async def state_location_name_city(self): ] ) ) - return FreeText( + return WhatsAppTextOnlyFreetext( self, question=question, next="state_location_area_type",