Esempio n. 1
0
    def process_indicators(self, namespaces, domain, doc_dict):
        case_type = doc_dict.get('type')
        if not case_type:
            return

        case_indicator_defs = []
        for namespace in namespaces:
            case_indicator_defs.extend(CaseIndicatorDefinition.get_all(
                namespace,
                domain,
                case_type=case_type
            ))
        if not case_indicator_defs:
            return

        try:
            indicator_case = IndicatorCase.get_or_create_from_dict(doc_dict)[0]
            indicator_case.update_indicators_in_bulk(
                case_indicator_defs, logger=pillow_eval_logging
            )
        except Exception as e:
            pillow_eval_logging.error(
                "Error creating for MVP Indicator for form %(form_id)s: "
                "%(error)s" % {
                    'form_id': doc_dict['_id'],
                    'error': e,
                },
            )

        # Now Update Data From Case to All Related Xforms (ewwww)
        xform_ids = doc_dict.get('xform_ids', [])
        if not xform_ids:
            return

        for xform_id in xform_ids:
            try:
                xform_dict = XFormInstance.get_db().get(xform_id)
                xform_doc = IndicatorXForm.get_or_create_from_dict(xform_dict)[0]
            except ResourceNotFound:
                pillow_eval_logging.error(
                    "Could not find an XFormInstance with id %(xform_id)s "
                    "related to Case %(case_id)s" % {
                        'xform_id': xform_id,
                        'case_id': doc_dict['_id'],
                    }
                )
                continue

            if not xform_doc.xmlns:
                continue
            related_xform_defs = []
            for namespace in namespaces:
                related_xform_defs.extend(
                    CaseDataInFormIndicatorDefinition.get_all(
                        namespace,
                        domain,
                        xmlns=xform_doc.xmlns
                    )
                )
            xform_doc.update_indicators_in_bulk(
                related_xform_defs,
                logger=pillow_eval_logging,
            )