Пример #1
0
    def get_payload(self, repeat_record, form):
        from casexml.apps.case.xform import cases_referenced_by_xform

        logger.debug('DHIS2: Form domain "%s" XMLNS "%s"', form['domain'], form['xmlns'])
        if form['xmlns'] not in (REGISTER_CHILD_XMLNS, GROWTH_MONITORING_XMLNS, RISK_ASSESSMENT_XMLNS):
            # This is not a form we care about
            raise IgnoreDocument


        settings = Dhis2Settings.for_domain(form['domain'])
        dhis2_api = Dhis2Api(settings.dhis2['host'], settings.dhis2['username'], settings.dhis2['password'],
                             settings.dhis2['top_org_unit_name'])
        cases = cases_referenced_by_xform(form)
        case = next(c for c in cases if c.type == CASE_TYPE)
        event = None

        if form['xmlns'] == REGISTER_CHILD_XMLNS:
            # Create a DHIS2 tracked entity instance from the form's case and
            # enroll in the nutrition assessment programme.
            logger.debug('DHIS2: Processing Register Child form')
            push_case(case, dhis2_api)
            #  We just need to enroll. No event to create
            raise IgnoreDocument

        elif form['xmlns'] == GROWTH_MONITORING_XMLNS:
            logger.debug('DHIS2: Processing Growth Monitoring form')
            if not getattr(case, 'external_id', None):
                logger.info('Register Child form must be processed before Growth Monitoring form')
                return  # Try again later
            self._update_instance(dhis2_api, case)
            # Create a paediatric nutrition assessment event.
            program_id = dhis2_api.get_program_id('Paediatric Nutrition Assessment')
            program_stage_id = dhis2_api.get_program_stage_id('Nutrition Assessment')
            event = dhis2_api.form_to_event(program_id, form, NUTRITION_ASSESSMENT_EVENT_FIELDS, program_stage_id,
                                            case['external_id'])

        elif form['xmlns'] == RISK_ASSESSMENT_XMLNS:
            logger.debug('DHIS2: Processing Risk Assessment form')
            if not getattr(case, 'external_id', None):
                logger.info('Register Child form must be processed before Risk Assessment form')
                return  # Try again later
            self._update_instance(dhis2_api, case)
            # Check whether the case needs to be enrolled in the Risk Assessment Program
            program_id = dhis2_api.get_program_id('Underlying Risk Assessment')
            if not dhis2_api.enrolled_in(case['external_id'], 'Underlying Risk Assessment'):
                today = json_format_date(date.today())
                program_data = {dhis2_attr: case[cchq_attr]
                                for cchq_attr, dhis2_attr in RISK_ASSESSMENT_PROGRAM_FIELDS.iteritems()}
                dhis2_api.enroll_in_id(case['external_id'], program_id, today, program_data)
            # Create a risk assessment event.
            program_stage_id = dhis2_api.get_program_stage_id('Underlying Risk Assessment')
            event = dhis2_api.form_to_event(program_id, form, RISK_ASSESSMENT_EVENT_FIELDS, program_stage_id,
                                            case['external_id'])

        return json.dumps(event, default=json_serializer)
Пример #2
0
    def get_payload(self, repeat_record, form):
        if not Dhis2Settings.is_enabled_for_domain(form['domain']):
            return

        from casexml.apps.case.models import CommCareCase  # avoid circular import
        settings = Dhis2Settings.for_domain(form['domain'])
        dhis2_api = Dhis2Api(settings.dhis2.host, settings.dhis2.username, settings.dhis2.password,
                             settings.dhis2.top_org_unit_name)
        if form['xmlns'] == 'http://openrosa.org/formdesigner/b6a45e8c03a6167acefcdb225ee671cbeb332a40':
            # This is a growth monitoring form. It needs to be converted into
            # a paediatric nutrition assessment event.
            nutrition_id = dhis2_api.get_program_id('Paediatric Nutrition Assessment')
            event = dhis2_api.form_to_event(nutrition_id, form, NUTRITION_ASSESSMENT_EVENT_FIELDS)
            # If the form is not to be forwarded, the event will be None
            return json.dumps(event, default=json_serializer) if event else None

        elif form['xmlns'] == 'http://openrosa.org/formdesigner/39F09AD4-B770-491E-9255-C97B34BDD7FC':
            # This is a risk assessment form. It needs to be converted into a
            # risk assessment event.
            risk_id = dhis2_api.get_program_id('Underlying Risk Assessment')
            # Check whether the case needs to be enrolled in the Risk Assessment Program
            cases = CommCareCase.get_by_xform_id(form.get_id)
            if len(cases) != 1:
                # TODO: Fail permanently
                return None
            case = cases[0]
            if not case.get('external_id'):
                # This case has not yet been pushed to DHIS2.
                # TODO: Try again later
                return None
            # TODO: Test one-line alternative below with risk assessment forms
            # Either ...
            if not dhis2_api.enrolled_in(case['external_id'], 'Underlying Risk Assessment'):
                today = date.today().strftime('%Y-%m-%d')
                program_data = {dhis2_attr: case[cchq_attr]
                                for cchq_attr, dhis2_attr in RISK_ASSESSMENT_PROGRAM_FIELDS.iteritems()}
                dhis2_api.enroll_in_id(case['external_id'], risk_id, today, program_data)
            event = dhis2_api.form_to_event(risk_id, form, RISK_ASSESSMENT_EVENT_FIELDS)
            # ... or just ...
            # event = dhis2_api.form_to_event(risk_id, form, RISK_ASSESSMENT_EVENT_FIELDS, case['external_id'])
            # ...?
            return json.dumps(event, default=json_serializer) if event else None

        else:
            # This is not a form we care about
            return None
Пример #3
0
        elif form['xmlns'] == RISK_ASSESSMENT_XMLNS:
            logger.debug('DHIS2: Processing Risk Assessment form')
            if not getattr(case, 'external_id', None):
                logger.info(
                    'Register Child form must be processed before Risk Assessment form'
                )
                return  # Try again later
            self._update_instance(dhis2_api, case)
            # Check whether the case needs to be enrolled in the Risk Assessment Program
            program_id = dhis2_api.get_program_id('Underlying Risk Assessment')
            if not dhis2_api.enrolled_in(case['external_id'],
                                         'Underlying Risk Assessment'):
                today = json_format_date(date.today())
                program_data = {
                    dhis2_attr: case[cchq_attr]
                    for cchq_attr, dhis2_attr in
                    RISK_ASSESSMENT_PROGRAM_FIELDS.iteritems()
                }
                dhis2_api.enroll_in_id(case['external_id'], program_id, today,
                                       program_data)
            # Create a risk assessment event.
            program_stage_id = dhis2_api.get_program_stage_id(
                'Underlying Risk Assessment')
            event = dhis2_api.form_to_event(program_id, form,
                                            RISK_ASSESSMENT_EVENT_FIELDS,
                                            program_stage_id,
                                            case['external_id'])

        return json.dumps(event, default=json_serializer)