def get_cleanliness_flag_from_scratch(domain, owner_id): footprint_info = get_case_footprint_info(domain, owner_id) cases_to_check = footprint_info.all_ids - footprint_info.base_ids if cases_to_check: closed_owned_case_ids = set(get_closed_case_ids(domain, owner_id)) cases_to_check = cases_to_check - closed_owned_case_ids if cases_to_check: # it wasn't in any of the open or closed IDs - it must be dirty reverse_index_ids = set(get_reverse_indexed_case_ids(domain, list(cases_to_check))) indexed_with_right_owner = (reverse_index_ids & (footprint_info.base_ids | closed_owned_case_ids)) if indexed_with_right_owner: return CleanlinessFlag(False, indexed_with_right_owner.pop()) # I'm not sure if this code can ever be hit, but if it is we should fail hard # until we can better understand it. raise IllegalCaseId('Owner {} in domain {} has an invalid index reference chain!!'.format( owner_id, domain )) return CleanlinessFlag(True, None)
def get_cleanliness_flag_from_scratch(domain, owner_id): footprint_info = get_case_footprint_info(domain, owner_id) cases_to_check = footprint_info.all_ids - footprint_info.base_ids if cases_to_check: closed_owned_case_ids = set(get_closed_case_ids(domain, owner_id)) cases_to_check = cases_to_check - closed_owned_case_ids if cases_to_check: # it wasn't in any of the open or closed IDs - it must be dirty reverse_index_infos = get_all_reverse_indices_info(domain, list(cases_to_check)) reverse_index_ids = set([r.case_id for r in reverse_index_infos]) indexed_with_right_owner = reverse_index_ids & (footprint_info.base_ids | closed_owned_case_ids) found_deleted_cases = False while indexed_with_right_owner: hint_id = indexed_with_right_owner.pop() infos_for_this_owner = _get_info_by_case_id(reverse_index_infos, hint_id) for info in infos_for_this_owner: try: case = CommCareCase.get(info.referenced_id) if case.doc_type == "CommCareCase": return CleanlinessFlag(False, hint_id) else: found_deleted_cases = True except ResourceNotFound: # the case doesn't exist - don't use it as a dirty flag found_deleted_cases = True if found_deleted_cases: # if we made it all the way to the end of the loop without returning anything # then the owner was only flagged as dirty due to missing cases, # This implies the owner is still clean. return CleanlinessFlag(True, None) else: # I don't believe code can ever be hit, but if it is we should fail hard # until we can better understand it. raise IllegalCaseId( "Owner {} in domain {} has an invalid index reference chain!!".format(owner_id, domain) ) return CleanlinessFlag(True, None)
def get_cleanliness_flag_from_scratch(domain, owner_id): footprint_info = get_case_footprint_info(domain, owner_id) cases_to_check = footprint_info.all_ids - footprint_info.base_ids if cases_to_check: closed_owned_case_ids = set(get_closed_case_ids(domain, owner_id)) cases_to_check = cases_to_check - closed_owned_case_ids if cases_to_check: # it wasn't in any of the open or closed IDs - it must be dirty reverse_index_ids = set( get_reverse_indexed_case_ids(domain, list(cases_to_check))) indexed_with_right_owner = ( reverse_index_ids & (footprint_info.base_ids | closed_owned_case_ids)) if indexed_with_right_owner: return CleanlinessFlag(False, indexed_with_right_owner.pop()) # I'm not sure if this code can ever be hit, but if it is we should fail hard # until we can better understand it. raise IllegalCaseId( 'Owner {} in domain {} has an invalid index reference chain!!'. format(owner_id, domain)) return CleanlinessFlag(True, None)
def get_closed_case_ids_for_owner(domain, owner_id): return get_closed_case_ids(domain, owner_id)