(
    dhis2_server: ConnectionSettings,
    clay: CassiusMarcellus,
    requested_period: Optional[str],
)
return clay
Fetch data sets of data elements for last quarter from ``dhis2_server``
    and update the data elements corresponding case properties in
    ``case_block`` in place.
Fetch data sets of data elements for last quarter from ``dhis2_server``
    and update the data elements corresponding case properties in
    ``case_block`` in place.
Fetch data sets of data elements for last quarter from dhis2_server and update the data elements corresponding case properties in case_block in place .
def set_case_updates(
    dhis2_server: ConnectionSettings,
    clay: CassiusMarcellus,
    requested_period: Optional[str],
) -> CassiusMarcellus:
    """
    Fetch data sets of data elements for last quarter from ``dhis2_server``
    and update the data elements corresponding case properties in
    ``case_block`` in place.
    """
    last_imported = clay.case.get_case_property(LAST_IMPORTED_PROPERTY)
    if last_imported:
        last_imported = datetime.strptime(last_imported, '%Y-%m-%d').date()
    # Several of the data elements we want belong to the same data
    # sets. Only fetch a data set if we don't already have it.
    data_set_cache = {}
    for mapping in iter_mappings():
        if not mapping.data_set_id:
            raise ValueError(
                f'Mapping {mapping} does not include data set ID. '
                'Use **fetch_onse_data_set_ids** command.')
        for period in get_periods(requested_period, last_imported):
            data_set_cache.setdefault(period, {})
            if mapping.data_set_id not in data_set_cache[period]:
                data_set_cache[period][mapping.data_set_id] = fetch_data_set(
                    dhis2_server,
                    mapping.data_set_id,
                    # facility case external_id is set to its DHIS2 org
                    # unit. This is the DHIS2 facility whose data we
                    # want to import.
                    org_unit_id=clay.case.external_id,
                    period=period,
                )
            data_values = data_set_cache[period][mapping.data_set_id]

            if data_values is None:
                continue  # No data for this facility. Try previous quarter
            found, total = get_data_element_total(
                mapping.data_element_id,
                data_values,
            )
            if found:
                clay.updates[mapping.case_property] = total
                break
            # else: look for values in previous quarter
    clay.updates[LAST_IMPORTED_PROPERTY] = date.today().isoformat()
    return clay
def set_case_updates ( dhis2_server : ConnectionSettings , clay : CassiusMarcellus , requested_period : Optional [ str ] , ) -> CassiusMarcellus : last_imported = clay . case . get_case_property ( LAST_IMPORTED_PROPERTY ) if last_imported : last_imported = datetime . strptime ( last_imported , '%Y-%m-%d' ) . date ( ) # Several of the data elements we want belong to the same data # sets. Only fetch a data set if we don't already have it. data_set_cache = { } for mapping in iter_mappings ( ) : if not mapping . data_set_id : raise ValueError ( f'Mapping {mapping} does not include data set ID. ' 'Use **fetch_onse_data_set_ids** command.' ) for period in get_periods ( requested_period , last_imported ) : data_set_cache . setdefault ( period , { } ) if mapping . data_set_id not in data_set_cache [ period ] : data_set_cache [ period ] [ mapping . data_set_id ] = fetch_data_set ( dhis2_server , mapping . data_set_id , # facility case external_id is set to its DHIS2 org # unit. This is the DHIS2 facility whose data we # want to import. org_unit_id = clay . case . external_id , period = period , ) data_values = data_set_cache [ period ] [ mapping . data_set_id ] if data_values is None : continue # No data for this facility. Try previous quarter found , total = get_data_element_total ( mapping . data_element_id , data_values , ) if found : clay . updates [ mapping . case_property ] = total break # else: look for values in previous quarter clay . updates [ LAST_IMPORTED_PROPERTY ] = date . today ( ) . isoformat ( ) return clay