Example #1
0
def _get_public_locations(person_case, episode_case):
    PublicPersonLocationHierarchy = namedtuple('PersonLocationHierarchy', 'sto dto tu phi')
    try:
        phi_location_id = None
        if episode_case:
            phi_location_id = episode_case.dynamic_case_properties().get('treatment_initiating_facility_id')
        # fallback to person_case.owner_id in case treatment_initiating_facility_id not set on episode
        # or if no episode case was passed
        if not phi_location_id:
            phi_location_id = person_case.owner_id
        phi_location = SQLLocation.active_objects.get(domain=person_case.domain, location_id=phi_location_id)
    except SQLLocation.DoesNotExist:
        raise NikshayLocationNotFound(
            """Location with id {location_id} not found.
            This is the diagnosing facility id for person with id: {person_id}"""
            .format(location_id=person_case.owner_id, person_id=person_case.case_id)
        )

    try:
        tu_location = phi_location.parent
        district_location = tu_location.parent
        city_location = district_location.parent
        state_location = city_location.parent
    except AttributeError:
        raise NikshayLocationNotFound("Location structure error for person: {}".format(person_case.case_id))
    try:
        return PublicPersonLocationHierarchy(
            sto=state_location.metadata['nikshay_code'],
            dto=district_location.metadata['nikshay_code'],
            tu=tu_location.metadata['nikshay_code'],
            phi=phi_location.metadata['nikshay_code'],
        )
    except (KeyError, AttributeError) as e:
        raise NikshayCodeNotFound("Nikshay codes not found: {}".format(e))
Example #2
0
def get_person_locations(person_case):
    PersonLocationHierarchy = namedtuple('PersonLocationHierarchy', 'sto dto tu phi')
    try:
        phi_location = SQLLocation.objects.get(location_id=person_case.owner_id)
    except SQLLocation.DoesNotExist:
        raise NikshayLocationNotFound(
            "Location with id {location_id} not found. This is the owner for person with id: {person_id}"
            .format(location_id=person_case.owner_id, person_id=person_case.case_id)
        )

    try:
        tu_location = phi_location.parent
        district_location = tu_location.parent
        city_location = district_location.parent
        state_location = city_location.parent
    except AttributeError:
        raise NikshayLocationNotFound("Location structure error for person: {}".format(person_case.case_id))
    try:
        # TODO: verify how location codes will be stored
        return PersonLocationHierarchy(
            sto=state_location.metadata['nikshay_code'],
            dto=district_location.metadata['nikshay_code'],
            tu=tu_location.metadata['nikshay_code'],
            phi=phi_location.metadata['nikshay_code'],
        )
    except (KeyError, AttributeError) as e:
        raise NikshayCodeNotFound("Nikshay codes not found: {}".format(e))
Example #3
0
def _get_private_locations(person_case):
    PrivatePersonLocationHierarchy = namedtuple('PersonLocationHierarchy',
                                                'sto dto pcp tu')
    try:
        pcp_location = SQLLocation.active_objects.get(
            domain=person_case.domain, location_id=person_case.owner_id)
    except SQLLocation.DoesNotExist:
        raise NikshayLocationNotFound(
            "Location with id {location_id} not found. This is the owner for person with id: {person_id}"
            .format(location_id=person_case.owner_id,
                    person_id=person_case.case_id))

    try:
        tu_location_nikshay_code = pcp_location.metadata[
            'nikshay_tu_id'] or None
    except KeyError:
        tu_location_nikshay_code = None

    try:
        district_location = pcp_location.parent
        city_location = district_location.parent
        state_location = city_location.parent
    except AttributeError:
        raise NikshayLocationNotFound(
            "Location structure error for person: {}".format(
                person_case.case_id))
    try:
        return PrivatePersonLocationHierarchy(
            sto=state_location.metadata['nikshay_code'],
            dto=district_location.metadata['nikshay_code'],
            # HACK: remove this when we have all of the "HE ids" imported from Nikshay
            pcp=pcp_location.metadata.get('nikshay_code') or None,
            tu=tu_location_nikshay_code)
    except (KeyError, AttributeError) as e:
        raise NikshayCodeNotFound("Nikshay codes not found: {}".format(e))
Example #4
0
def _get_private_locations(person_case, episode_case=None):
    """
    if episode case is passed
    - find the location id as episode_treating_hospital on episode
     - if this location has nikshay code
      - consider this as the pcp
     - else
      - fallback to owner id as the pcp itself
    """
    PrivatePersonLocationHierarchy = namedtuple('PersonLocationHierarchy', 'sto dto pcp tu')
    pcp_location = None
    if episode_case:
        episode_treating_hospital = episode_case.get_case_property('episode_treating_hospital')
        if episode_treating_hospital:
            pcp_location = SQLLocation.active_objects.get_or_None(
                location_id=episode_treating_hospital)
            if pcp_location:
                if not pcp_location.metadata.get('nikshay_code'):
                    pcp_location = None
    if not pcp_location:
        try:
            pcp_location = SQLLocation.active_objects.get(
                domain=person_case.domain, location_id=person_case.owner_id)
        except SQLLocation.DoesNotExist:
            raise NikshayLocationNotFound(
                "Location with id {location_id} not found. This is the owner for person with id: {person_id}"
                .format(location_id=person_case.owner_id, person_id=person_case.case_id)
            )

    try:
        tu_location_nikshay_code = pcp_location.metadata['nikshay_tu_id'] or None
    except KeyError:
        tu_location_nikshay_code = None

    try:
        district_location = pcp_location.parent
        city_location = district_location.parent
        state_location = city_location.parent
    except AttributeError:
        raise NikshayLocationNotFound("Location structure error for person: {}".format(person_case.case_id))
    try:
        dto_code = district_location.metadata['nikshay_code']
        # HACK: remove this when we have all of the "HE ids" imported from Nikshay
        pcp_code = pcp_location.metadata.get('nikshay_code') or None
        # append 0 in beginning to make the code 6-digit
        if pcp_code and len(pcp_code) == 5:
            pcp_code = '0' + pcp_code
        if not dto_code:
            dto_code = pcp_location.metadata.get('rntcp_district_code')
        return PrivatePersonLocationHierarchy(
            sto=state_location.metadata['nikshay_code'],
            dto=dto_code,
            pcp=pcp_code,
            tu=tu_location_nikshay_code
        )
    except (KeyError, AttributeError) as e:
        raise NikshayCodeNotFound("Nikshay codes not found: {}".format(e))
Example #5
0
def get_health_establishment_hierarchy_codes(location):
    def get_parent(parent_type):
        try:
            return location.get_ancestors().get(location_type__name=parent_type)
        except SQLLocation.DoesNotExist:
            raise NikshayLocationNotFound(
                "Missing parent of type {location_type} for {location_id}".format(
                    location_type=parent_type,
                    location_id=location.location_id))

    HealthEstablishmentHierarchy = namedtuple('HealthEstablishmentHierarchy', 'stcode dtcode')
    state = get_parent('sto')
    district = get_parent('dto')
    try:
        return HealthEstablishmentHierarchy(
            stcode=state.metadata['nikshay_code'],
            dtcode=district.metadata['nikshay_code'],
        )
    except (KeyError, AttributeError) as e:
        raise NikshayCodeNotFound("Nikshay codes not found: {}".format(e))
Example #6
0
def _get_private_locations(person_case):
    PrivatePersonLocationHierarchy = namedtuple('PersonLocationHierarchy',
                                                'sto dto pcp tu')
    try:
        pcp_location = SQLLocation.active_objects.get(
            domain=person_case.domain, location_id=person_case.owner_id)
    except SQLLocation.DoesNotExist:
        raise NikshayLocationNotFound(
            "Location with id {location_id} not found. This is the owner for person with id: {person_id}"
            .format(location_id=person_case.owner_id,
                    person_id=person_case.case_id))

    try:
        tu_id = person_case.dynamic_case_properties().get('tu_choice')
        tu_location = SQLLocation.active_objects.get(
            domain=person_case.domain,
            location_id=tu_id,
        )
        tu_location_nikshay_code = tu_location.metadata['nikshay_code']
    except (SQLLocation.DoesNotExist, KeyError, AttributeError):
        tu_location_nikshay_code = None

    try:
        district_location = pcp_location.parent
        city_location = district_location.parent
        state_location = city_location.parent
    except AttributeError:
        raise NikshayLocationNotFound(
            "Location structure error for person: {}".format(
                person_case.case_id))
    try:
        return PrivatePersonLocationHierarchy(
            sto=state_location.metadata['nikshay_code'],
            dto=district_location.metadata['nikshay_code'],
            pcp=pcp_location.metadata['nikshay_code'],
            tu=tu_location_nikshay_code)
    except (KeyError, AttributeError) as e:
        raise NikshayCodeNotFound("Nikshay codes not found: {}".format(e))