def lookup_case(search_field, search_id, domain, case_type): """ Attempt to find the case in CouchDB by the provided search_field and search_id. Returns a tuple with case (if found) and an error code (if there was an error in lookup). """ found = False if search_field == 'case_id': try: case = CommCareCase.get(search_id) if case.domain == domain and case.type == case_type: found = True except Exception: pass elif search_field == 'external_id': results = get_cases_in_domain_by_external_id(domain, search_id) if results: cases_by_type = [case for case in results if case.type == case_type] if not cases_by_type: return (None, LookupErrors.NotFound) elif len(cases_by_type) > 1: return (None, LookupErrors.MultipleResults) else: case = cases_by_type[0] found = True if found: return (case, None) else: return (None, LookupErrors.NotFound)
def get_case_by_external_id(domain, external_id, user): cases = get_cases_in_domain_by_external_id(domain, external_id) def filter_fcn(case): return not case.closed and user_can_access_case(user, case) cases = filter(filter_fcn, cases) if len(cases) == 1: return (cases[0], 1) else: return (None, len(cases))
def get_cases_by_external_id(domain, external_id, case_type=None): cases = get_cases_in_domain_by_external_id(domain, external_id) if case_type: return [case for case in cases if case.type == case_type] return cases
Attempt to find the case in CouchDB by the provided search_field and search_id. Returns a tuple with case (if found) and an error code (if there was an error in lookup). """ found = False if search_field == 'case_id': try: case = CommCareCase.get(search_id) if case.domain == domain and case.type == case_type: found = True except Exception: pass elif search_field == 'external_id': results = get_cases_in_domain_by_external_id(domain, search_id) if results: cases_by_type = [ case for case in results if case.type == case_type ] if not cases_by_type: return (None, LookupErrors.NotFound) elif len(cases_by_type) > 1: return (None, LookupErrors.MultipleResults) else: case = cases_by_type[0] found = True if found: return (case, None)
def get_case(self, external_id): [case] = get_cases_in_domain_by_external_id(self.domain, external_id) return case