def add_cases_to_case_group(domain, case_group_id, uploaded_data, progress_tracker): response = { 'errors': [], 'success': [], } try: case_group = CommCareCaseGroup.get(case_group_id) except ResourceNotFound: response['errors'].append(_("The case group was not found.")) return response num_rows = len(uploaded_data) progress_tracker(0, num_rows) for row_number, row in enumerate(uploaded_data): identifier = row.get('case_identifier') case = None if identifier is not None: case = get_case_by_identifier(domain, str(identifier)) if not case: response['errors'].append( _("Could not find case with identifier '{}'.").format( identifier)) elif case.doc_type != 'CommCareCase': response['errors'].append( _("It looks like the case with identifier '{}' " "is marked as deleted.").format(identifier))
def add_cases_to_case_group(domain, case_group_id, uploaded_data): response = {"errors": [], "success": []} try: case_group = CommCareCaseGroup.get(case_group_id) except ResourceNotFound: response["errors"].append(_("The case group was not found.")) return response for row in uploaded_data: identifier = row.get("case_identifier") case = None if identifier is not None: case = get_case_by_identifier(domain, str(identifier)) if not case: response["errors"].append(_("Could not find case with identifier '%s'." % identifier)) elif case.doc_type != "CommCareCase": response["errors"].append(_("It looks like the case with identifier '%s' is deleted" % identifier)) elif case._id in case_group.cases: response["errors"].append(_("A case with identifier %s already exists in this group." % identifier)) else: case_group.cases.append(case._id) response["success"].append(_("Case with identifier '%s' has been added to this group." % identifier)) if response["success"]: case_group.save() return response
def get_create_item_data(self, create_form): case_identifier = create_form.cleaned_data['case_identifier'] case = get_case_by_identifier(self.domain, case_identifier) if case is None: return { 'itemData': { 'id': case_identifier.replace(' ', '_'), 'identifier': case_identifier, 'message': _('Sorry, we could not a find a case that ' 'matched the identifier you provided.'), }, 'rowClass': 'warning', 'template': 'case-message-template', } item_data = self._get_item_data(case) if case._id in self.case_group.cases: message = '<span class="label label-important">%s</span>' % _("Case already in group") elif case.doc_type != 'CommCareCase': message = '<span class="label label-important">%s</span>' % _("It looks like this case was deleted.") else: message = '<span class="label label-success">%s</span>' % _("Case added") self.case_group.cases.append(case._id) self.case_group.save() item_data['message'] = message return { 'itemData': item_data, 'template': 'new-case-template', }
def get_create_item_data(self, create_form): case_identifier = create_form.cleaned_data['case_identifier'] case = get_case_by_identifier(self.domain, case_identifier) if case is None: return { 'itemData': { 'id': case_identifier.replace(' ', '_'), 'identifier': case_identifier, 'message': _('Sorry, we could not a find a case that ' 'matched the identifier you provided.'), }, 'rowClass': 'warning', 'template': 'case-message-template', } item_data = self._get_item_data(case) if case.case_id in self.case_group.cases: message = '<span class="label label-danger">%s</span>' % _("Case already in group")
def pull_child_entities(settings, dhis2_children): """ Create new child cases for nutrition tracking in CommCare. Sets external_id on new child cases, and CCHQ Case ID on DHIS2 tracked entity instances. (CCHQ Case ID is initially unset because the case is new and does not exist in CommCare.) :param settings: DHIS2 settings, incl. relevant domain :param dhis2_children: A list of dictionaries of TRACKED_ENTITY (i.e. "Child") tracked entities from the DHIS2 API where CCHQ Case ID is unset This fulfills the third requirement of `DHIS2 Integration`_. .. _DHIS2 Integration: https://www.dropbox.com/s/8djk1vh797t6cmt/WV Sri Lanka Detailed Requirements.docx """ dhis2_api = Dhis2Api(settings.dhis2['host'], settings.dhis2['username'], settings.dhis2['password'], settings.dhis2['top_org_unit_name']) for dhis2_child in dhis2_children: # Add each child separately. Although this is slower, it avoids problems if a DHIS2 API call fails # ("Instance" is DHIS2's friendly name for "id") logger.info('DHIS2: Syncing DHIS2 child "%s"', dhis2_child['Instance']) case = get_case_by_identifier( settings.domain, dhis2_child['Instance']) # Get case by external_id if case: case_id = case['case_id'] else: user = get_user_by_org_unit(settings.domain, dhis2_child['Org unit'], settings.dhis2['top_org_unit_name']) if not user: # No user is assigned to this or any higher organisation unit logger.error( 'DHIS2: Unable to import DHIS2 instance "%s"; there is no user at org unit "%s" or ' 'above to assign the case to.', dhis2_child['Instance'], dhis2_child['Org unit']) continue case_id = create_case_from_dhis2(dhis2_child, settings.domain, user) dhis2_child[CCHQ_CASE_ID] = case_id dhis2_api.update_te_inst(dhis2_child)
def test_case_by_identifier(self): self._send_case_to_es(case_type='ccuser') case = self._send_case_to_es() case.external_id = '123' case.save() case = CaseAccessors(self.domain).get_case(case.case_id) case_json = case.to_json() case_json['contact_phone_number'] = '234' es_case = transform_case_for_elasticsearch(case_json) send_to_elasticsearch('cases', es_case) self.es.indices.refresh(CASE_INDEX) self.assertEqual( get_case_by_identifier(self.domain, case.case_id).case_id, case.case_id) self.assertEqual( get_case_by_identifier(self.domain, '234').case_id, case.case_id) self.assertEqual( get_case_by_identifier(self.domain, '123').case_id, case.case_id)
def add_cases_to_case_group(domain, case_group_id, uploaded_data): response = { 'errors': [], 'success': [], } try: case_group = CommCareCaseGroup.get(case_group_id) except ResourceNotFound: response['errors'].append(_("The case group was not found.")) return response for row in uploaded_data: identifier = row.get('case_identifier') case = None if identifier is not None: case = get_case_by_identifier(domain, str(identifier)) if not case: response['errors'].append(_("Could not find case with identifier '%s'." % identifier)) elif case.doc_type != 'CommCareCase': response['errors'].append(_("It looks like the case with identifier '%s' is deleted" % identifier))
def pull_child_entities(settings, dhis2_children): """ Create new child cases for nutrition tracking in CommCare. Sets external_id on new child cases, and CCHQ Case ID on DHIS2 tracked entity instances. (CCHQ Case ID is initially unset because the case is new and does not exist in CommCare.) :param settings: DHIS2 settings, incl. relevant domain :param dhis2_children: A list of dictionaries of TRACKED_ENTITY (i.e. "Child") tracked entities from the DHIS2 API where CCHQ Case ID is unset This fulfills the third requirement of `DHIS2 Integration`_. .. _DHIS2 Integration: https://www.dropbox.com/s/8djk1vh797t6cmt/WV Sri Lanka Detailed Requirements.docx """ dhis2_api = Dhis2Api(settings.dhis2['host'], settings.dhis2['username'], settings.dhis2['password'], settings.dhis2['top_org_unit_name']) for dhis2_child in dhis2_children: # Add each child separately. Although this is slower, it avoids problems if a DHIS2 API call fails # ("Instance" is DHIS2's friendly name for "id") logger.info('DHIS2: Syncing DHIS2 child "%s"', dhis2_child['Instance']) case = get_case_by_identifier(settings.domain, dhis2_child['Instance']) # Get case by external_id if case: case_id = case['case_id'] else: user = get_user_by_org_unit(settings.domain, dhis2_child['Org unit'], settings.dhis2['top_org_unit_name']) if not user: # No user is assigned to this or any higher organisation unit logger.error('DHIS2: Unable to import DHIS2 instance "%s"; there is no user at org unit "%s" or ' 'above to assign the case to.', dhis2_child['Instance'], dhis2_child['Org unit']) continue case_id = create_case_from_dhis2(dhis2_child, settings.domain, user) dhis2_child[CCHQ_CASE_ID] = case_id dhis2_api.update_te_inst(dhis2_child)
def get_case_by_external_id(domain, external_id): """ Filter cases by external_id """ return get_case_by_identifier(domain, external_id)