Exemplo n.º 1
0
def update_patient(repeater, patient_uuid):
    """
    Fetch patient from OpenMRS, submit case update for all mapped case
    properties.

    .. NOTE:: OpenMRS UUID must be saved to "external_id" case property

    """
    if len(repeater.white_listed_case_types) != 1:
        raise ConfigurationError(
            _(f'{repeater.domain}: {repeater}: Error in settings: Unable to update '
              f'patients from OpenMRS unless only one case type is specified.')
        )
    case_type = repeater.white_listed_case_types[0]
    try:
        patient = get_patient_by_uuid(repeater.requests, patient_uuid)
    except (RequestException, ValueError) as err:
        raise OpenmrsException(
            _(f'{repeater.domain}: {repeater}: Error fetching Patient '
              f'{patient_uuid!r}: {err}')) from err

    case, error = importer_util.lookup_case(
        EXTERNAL_ID,
        patient_uuid,
        repeater.domain,
        case_type=case_type,
    )
    if error == LookupErrors.NotFound:
        if not repeater.openmrs_config.case_config.import_creates_cases:
            # We can't create cases via the Atom feed, just update them.
            # Nothing to do here.
            return
        default_owner: Optional[CommCareUser] = repeater.first_user
        case_block = get_addpatient_caseblock(case_type, default_owner,
                                              patient, repeater)
    elif error == LookupErrors.MultipleResults:
        # Multiple cases have been matched to the same patient.
        # Could be caused by:
        # * The cases were given the same identifier value. It could
        #   be user error, or case config assumed identifier was
        #   unique but it wasn't.
        # * PatientFinder matched badly.
        # * Race condition where a patient was previously added to
        #   both CommCare and OpenMRS.
        raise DuplicateCaseMatch(
            _(f'{repeater.domain}: {repeater}: More than one case found '
              f'matching unique OpenMRS UUID. case external_id: "{patient_uuid}"'
              ))
    else:
        case_block = get_updatepatient_caseblock(case, patient, repeater)

    if case_block:
        submit_case_blocks(
            [case_block.as_text()],
            repeater.domain,
            xmlns=XMLNS_OPENMRS,
            device_id=OPENMRS_ATOM_FEED_DEVICE_ID + repeater.get_id,
        )
Exemplo n.º 2
0
def get_case(
    repeater: OpenmrsRepeater,
    patient_uuid: str,
) -> Union[CommCareCase, CommCareCaseSQL, None]:

    case_type = repeater.white_listed_case_types[0]
    case, error = importer_util.lookup_case(
        EXTERNAL_ID,
        patient_uuid,
        repeater.domain,
        case_type=case_type,
    )
    if error == LookupErrors.MultipleResults:
        raise DuplicateCaseMatch(
            _(f'{repeater.domain}: {repeater}: More than one case found '
              'matching unique OpenMRS UUID. case external_id: '
              f'"{patient_uuid}". '))
    return case
Exemplo n.º 3
0
    )
    if another_case:
        case_str = get_case_str(case)
        another_case_str = get_case_str(another_case)
        message = (
            f'Unable to match {case_str} with OpenMRS patient "{external_id}": '
            f'{another_case_str} already exists with external_id="{external_id}".'
        )
    elif error == LookupErrors.MultipleResults:
        case_str = get_case_str(case)
        message = (
            f'Unable to match {case_str} with OpenMRS patient "{external_id}": '
            f'Multiple cases already exist with external_id="{external_id}".')
    else:  # error == LookupErrors.NotFound:
        return
    raise DuplicateCaseMatch(message)


def create_patient(requests, info, case_config):
    def get_name():
        return {
            property_: value_source.get_value(info)
            for property_, value_source in
            case_config.person_preferred_name.items()
            if (property_ in NAME_PROPERTIES and value_source.check_direction(
                DIRECTION_EXPORT) and value_source.get_value(info))
        }

    def get_address():
        return {
            property_: value_source.get_value(info)