示例#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))
示例#2
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))
示例#3
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))
示例#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))
示例#5
0
def _get_district_location(pcp_location):
    try:
        district_location = pcp_location.parent
        if district_location.location_type.code != 'dto':
            raise NikshayLocationNotFound(
                "Parent location of {} is not a district".format(pcp_location))
        return district_location
    except AttributeError:
        raise NikshayLocationNotFound(
            "Parent location of {} not found".format(pcp_location))
示例#6
0
    def create_ayush_referral_payload(cls, episode_case):
        episode_case_properties = episode_case.dynamic_case_properties()
        person_case = get_person_case_from_episode(episode_case.domain,
                                                   episode_case.case_id)

        location = cls._get_location(
            episode_case_properties.get("registered_by"),
            field_name="registered_by",
            related_case_type="episode",
            related_case_id=episode_case.case_id,
        )
        if not location.user_id:
            raise NikshayLocationNotFound(
                "Location {} does not have a virtual location user".format(
                    location.location_id))

        return cls(
            EventID=AYUSH_REFERRAL_EVENT,
            EventOccurDate=string_to_date_or_None(
                episode_case.get_case_property(
                    FIRST_PRESCRIPTION_VOUCHER_REDEEMED_DATE)),
            BeneficiaryUUID=location.user_id,
            BeneficiaryType='ayush_other',
            EpisodeID=episode_case.case_id,
            Location=episode_case_properties.get("registered_by"),
            DTOLocation=_get_district_location_id(location),
            PersonId=person_case.get_case_property('person_id'),
            AgencyId=cls._get_agency_id(
                episode_case),  # not migrated from UATBC, so we're good
            # Incentives are not yet approved in eNikshay
            EnikshayApprover=None,
            EnikshayRole=None,
            EnikshayApprovalDate=None,
        )
示例#7
0
 def _get_dmc_code(self, test_case, test_case_properties):
     dmc_location_id = test_case_properties.get("testing_facility_id", None)
     if not dmc_location_id:
         # fallback to lab referral case owner id for older versions of app
         lab_referral_case = get_lab_referral_from_test(
             test_case.domain, test_case.get_id)
         dmc_location_id = lab_referral_case.owner_id
     if not dmc_location_id:
         raise NikshayRequiredValueMissing(
             "Value missing for dmc_code/testing_facility_id for test case: "
             + test_case.get_id)
     dmc = SQLLocation.active_objects.get_or_None(
         location_id=dmc_location_id)
     if not dmc:
         raise NikshayLocationNotFound(
             "Location with id: {location_id} not found."
             "This is the testing facility id assigned for test: {test_case_id}"
             .format(location_id=dmc_location_id,
                     test_case_id=test_case.get_id))
     nikshay_code = dmc.metadata.get('nikshay_code')
     if not nikshay_code or (isinstance(nikshay_code, basestring)
                             and not nikshay_code.isdigit()):
         raise NikshayRequiredValueMissing(
             "Inappropriate value for dmc, got value: {}".format(
                 nikshay_code))
     return dmc.metadata.get('nikshay_code')
示例#8
0
 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))
 def get_parent(parent_type):
     parent = location.get_parent_of_type(parent_type)
     if not parent:
         raise NikshayLocationNotFound(
             "Missing parent of type {location_type} for {location_id}".
             format(location_type=parent_type,
                    location_id=location.location_id))
     return parent[0]
示例#10
0
def _is_submission_from_test_location(case_id, owner_id):
    try:
        phi_location = SQLLocation.objects.get(location_id=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=owner_id, person_id=case_id))
    return phi_location.metadata.get('is_test', "yes") == "yes"
示例#11
0
 def _get_agency_id(episode_case):
     agency_id = episode_case.get_case_property(
         'bets_notifying_provider_user_id')
     if not agency_id:
         raise NikshayLocationNotFound(
             "Episode {} does not have an agency".format(
                 episode_case.case_id))
     agency_user = CommCareUser.get_by_user_id(agency_id)
     return agency_user.raw_username
示例#12
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))
示例#13
0
def is_valid_test_submission(test_case):
    try:
        lab_referral_case = get_lab_referral_from_test(test_case.domain, test_case.get_id)
    except ENikshayCaseNotFound:
        return False

    try:
        dmc_location = SQLLocation.objects.get(location_id=lab_referral_case.owner_id)
    except SQLLocation.DoesNotExist:
        raise NikshayLocationNotFound(
            "Location with id {location_id} not found. This is the owner for lab referral with id: \
            {lab_referral_id}"
            .format(location_id=lab_referral_case.owner_id, lab_referral_id=lab_referral_case.case_id)
        )
    return dmc_location.metadata.get('is_test', "yes") == "yes"
示例#14
0
 def _get_location(cls,
                   location_id,
                   field_name=None,
                   related_case_type=None,
                   related_case_id=None):
     try:
         return SQLLocation.objects.get(location_id=location_id)
     except SQLLocation.DoesNotExist:
         msg = "Location with id {location_id} not found.".format(
             location_id=location_id)
         if field_name and related_case_type and related_case_id:
             msg += " This is the {field_name} for {related_case_type} with id: {related_case_id}".format(
                 field_name=field_name,
                 related_case_type=related_case_type,
                 related_case_id=related_case_id,
             )
         raise NikshayLocationNotFound(msg)