def create_case_from_dhis2(dhis2_child, domain, user): """ Create a new case using the data pulled from DHIS2 :param dhis2_child: TRACKED_ENTITY (i.e. "Child") from DHIS2 :param domain: (str) The name of the domain :param user: (Document) The owner of the new case :return: New case ID """ case_id = uuid.uuid4().hex update = {k: dhis2_child[v] for k, v in NUTRITION_ASSESSMENT_PROGRAM_FIELDS.iteritems()} update['dhis_org_id'] = dhis2_child['Org unit'] # Do the inverse of push_case() to 'Gender' / 'child_gender' if 'child_gender' in update: if update['child_gender'] == 'Undefined': del update['child_gender'] else: update['child_gender'] = update['child_gender'].lower() caseblock = CaseBlock( create=True, case_id=case_id, owner_id=user.userID, user_id=user.userID, case_type=CASE_TYPE, case_name=update[CASE_NAME] if CASE_NAME else '', external_id=dhis2_child['Instance'], update=update ) casexml = ElementTree.tostring(caseblock.as_xml()) submit_case_blocks(casexml, domain) return case_id
def create_case_from_dhis2(dhis2_child, domain, user): """ Create a new case using the data pulled from DHIS2 :param dhis2_child: TRACKED_ENTITY (i.e. "Child") from DHIS2 :param domain: (str) The name of the domain :param user: (Document) The owner of the new case :return: New case ID """ case_id = uuid.uuid4().hex update = { k: dhis2_child[v] for k, v in NUTRITION_ASSESSMENT_PROGRAM_FIELDS.iteritems() } update['dhis_org_id'] = dhis2_child['Org unit'] # Do the inverse of push_case() to 'Gender' / 'child_gender' if 'child_gender' in update: if update['child_gender'] == 'Undefined': del update['child_gender'] else: update['child_gender'] = update['child_gender'].lower() caseblock = CaseBlock(create=True, case_id=case_id, owner_id=user.userID, user_id=user.userID, version=V2, case_type=CASE_TYPE, case_name=update[CASE_NAME] if CASE_NAME else '', external_id=dhis2_child['Instance'], update=update) casexml = ElementTree.tostring(caseblock.as_xml()) submit_case_blocks(casexml, domain) return case_id
def _update_instance(self, dhis2_api, case): instance = dhis2_api.get_te_inst(case['external_id']) instance.update({dhis2_attr: case[cchq_attr] for cchq_attr, dhis2_attr in NUTRITION_ASSESSMENT_PROGRAM_FIELDS.iteritems() if getattr(case, cchq_attr, None)}) if 'Gender' in instance: instance['Gender'] = instance['Gender'].capitalize() dhis2_api.update_te_inst(instance)
def _update_instance(self, dhis2_api, case): instance = dhis2_api.get_te_inst(case['external_id']) instance.update({ dhis2_attr: case[cchq_attr] for cchq_attr, dhis2_attr in NUTRITION_ASSESSMENT_PROGRAM_FIELDS.iteritems() if getattr(case, cchq_attr, None) }) if 'Gender' in instance: instance['Gender'] = instance['Gender'].capitalize() dhis2_api.update_te_inst(instance)
def push_case(case, dhis2_api): """ Create a DHIS2 tracked entity instance from the form's case and enroll in the nutrition assessment programme. """ if getattr(case, 'dhis_org_id', None): ou_id = case.dhis_org_id # App sets this case property from user custom data else: # This is an old case, or org unit is not set. Skip it return program_data = {dhis2_attr: case[cchq_attr] for cchq_attr, dhis2_attr in NUTRITION_ASSESSMENT_PROGRAM_FIELDS.iteritems() if getattr(case, cchq_attr, None)} if 'Gender' in program_data: # Gender is an optionSet. Options are "Male", "Female" and "Undefined" # cf. http://dhis1.internal.commcarehq.org:8080/dhis/api/optionSets/wG0c8ReYyNz.json program_data['Gender'] = program_data['Gender'].capitalize() # "male" -> "Male" else: program_data['Gender'] = 'Undefined' try: # Search for CCHQ Case ID in case previous attempt to register failed. instance = next(dhis2_api.gen_instances_with_equals(TRACKED_ENTITY, CCHQ_CASE_ID, case['_id'])) instance_id = instance['Instance'] except StopIteration: # Create a DHIS2 tracked entity instance instance = {CCHQ_CASE_ID: case['_id']} instance.update(program_data) try: instance_id = dhis2_api.add_te_inst(TRACKED_ENTITY, ou_id, instance) except (JsonApiError, Dhis2ApiQueryError) as err: logger.error('Failed to create DHIS2 entity from CCHQ case "%s". DHIS2 server error: %s', case['_id'], err) return # Enroll in Pediatric Nutrition Assessment date_of_visit = case['date_of_visit'] if getattr(case, 'date_of_visit', None) else date.today() try: response = dhis2_api.enroll_in(instance_id, 'Paediatric Nutrition Assessment', date_of_visit, program_data) except (JsonApiError, Dhis2ApiQueryError) as err: logger.error('Failed to push CCHQ case "%s" to DHIS2 program "%s". DHIS2 server error: %s', case['_id'], 'Paediatric Nutrition Assessment', err) return if response['status'] != 'SUCCESS': logger.error('Failed to push CCHQ case "%s" to DHIS2 program "%s". DHIS2 API error: %s', case['_id'], 'Paediatric Nutrition Assessment', response) return # Set external_id in CCHQ to flag the case as pushed. update_case_external_id(case, instance_id)
def push_child_entities(settings, children): """ Register child entities in DHIS2 and enroll them in the Pediatric Nutrition Assessment program. :param children: child_gmp cases where external_id is not set .. Note:: Once pushed, external_id is set to the ID of the tracked entity instance. This fulfills the second 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) # nutrition_id = dhis2_api.get_program_stage_id('Nutrition Assessment') nutrition_id = dhis2_api.get_program_id('Paediatric Nutrition Assessment') # For testing # fallback_org_unit = dhis2_api.get_resource_id('organisationUnits', 'Fermathe Clinic') today = date.today() for child in children: if getattr(child, 'dhis_org_id', None): ou_id = child.dhis_org_id # App sets this case property from user custom data else: # This is an old case, or org unit is not set. Skip it continue # For testing: # ou_id = fallback_org_unit try: # Search for CCHQ Case ID in case previous attempt to register failed. dhis2_child = next(dhis2_api.gen_instances_with_equals('Child', 'CCHQ Case ID', child['_id'])) dhis2_child_id = dhis2_child['Instance'] except StopIteration: # Register child entity in DHIS2, and set CCHQ Case ID. dhis2_child = { 'CCHQ Case ID': child['_id'], } dhis2_child_id = dhis2_api.add_te_inst(dhis2_child, 'Child', ou_id=ou_id) # Enroll in Pediatric Nutrition Assessment date_of_visit = child['date_of_visit'] if getattr(child, 'date_of_visit', None) else today program_data = {dhis2_attr: child[cchq_attr] for cchq_attr, dhis2_attr in NUTRITION_ASSESSMENT_PROGRAM_FIELDS.iteritems() if getattr(child, cchq_attr, None)} # TODO: DHIS2 says CHDR Number is optional, but throws an error if it's not passed response = dhis2_api.enroll_in_id(dhis2_child_id, nutrition_id, date_of_visit, program_data) if response['status'] != 'SUCCESS': # TODO: Log the error # Skip to the next case continue # Set external_id in CCHQ to flag the case as pushed. commcare_user = CommCareUser.get(child['owner_id']) caseblock = CaseBlock( create=False, case_id=child['_id'], version=V2, external_id=dhis2_child_id ) casexml = ElementTree.tostring(caseblock.as_xml()) submit_case_blocks(casexml, commcare_user.project.name)
def push_case(case, dhis2_api): """ Create a DHIS2 tracked entity instance from the form's case and enroll in the nutrition assessment programme. """ if getattr(case, 'dhis_org_id', None): ou_id = case.dhis_org_id # App sets this case property from user custom data else: # This is an old case, or org unit is not set. Skip it return program_data = { dhis2_attr: case[cchq_attr] for cchq_attr, dhis2_attr in NUTRITION_ASSESSMENT_PROGRAM_FIELDS.iteritems() if getattr(case, cchq_attr, None) } if 'Gender' in program_data: # Gender is an optionSet. Options are "Male", "Female" and "Undefined" # cf. http://dhis1.internal.commcarehq.org:8080/dhis/api/optionSets/wG0c8ReYyNz.json program_data['Gender'] = program_data['Gender'].capitalize( ) # "male" -> "Male" else: program_data['Gender'] = 'Undefined' try: # Search for CCHQ Case ID in case previous attempt to register failed. instance = next( dhis2_api.gen_instances_with_equals(TRACKED_ENTITY, CCHQ_CASE_ID, case['_id']))