<commit_before>import uuid
import json
import phonenumbers
import jsonobject
from django.utils.dateparse import parse_date

from corehq.motech.repeaters.repeater_generators import BasePayloadGenerator
from corehq.motech.repeaters.exceptions import RequestConnectionError
from custom.enikshay.case_utils import (
    get_occurrence_case_from_episode,
    get_person_case_from_occurrence,
    get_open_episode_case_from_person,
    update_case,
    get_person_locations,
    get_episode_case_from_adherence,
    CASE_TYPE_PERSON,
    CASE_TYPE_EPISODE,
)
from custom.enikshay.const import (
    NINETYNINEDOTS_NUMBERS,
    ENIKSHAY_ID,
    PERSON_FIRST_NAME,
    PERSON_LAST_NAME,
    TREATMENT_START_DATE,
    TREATMENT_SUPPORTER_FIRST_NAME,
    TREATMENT_SUPPORTER_LAST_NAME,
    TREATMENT_SUPPORTER_PHONE,
    TREATMENT_OUTCOME,
    TREATMENT_OUTCOME_DATE,
    WEIGHT_BAND,
    CURRENT_ADDRESS,
    ENROLLED_IN_PRIVATE,
    MERM_ID,
    MERM_DAILY_REMINDER_STATUS,
    MERM_DAILY_REMINDER_TIME,
    MERM_REFILL_REMINDER_STATUS,
    MERM_REFILL_REMINDER_DATE,
    MERM_REFILL_REMINDER_TIME,
    MERM_RT_HOURS,
)
from custom.enikshay.exceptions import ENikshayCaseNotFound


class MermParams(jsonobject.JsonObject):
    IMEI = jsonobject.StringProperty(required=False, exclude_if_none=True)
    daily_reminder_status = jsonobject.StringProperty(required=False, exclude_if_none=True)
    daily_reminder_time = jsonobject.StringProperty(required=False, exclude_if_none=True)  # HH:mm
    refill_reminder_status = jsonobject.StringProperty(required=False, exclude_if_none=True)
    refill_reminder_datetime = jsonobject.StringProperty(
        required=False,
        exclude_if_none=True
    )  # yy/MM/dd HH:mm:ss
    RT_hours = jsonobject.StringProperty(
        required=False,
        exclude_if_none=True
    )  # 1 = 12 hours; i.e. for 3 days - RT_hours = 6


class PatientPayload(jsonobject.JsonObject):
    beneficiary_id = jsonobject.StringProperty(required=True)
    enikshay_id = jsonobject.StringProperty(required=False)

    first_name = jsonobject.StringProperty(required=False)
    last_name = jsonobject.StringProperty(required=False)

    state_code = jsonobject.StringProperty(required=False)
    district_code = jsonobject.StringProperty(required=False)
    tu_code = jsonobject.StringProperty(required=False)
    phi_code = jsonobject.StringProperty(required=False, exclude_if_none=True)
    he_code = jsonobject.StringProperty(required=False, exclude_if_none=True)

    phone_numbers = jsonobject.StringProperty(required=False)
    merm_params = jsonobject.ObjectProperty(MermParams, required=False, exclude_if_none=True)

    treatment_start_date = jsonobject.StringProperty(required=False)

    treatment_supporter_name = jsonobject.StringProperty(required=False)
    treatment_supporter_phone_number = jsonobject.StringProperty(required=False)

    @classmethod
    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)
        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),<commit_msg>update to use diagnosing_facility_id for fetching locations for 99dots as well<commit_after>import uuid
import json
import phonenumbers
import jsonobject
from django.utils.dateparse import parse_date

from corehq.motech.repeaters.repeater_generators import BasePayloadGenerator
from corehq.motech.repeaters.exceptions import RequestConnectionError
from custom.enikshay.case_utils import (
    get_occurrence_case_from_episode,
    get_person_case_from_occurrence,
    get_open_episode_case_from_person,
    update_case,
    get_person_locations,
    get_episode_case_from_adherence,
    CASE_TYPE_PERSON,
    CASE_TYPE_EPISODE,
)
from custom.enikshay.const import (
    NINETYNINEDOTS_NUMBERS,
    ENIKSHAY_ID,
    PERSON_FIRST_NAME,
    PERSON_LAST_NAME,
    TREATMENT_START_DATE,
    TREATMENT_SUPPORTER_FIRST_NAME,
    TREATMENT_SUPPORTER_LAST_NAME,
    TREATMENT_SUPPORTER_PHONE,
    TREATMENT_OUTCOME,
    TREATMENT_OUTCOME_DATE,
    WEIGHT_BAND,
    CURRENT_ADDRESS,
    ENROLLED_IN_PRIVATE,
    MERM_ID,
    MERM_DAILY_REMINDER_STATUS,
    MERM_DAILY_REMINDER_TIME,
    MERM_REFILL_REMINDER_STATUS,
    MERM_REFILL_REMINDER_DATE,
    MERM_REFILL_REMINDER_TIME,
    MERM_RT_HOURS,
)
from custom.enikshay.exceptions import ENikshayCaseNotFound


class MermParams(jsonobject.JsonObject):
    IMEI = jsonobject.StringProperty(required=False, exclude_if_none=True)
    daily_reminder_status = jsonobject.StringProperty(required=False, exclude_if_none=True)
    daily_reminder_time = jsonobject.StringProperty(required=False, exclude_if_none=True)  # HH:mm
    refill_reminder_status = jsonobject.StringProperty(required=False, exclude_if_none=True)
    refill_reminder_datetime = jsonobject.StringProperty(
        required=False,
        exclude_if_none=True
    )  # yy/MM/dd HH:mm:ss
    RT_hours = jsonobject.StringProperty(
        required=False,
        exclude_if_none=True
    )  # 1 = 12 hours; i.e. for 3 days - RT_hours = 6


class PatientPayload(jsonobject.JsonObject):
    beneficiary_id = jsonobject.StringProperty(required=True)
    enikshay_id = jsonobject.StringProperty(required=False)

    first_name = jsonobject.StringProperty(required=False)
    last_name = jsonobject.StringProperty(required=False)

    state_code = jsonobject.StringProperty(required=False)
    district_code = jsonobject.StringProperty(required=False)
    tu_code = jsonobject.StringProperty(required=False)
    phi_code = jsonobject.StringProperty(required=False, exclude_if_none=True)
    he_code = jsonobject.StringProperty(required=False, exclude_if_none=True)

    phone_numbers = jsonobject.StringProperty(required=False)
    merm_params = jsonobject.ObjectProperty(MermParams, required=False, exclude_if_none=True)

    treatment_start_date = jsonobject.StringProperty(required=False)

    treatment_supporter_name = jsonobject.StringProperty(required=False)
    treatment_supporter_phone_number = jsonobject.StringProperty(required=False)

    @classmethod
    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),
{'fraction_non_alphanumeric': 0.06387872451646628, 'fraction_numerical': 0.0012545739675901724, 'mean_word_length': 4.137486573576799, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4670036', 'n_tokens_mistral': 3183, 'n_tokens_neox': 3041, 'n_words': 396}