Skip to content

Commit

Permalink
Merge pull request #661 from praekeltfoundation/baseline-multiple-fix
Browse files Browse the repository at this point in the history
Baseline multiple runs fix
  • Loading branch information
erikh360 authored Sep 6, 2023
2 parents 61ba2a3 + 05f11e2 commit 0f84c81
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 59 deletions.
4 changes: 2 additions & 2 deletions yal/askaquestion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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")),
Expand Down
5 changes: 0 additions & 5 deletions yal/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@
"remind me tomorrow",
"i m not interested",
}
SURVEY_KEYWORDS = {"baseline"}
CALLBACK_CHECK_KEYWORDS = {"callback"}
FEEDBACK_KEYWORDS = {"feedback"}
QA_RESET_FEEDBACK_TIMESTAMP_KEYWORDS = {"resetfeedbacktimestampobzvmp"}
Expand Down Expand Up @@ -217,10 +216,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
Expand Down
2 changes: 1 addition & 1 deletion yal/mainmenu.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions yal/pushmessages_optin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"):
Expand Down
45 changes: 8 additions & 37 deletions yal/rapidpro.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,25 +157,27 @@ async def get_group_membership_count(group_name):
return False, count


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:
Expand All @@ -184,35 +186,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
11 changes: 0 additions & 11 deletions yal/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -847,17 +847,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
Expand Down
16 changes: 15 additions & 1 deletion yal/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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")

0 comments on commit 0f84c81

Please sign in to comment.