def create(cls, person_case, episode_case): person_case_properties = person_case.dynamic_case_properties() episode_case_properties = episode_case.dynamic_case_properties() person_locations = get_person_locations(person_case) return cls( beneficiary_id=person_case.case_id, first_name=person_case_properties.get(PERSON_FIRST_NAME, None), last_name=person_case_properties.get(PERSON_LAST_NAME, None), state_code=person_locations.sto, district_code=person_locations.dto, tu_code=person_locations.tu, phi_code=person_locations.phi, phone_numbers=_get_phone_numbers(person_case_properties), merm_id=person_case_properties.get(MERM_ID, None), treatment_start_date=episode_case_properties.get( TREATMENT_START_DATE, None), treatment_supporter_name=u"{} {}".format( episode_case_properties.get(TREATMENT_SUPPORTER_FIRST_NAME, ''), episode_case_properties.get(TREATMENT_SUPPORTER_LAST_NAME, ''), ), treatment_supporter_phone_number=(_format_number( _parse_number( episode_case_properties.get(TREATMENT_SUPPORTER_PHONE)))), weight_band=episode_case_properties.get(WEIGHT_BAND), address=person_case_properties.get(CURRENT_ADDRESS), )
def _get_person_case_properties(episode_case, person_case, person_case_properties): """ :return: Example {'dcode': u'JLR', 'paddress': u'123, near asdf, Jalore, Rajasthan ', 'cmob': u'1234567890', 'pname': u'home visit', 'scode': u'RJ', 'tcode': 'AB', dotphi': u'Test S1-C1-D1-T1 PHI 1', 'pmob': u'1234567890', 'cname': u'123', 'caddress': u'123', 'pgender': 'T', 'page': u'79', 'pcategory': 1} """ person_category = '2' if person_case_properties.get('previous_tb_treatment', '') == 'yes' else '1' person_properties = { "pname": person_case.name, "pgender": gender_mapping.get(person_case_properties.get('sex', ''), ''), # 2B is currently setting age_entered but we are in the short term moving it to use age instead "page": _get_person_age(person_case_properties), "paddress": person_case_properties.get('current_address', ''), "pmob": person_case_properties.get(PRIMARY_PHONE_NUMBER, ''), "cname": person_case_properties.get('secondary_contact_name_address', ''), "caddress": person_case_properties.get('secondary_contact_name_address', ''), "cmob": person_case_properties.get(BACKUP_PHONE_NUMBER, ''), "pcategory": person_category } person_locations = get_person_locations(person_case, episode_case) person_properties.update( { 'scode': person_locations.sto, 'dcode': person_locations.dto, 'tcode': person_locations.tu, 'dotphi': person_locations.phi, } ) return person_properties
def create(cls, person_case, episode_case): person_case_properties = person_case.dynamic_case_properties() episode_case_properties = episode_case.dynamic_case_properties() all_properties = episode_case_properties.copy() all_properties.update(person_case_properties) # items set on person trump items set on episode person_locations = get_person_locations(person_case, episode_case) try: locations = dict( state_code=person_locations.sto, district_code=person_locations.dto, tu_code=person_locations.tu, phi_code=person_locations.phi, ) except AttributeError: locations = dict( state_code=person_locations.sto, district_code=person_locations.dto, tu_code=person_locations.tu, he_code=person_locations.pcp, ) refill_reminder_date = episode_case_properties.get(MERM_REFILL_REMINDER_DATE, None) refill_reminder_time = episode_case_properties.get(MERM_REFILL_REMINDER_TIME, None) if refill_reminder_time and refill_reminder_date: refill_reminder_datetime = "{}T{}".format(refill_reminder_date, refill_reminder_time) else: refill_reminder_datetime = None merm_params = MermParams( IMEI=episode_case_properties.get(MERM_ID, None), daily_reminder_status=episode_case_properties.get(MERM_DAILY_REMINDER_STATUS, None), daily_reminder_time=episode_case_properties.get(MERM_DAILY_REMINDER_TIME, None), refill_reminder_status=episode_case_properties.get(MERM_REFILL_REMINDER_STATUS, None), refill_reminder_datetime=refill_reminder_datetime, RT_hours=episode_case_properties.get(MERM_RT_HOURS, None), ) return cls( beneficiary_id=person_case.case_id, enikshay_id=person_case_properties.get(ENIKSHAY_ID, None), first_name=person_case_properties.get(PERSON_FIRST_NAME, None), last_name=person_case_properties.get(PERSON_LAST_NAME, None), phone_numbers=_get_phone_numbers(all_properties), merm_params=merm_params if episode_case_properties.get(MERM_ID, '') != '' else None, treatment_start_date=episode_case_properties.get(TREATMENT_START_DATE, None), treatment_supporter_name=u"{} {}".format( episode_case_properties.get(TREATMENT_SUPPORTER_FIRST_NAME, ''), episode_case_properties.get(TREATMENT_SUPPORTER_LAST_NAME, ''), ), treatment_supporter_phone_number=( _format_number( _parse_number(episode_case_properties.get(TREATMENT_SUPPORTER_PHONE)) ) ), weight_band=episode_case_properties.get(WEIGHT_BAND), address=person_case_properties.get(CURRENT_ADDRESS), sector='private' if person_case_properties.get(ENROLLED_IN_PRIVATE) == 'true' else 'public', **locations )
def _assert_payload_equal(self, casedb, expected_numbers): person_case = casedb[self.person_id] episode_case = casedb[self.episode_id] person_case_properties = person_case.dynamic_case_properties() episode_case_properties = episode_case.dynamic_case_properties() person_locations = get_person_locations(person_case) expected_payload = { "beneficiary_id": self.person_id, "first_name": person_case_properties.get(PERSON_FIRST_NAME, None), "last_name": person_case_properties.get(PERSON_LAST_NAME, None), "state_code": person_locations.sto, "district_code": person_locations.dto, "tu_code": person_locations.tu, "phi_code": person_locations.phi, "phone_numbers": expected_numbers, "merm_id": person_case_properties.get(MERM_ID, None), "treatment_start_date": episode_case_properties.get(TREATMENT_START_DATE, None), "treatment_supporter_name": "{} {}".format( episode_case_properties.get(TREATMENT_SUPPORTER_FIRST_NAME, ''), episode_case_properties.get(TREATMENT_SUPPORTER_LAST_NAME, ''), ), "treatment_supporter_phone_number": "+91{}".format(self.treatment_supporter_phone[1:]), "weight_band": episode_case_properties.get(WEIGHT_BAND), "address": person_case_properties.get(CURRENT_ADDRESS), } actual_payload = json.loads(self._get_actual_payload(casedb)) self.assertDictEqual(expected_payload, actual_payload)
def get_locations(person_case, episode_case): person_locations = get_person_locations(person_case, episode_case) return { "state_code": person_locations.sto, "district_code": person_locations.dto, "tu_code": person_locations.tu, "he_code": person_locations.pcp, }
def test_get_person_locations(self): self.assign_person_to_location(self.phi.location_id) person_case = CaseAccessors(self.domain).get_case(self.person_id) expected_locations = { 'sto': self.sto.metadata['nikshay_code'], 'dto': self.dto.metadata['nikshay_code'], 'tu': self.tu.metadata['nikshay_code'], 'phi': self.phi.metadata['nikshay_code'], } self.assertEqual(expected_locations, get_person_locations(person_case)._asdict())
def test_get_person_locations(self): self.assign_person_to_location(self.phi.location_id) person_case = CaseAccessors(self.domain).get_case(self.person_id) expected_locations = { 'sto': self.sto.metadata['nikshay_code'], 'dto': self.dto.metadata['nikshay_code'], 'tu': self.tu.metadata['nikshay_code'], 'phi': self.phi.metadata['nikshay_code'], } self.assertEqual(expected_locations, get_person_locations(person_case)._asdict()) update_case(self.domain, self.episode_id, { "treatment_initiating_facility_id": person_case.owner_id }) person_case.owner_id = ARCHIVED_CASE_OWNER_ID person_case.save() person_case = CaseAccessors(self.domain).get_case(self.person_id) episode_case = CaseAccessors(self.domain).get_case(self.episode_id) with self.assertRaises(NikshayLocationNotFound): get_person_locations(person_case) self.assertEqual(expected_locations, get_person_locations(person_case, episode_case)._asdict())
def _assert_payload_contains_subset(self, casedb, expected_numbers=False, sector=u'public'): person_case = casedb[self.person_id] episode_case = casedb[self.episode_id] person_case_properties = person_case.dynamic_case_properties() episode_case_properties = episode_case.dynamic_case_properties() person_locations = get_person_locations(person_case) locations = { u"state_code": person_locations.sto, u"district_code": person_locations.dto, u"tu_code": person_locations.tu, } if sector == 'public': locations.update({ u"phi_code": person_locations.phi, }) else: locations.update({ u"he_code": person_locations.pcp, }) expected_numbers = u"+91{}, +91{}, +91{}".format( self.primary_phone_number.replace("0", ""), self.secondary_phone_number.replace("0", ""), self.other_number.replace("0", "") ) if expected_numbers is False else expected_numbers expected_payload = { u"beneficiary_id": self.person_id, u"enikshay_id": person_case_properties.get(ENIKSHAY_ID, None), u"first_name": person_case_properties.get(PERSON_FIRST_NAME, None), u"last_name": person_case_properties.get(PERSON_LAST_NAME, None), u"phone_numbers": expected_numbers, u"treatment_start_date": episode_case_properties.get(TREATMENT_START_DATE, None), u"treatment_supporter_name": u"{} {}".format( episode_case_properties.get(TREATMENT_SUPPORTER_FIRST_NAME, ''), episode_case_properties.get(TREATMENT_SUPPORTER_LAST_NAME, ''), ), u"treatment_supporter_phone_number": u"+91{}".format(self.treatment_supporter_phone[1:]), u"weight_band": episode_case_properties.get(WEIGHT_BAND), u"address": person_case_properties.get(CURRENT_ADDRESS), u"sector": sector, } if episode_case_properties.get(MERM_ID, None) is not None: expected_payload.update({ u"merm_params": { u"IMEI": episode_case_properties.get(MERM_ID, None), } }) expected_payload.update(locations) actual_payload = json.loads(self._get_actual_payload(casedb)) self.assertDictContainsSubset(expected_payload, actual_payload) return actual_payload
def test_nikshay_location_not_found(self): self.assign_person_to_location("-") person_case = CaseAccessors(self.domain).get_case(self.person_id) with self.assertRaises(NikshayLocationNotFound): get_person_locations(person_case)
def _get_person_locations(self, episode_case): person_case = self._get_person_case(episode_case) return get_person_locations(person_case)