From d848a92176bf2bfdedf88d9e88872525a4635207 Mon Sep 17 00:00:00 2001 From: Erik Harding Date: Thu, 10 Aug 2023 14:24:41 +0200 Subject: [PATCH 1/2] remove baseline keyword --- yal/main.py | 5 ----- yal/tests/test_main.py | 11 ----------- 2 files changed, 16 deletions(-) diff --git a/yal/main.py b/yal/main.py index f60fe39a..749847bb 100644 --- a/yal/main.py +++ b/yal/main.py @@ -79,7 +79,6 @@ "remind me tomorrow", "i m not interested", } -SURVEY_KEYWORDS = {"baseline"} CALLBACK_CHECK_KEYWORDS = {"callback"} FEEDBACK_KEYWORDS = {"feedback"} QA_RESET_FEEDBACK_TIMESTAMP_KEYWORDS = {"resetfeedbacktimestampobzvmp"} @@ -210,10 +209,6 @@ async def process_message(self, message): self.user.session_id = None self.state_name = AssessmentApplication.REMINDER_STATE - elif keyword in SURVEY_KEYWORDS: - self.user.session_id = None - self.state_name = "state_baseline_start" - elif ( keyword in EJAF_ENDLINE_SURVEY_KEYWORDS and baseline_survey_completed diff --git a/yal/tests/test_main.py b/yal/tests/test_main.py index f38f9864..fb3c8d5f 100644 --- a/yal/tests/test_main.py +++ b/yal/tests/test_main.py @@ -833,17 +833,6 @@ async def test_state_self_perceived_healthcare_assessment_later( tester.assert_message(message) -@pytest.mark.asyncio -async def test_baseline_survey_start_keywords( - tester: AppTester, rapidpro_mock, contentrepo_api_mock -): - rapidpro_mock.tstate.contact_fields["onboarding_completed"] = True - rapidpro_mock.tstate.contact_fields["terms_accepted"] = True - rapidpro_mock.tstate.contact_fields["assessment_reminder_sent"] = True - await tester.user_input("baseline") - tester.assert_state("state_survey_question") - - @pytest.mark.asyncio async def test_endline_survey_start_keywords( tester: AppTester, rapidpro_mock, contentrepo_api_mock From 00cf8dbd6e813be56e1add20b60eca30d2425e28 Mon Sep 17 00:00:00 2001 From: Erik Harding Date: Thu, 10 Aug 2023 14:42:23 +0200 Subject: [PATCH 2/2] refactor rapidpro global check --- yal/askaquestion.py | 4 ++-- yal/mainmenu.py | 2 +- yal/pushmessages_optin.py | 4 ++-- yal/rapidpro.py | 45 +++++++-------------------------------- yal/utils.py | 16 +++++++++++++- 5 files changed, 28 insertions(+), 43 deletions(-) diff --git a/yal/askaquestion.py b/yal/askaquestion.py index 3eb4688d..1e1b56a2 100644 --- a/yal/askaquestion.py +++ b/yal/askaquestion.py @@ -15,7 +15,7 @@ ) from vaccine.utils import get_display_choices from vaccine.validators import nonempty_validator -from yal import aaq_core, config, rapidpro +from yal import aaq_core, config, rapidpro, utils from yal.pleasecallme import Application as PleaseCallMeApplication from yal.servicefinder import Application as ServiceFinderApplication from yal.utils import ( @@ -471,7 +471,7 @@ async def state_no_question_not_answered_thank_you(self): "counsellor": PleaseCallMeApplication.START_STATE, } - if (await rapidpro.check_if_service_finder_active()).lower() == "true": + if await utils.check_if_service_finder_active(): choices = [ Choice("clinic", self._("Find a clinic")), Choice("counsellor", self._("Talk to a counsellor")), diff --git a/yal/mainmenu.py b/yal/mainmenu.py index f05b1602..c39b3f3d 100644 --- a/yal/mainmenu.py +++ b/yal/mainmenu.py @@ -94,7 +94,7 @@ async def state_mainmenu(self): ) ] - if (await rapidpro.check_if_service_finder_active()).lower() == "true": + if await utils.check_if_service_finder_active(): sections[0][1].insert( 1, Choice( diff --git a/yal/pushmessages_optin.py b/yal/pushmessages_optin.py index da5d5e49..531eb208 100644 --- a/yal/pushmessages_optin.py +++ b/yal/pushmessages_optin.py @@ -4,7 +4,7 @@ from vaccine.base_application import BaseApplication from vaccine.states import Choice, WhatsAppButtonState from vaccine.utils import get_display_choices -from yal import contentrepo, rapidpro +from yal import contentrepo, rapidpro, utils from yal.askaquestion import Application as AaqApplication from yal.mainmenu import Application as MainMenuApplication from yal.surveys.baseline import Application as BaselineSurveyApplication @@ -82,7 +82,7 @@ async def state_pushmessage_optin_yes_submit(self): if error: return await self.go_to_state("state_error") - is_baseline_survey_active = await rapidpro.check_if_baseline_active() == "True" + is_baseline_survey_active = await utils.check_if_baseline_active() is_in_south_africa = self.user.metadata.get("country") == "south africa" is_in_age_range = None if self.user.metadata.get("age"): diff --git a/yal/rapidpro.py b/yal/rapidpro.py index 23bb1eba..1b2ddb06 100644 --- a/yal/rapidpro.py +++ b/yal/rapidpro.py @@ -133,25 +133,27 @@ async def get_instance_fields(): return fields -async def check_if_baseline_active(): +async def get_global_flag(global_name): """ - Checks a Global var on the RapidPro instance to see if the Baseline survey is active + Checks a Global var on the RapidPro instance to see if it is active """ async with get_rapidpro_api() as session: - is_baseline_survey_active = False + is_active = False for i in range(3): try: response = await session.get( urljoin( config.RAPIDPRO_URL, - "/api/v2/globals.json?key=baseline_survey_active", + f"/api/v2/globals.json?key={global_name}", ), ) response.raise_for_status() response_body = await response.json() if len(response_body["results"]) > 0: - is_baseline_survey_active = response_body["results"][0]["value"] + is_active = ( + str(response_body["results"][0]["value"]).lower() == "true" + ) break except HTTP_EXCEPTIONS as e: @@ -160,35 +162,4 @@ async def check_if_baseline_active(): return False else: continue - return is_baseline_survey_active - - -async def check_if_service_finder_active(): - """ - Checks a Global var on the RapidPro instance to see if the Service finder is active - """ - async with get_rapidpro_api() as session: - is_service_finder_active = "False" - for i in range(3): - try: - response = await session.get( - urljoin( - config.RAPIDPRO_URL, - "/api/v2/globals.json?key=service_finder_active", - ), - ) - - response.raise_for_status() - response_body = await response.json() - - if len(response_body["results"]) > 0: - is_service_finder_active = response_body["results"][0]["value"] - response.close() - break - except HTTP_EXCEPTIONS as e: - if i == 2: - logger.exception(e) - return "False" - else: - continue - return is_service_finder_active + return is_active diff --git a/yal/utils.py b/yal/utils.py index b7e7ea4b..562a4184 100644 --- a/yal/utils.py +++ b/yal/utils.py @@ -10,7 +10,7 @@ from emoji import emoji_list from rapidfuzz import fuzz, process -from yal import config +from yal import config, rapidpro TZ_SAST = timezone(timedelta(hours=2), "SAST") PROVINCES = sorted( @@ -152,3 +152,17 @@ def is_integer(string: str) -> bool: def get_generic_error_options(): return random.choice(GENERIC_ERROR_OPTIONS) + + +async def check_if_baseline_active(): + """ + Checks a Global var on the RapidPro instance to see if the Baseline survey is active + """ + return await rapidpro.get_global_flag("baseline_survey_active") + + +async def check_if_service_finder_active(): + """ + Checks a Global var on the RapidPro instance to see if the Service finder is active + """ + return await rapidpro.get_global_flag("service_finder_active")