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

        case_indicators = []
        for namespace in namespaces:
            case_indicators.extend(CaseIndicatorDefinition.get_all(namespace, domain, case_type=case_type))

        if case_indicators:
            case_doc = CommCareCase.get(doc_dict['_id'])
            case_doc.update_indicators_in_bulk(case_indicators, logger=pillow_logging)

        xform_ids = doc_dict.get('xform_ids', [])
        for namespace in namespaces:
            for xform_id in xform_ids:
                try:
                    xform_doc = XFormInstance.get(xform_id)
                    if not xform_doc.xmlns:
                        continue
                    related_xform_indicators = CaseDataInFormIndicatorDefinition.get_all(namespace, domain,
                                                                                         xmlns=xform_doc.xmlns)
                    xform_doc.update_indicators_in_bulk(related_xform_indicators, logger=pillow_logging)
                except ResourceNotFound:
                    pillow_logging.error("[INDICATOR %(namespace)s %(domain)s] Tried to form indicator %(xform_id)s "
                                         "from case %(case_id)s and failed." % {
                                             'namespace': namespace,
                                             'domain': domain,
                                             'xform_id': xform_id,
                                             'case_id': doc_dict['_id'],
                                         })
예제 #2
0
    def get_indicators_by_doc(self, doc_dict):
        indicators = []

        case_type = doc_dict.get('type')
        domain = doc_dict.get('domain')
        form_ids = doc_dict.get('xform_ids') or []

        if case_type and domain:
            # relevant namespaces
            namespaces = INDICATOR_CONFIG[domain]

            for namespace in namespaces:
                # need all the relevant Case Indicator Defs
                indicators.extend(CaseIndicatorDefinition.get_all(namespace, domain, case_type=case_type))

                # we also need to update forms that are related to this case and might use data which
                # may have just been updated
                for form_id in form_ids:
                    try:
                        xform = XFormInstance.get(form_id)
                        if xform.xmlns and isinstance(xform, XFormInstance):
                            case_related_indicators = CaseDataInFormIndicatorDefinition.get_all(namespace, domain,
                                xmlns=xform.xmlns)
                            logging.info("Grabbed Related XForm %s and now processing %d indicators." %
                                         (form_id, len(case_related_indicators)))
                            FormIndicatorPillow.update_indicators_in_doc_instance(xform, case_related_indicators)
                            logging.info("Done processing indicators.")
                    except Exception as e:
                        logging.error("Error grabbing related xform for CommCareCase %s: %s" % (doc_dict['_id'], e))

        return indicators
 def insert_dob_into_form(self, indicator_slug, xmlns, shared_args, version=1):
     child_dob = CaseDataInFormIndicatorDefinition.increment_or_create_unique(
         *shared_args,
         slug=indicator_slug,
         xmlns=xmlns,
         case_property="dob_calc",
         version=version
     )
     print(child_dob)
class CaseDataInFormAdminInterface(BaseIndicatorAdminInterface):
    name = CaseDataInFormIndicatorDefinition.get_nice_name()
    description = ("Grabs the specified case property value of the form's related case and inserts it into the form's "
                   "indicators.")
    slug = "form_case_data"
    document_class = CaseDataInFormIndicatorDefinition
    form_class = CaseDataInFormIndicatorDefinitionForm

    @property
    def headers(self):
        header = super(CaseDataInFormAdminInterface, self).headers
        header.insert_column(DataTablesColumn("XMLNS or Label"), -3)
        header.insert_column(DataTablesColumn("Case Property"), -3)
        return header
예제 #5
0
    def process_indicators(self, doc_dict, domain, namespaces):
        case_type = doc_dict.get('type')
        if not case_type:
            pillow_eval_logging.info("CaseIndicatorPillow did not find a case type for %(domain)s, "
                                     "doc id: %(doc_id)s" % {
                                         'domain': domain,
                                         'doc_id': doc_dict['_id'],
                                     })
            return

        case_indicators = []
        for namespace in namespaces:
            case_indicators.extend(CaseIndicatorDefinition.get_all(namespace, domain, case_type=case_type))

        if case_indicators:
            case_doc = CommCareCase.get(doc_dict['_id'])
            case_doc.update_indicators_in_bulk(case_indicators, logger=pillow_logging)
        else:
            pillow_eval_logging.info("CaseIndicatorPillow could not find case indicators for %(domain)s, "
                                     "doc id: %(doc_id)s" % {
                                         'domain': domain,
                                         'doc_id': doc_dict['_id'],
                                     })

        xform_ids = doc_dict.get('xform_ids', [])
        if not xform_ids:
            pillow_eval_logging.info("CaseIndicatorPillow could not find related xforms for case in %(domain)s, "
                                     "doc id: %(doc_id)s" % {
                                         'domain': domain,
                                         'doc_id': doc_dict['_id'],
                                     })

        for xform_id in xform_ids:
            try:
                xform_doc = XFormInstance.get(xform_id)
                if not xform_doc.xmlns:
                    continue
                related_xform_indicators = []
                for namespace in namespaces:
                    related_xform_indicators.extend(CaseDataInFormIndicatorDefinition.get_all(
                        namespace, domain, xmlns=xform_doc.xmlns))
                xform_doc.update_indicators_in_bulk(related_xform_indicators, logger=pillow_eval_logging)
            except ResourceNotFound:
                pillow_logging.error("[INDICATOR %(domain)s] Tried to form grab indicators for %(xform_id)s "
                                     "from case %(case_id)s and failed." % {
                                         'domain': domain,
                                         'xform_id': xform_id,
                                         'case_id': doc_dict['_id'],
                                     })
예제 #6
0
    def get_indicators_by_doc(self, doc_dict):
        indicators = []

        case_type = doc_dict.get('type')
        domain = doc_dict.get('domain')
        form_ids = doc_dict.get('xform_ids') or []

        if case_type and domain:
            # relevant namespaces
            namespaces = INDICATOR_CONFIG[domain]

            for namespace in namespaces:
                # need all the relevant Case Indicator Defs
                indicators.extend(
                    CaseIndicatorDefinition.get_all(namespace,
                                                    domain,
                                                    case_type=case_type))

                # we also need to update forms that are related to this case and might use data which
                # may have just been updated
                for form_id in form_ids:
                    try:
                        xform = XFormInstance.get(form_id)
                        if xform.xmlns and isinstance(xform, XFormInstance):
                            case_related_indicators = CaseDataInFormIndicatorDefinition.get_all(
                                namespace, domain, xmlns=xform.xmlns)
                            logging.info(
                                "Grabbed Related XForm %s and now processing %d indicators."
                                % (form_id, len(case_related_indicators)))
                            FormIndicatorPillow.update_indicators_in_doc_instance(
                                xform, case_related_indicators)
                            logging.info("Done processing indicators.")
                    except Exception as e:
                        logging.error(
                            "Error grabbing related xform for CommCareCase %s: %s"
                            % (doc_dict['_id'], e))

        return indicators
    def handle(self, **options):
        for domain in MVP.DOMAINS:
            shared_args=(
                MVP.NAMESPACE,
                domain
            )

            # Registration forms
            self.create_form_alias_indicators(CHILD_REGISTRATION_QUESTION_IDS,
                MVP.REGISTRATION_FORMS.get('child_registration'), domain, shared_args)

            # All the visit forms
            self.create_form_alias_indicators(CHILD_VISIT_QUESTION_IDS,
                MVP.VISIT_FORMS.get('child_visit'), domain, shared_args)
            self.create_form_alias_indicators(HOUSEHOLD_VISIT_QUESTION_IDS,
                MVP.VISIT_FORMS.get('household_visit'), domain, shared_args)
            self.create_form_alias_indicators(PREGNANCY_VISIT_QUESTION_IDS,
                MVP.VISIT_FORMS.get('pregnancy_visit'), domain, shared_args)

            # All the close forms
            self.create_form_alias_indicators(CHILD_CLOSE_FORM_QUESTION_IDS,
                MVP.CLOSE_FORMS.get('child_close'), domain, shared_args)
            self.create_form_alias_indicators(PREGNANCY_CLOSE_FORM_QUESTION_IDS,
                MVP.CLOSE_FORMS.get('pregnancy_close'), domain, shared_args)

            pregnancy_edd = CaseDataInFormIndicatorDefinition.increment_or_create_unique(
                *shared_args,
                slug="pregnancy_edd",
                xmlns=MVP.VISIT_FORMS.get('pregnancy_visit'),
                case_property="edd_calc",
                version=1
            )
            print(pregnancy_edd)

            pregnancy_end = CaseDataInFormIndicatorDefinition.increment_or_create_unique(
                *shared_args,
                slug="pregnancy_end",
                xmlns=MVP.VISIT_FORMS.get('pregnancy_visit'),
                case_property="closed_on",
                version=1
            )
            print(pregnancy_end)

            child_visit_referral = CHILD_VISIT_QUESTION_IDS.get('referral_type', {}).get(domain)
            if child_visit_referral:
                child_case_referral_type = FormDataInCaseIndicatorDefinition.increment_or_create_unique(
                    *shared_args,
                    slug="referral_type",
                    case_type='child',
                    xmlns=MVP.VISIT_FORMS.get('child_visit'),
                    **child_visit_referral
                )
                print(child_case_referral_type)

            pregnancy_visit_referral = PREGNANCY_VISIT_QUESTION_IDS.get('referral_type', {}).get(domain)
            if pregnancy_visit_referral:
                pregnancy_case_referral_type = FormDataInCaseIndicatorDefinition.increment_or_create_unique(
                    *shared_args,
                    slug="referral_type",
                    case_type='pregnancy',
                    xmlns=MVP.VISIT_FORMS.get('pregnancy_visit'),
                    **pregnancy_visit_referral
                )
                print(pregnancy_case_referral_type)

            visit_hospital = CHILD_VISIT_QUESTION_IDS.get('visit_hospital', {}).get(domain)
            if visit_hospital:
                visit_hospital_case = FormDataInCaseIndicatorDefinition.increment_or_create_unique(
                    *shared_args,
                    slug="visit_hospital",
                    case_type='child',
                    xmlns=MVP.VISIT_FORMS.get('child_visit'),
                    **visit_hospital
                )
                print(visit_hospital_case)

            immediate_danger_sign = CHILD_VISIT_QUESTION_IDS.get('immediate_danger_sign', {}).get(domain)
            if immediate_danger_sign:
                immediate_danger_sign_case = FormDataInCaseIndicatorDefinition.increment_or_create_unique(
                    *shared_args,
                    slug="immediate_danger_sign",
                    case_type='child',
                    xmlns=MVP.VISIT_FORMS.get('child_visit'),
                    **immediate_danger_sign
                )
                print(immediate_danger_sign_case)

            diarrhea_medication_in_case = CHILD_VISIT_QUESTION_IDS.get('diarrhea_medication', {}).get(domain)
            if diarrhea_medication_in_case:
                diarrhea_medication_case = FormDataInCaseIndicatorDefinition.increment_or_create_unique(
                    *shared_args,
                    slug="diarrhea_medication",
                    case_type='child',
                    xmlns=MVP.VISIT_FORMS.get('child_visit'),
                    **diarrhea_medication_in_case
                )
                print(diarrhea_medication_case)

            fever_medication_in_case = CHILD_VISIT_QUESTION_IDS.get('fever_medication', {}).get(domain)
            if fever_medication_in_case:
                fever_medication_case = FormDataInCaseIndicatorDefinition.increment_or_create_unique(
                    *shared_args,
                    slug="fever_medication",
                    case_type='child',
                    xmlns=MVP.VISIT_FORMS.get('child_visit'),
                    **fever_medication_in_case
                )
                print(fever_medication_case)

            self.insert_dob_into_form('child_dob', MVP.VISIT_FORMS.get('child_visit'),
                shared_args)

            self.insert_dob_into_form('child_dob', MVP.CLOSE_FORMS.get('child_close'),
                shared_args)
예제 #8
0
    def process_indicators(self, doc_dict, domain, namespaces):
        case_type = doc_dict.get('type')
        if not case_type:
            pillow_eval_logging.info(
                "CaseIndicatorPillow did not find a case type for %(domain)s, "
                "doc id: %(doc_id)s" % {
                    'domain': domain,
                    'doc_id': doc_dict['_id'],
                })
            return

        case_indicators = []
        for namespace in namespaces:
            case_indicators.extend(
                CaseIndicatorDefinition.get_all(namespace,
                                                domain,
                                                case_type=case_type))

        if case_indicators:
            case_doc = CommCareCase.get(doc_dict['_id'])
            case_doc.update_indicators_in_bulk(case_indicators,
                                               logger=pillow_logging)
        else:
            pillow_eval_logging.info("CaseIndicatorPillow could not find "
                                     "case indicators for %(domain)s, "
                                     "doc id: %(doc_id)s" % {
                                         'domain': domain,
                                         'doc_id': doc_dict['_id'],
                                     })

        xform_ids = doc_dict.get('xform_ids', [])
        if not xform_ids:
            pillow_eval_logging.info("CaseIndicatorPillow could not find "
                                     "related xforms for case in %(domain)s, "
                                     "doc id: %(doc_id)s" % {
                                         'domain': domain,
                                         'doc_id': doc_dict['_id'],
                                     })

        for xform_id in xform_ids:
            try:
                with XFormInstance.get_locked_obj(_id=xform_id) as xform_doc:
                    if not xform_doc.xmlns:
                        continue
                    related_xform_indicators = []
                    for namespace in namespaces:
                        related_xform_indicators.extend(
                            CaseDataInFormIndicatorDefinition.get_all(
                                namespace, domain, xmlns=xform_doc.xmlns))
                    xform_doc.update_indicators_in_bulk(
                        related_xform_indicators,
                        logger=pillow_eval_logging,
                    )
            except ResourceNotFound:
                pillow_logging.error(
                    "[INDICATOR %(domain)s] Tried to form grab indicators "
                    "for %(xform_id)s "
                    "from case %(case_id)s and failed." % {
                        'domain': domain,
                        'xform_id': xform_id,
                        'case_id': doc_dict['_id'],
                    })
예제 #9
0
def process_indicators_for_case(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
        ))

    try:
        indicator_case = IndicatorCase.wrap_for_indicator_db(doc_dict)
        indicator_case.update_indicators_in_bulk(
            case_indicator_defs, logger=pillow_logging,
            save_on_update=False
        )
        indicator_case.save()
    except Exception as e:
        pillow_logging.error(
            "Error creating for MVP Indicator for form %(form_id)s: "
            "%(error)s" % {
                'form_id': doc_dict['_id'],
                'error': e,
            },
        )
        raise

    # 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:
        related_xform_indicators = []
        try:
            # first try to get the doc from the indicator DB
            xform_doc = IndicatorXForm.get(xform_id)
        except ResourceNotFound:
            # if that fails fall back to the main DB
            try:
                xform_dict = XFormInstance.get_db().get(xform_id)
                xform_doc = IndicatorXForm.wrap_for_indicator_db(xform_dict)
                related_xform_indicators = get_form_indicators(namespaces, domain, xform_doc.xmlns)
            except ResourceNotFound:
                pillow_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

        for namespace in namespaces:
            related_xform_indicators.extend(
                CaseDataInFormIndicatorDefinition.get_all(
                    namespace,
                    domain,
                    xmlns=xform_doc.xmlns
                )
            )
        xform_doc.update_indicators_in_bulk(
            related_xform_indicators,
            logger=pillow_logging,
            save_on_update=False
        )
        xform_doc.save()
예제 #10
0
def process_indicators_for_case(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))

    try:
        indicator_case = IndicatorCase.wrap_for_indicator_db(doc_dict)
        indicator_case.update_indicators_in_bulk(case_indicator_defs,
                                                 logger=pillow_logging,
                                                 save_on_update=False)
        indicator_case.save()
    except Exception as e:
        pillow_logging.error(
            "Error creating for MVP Indicator for form %(form_id)s: "
            "%(error)s" % {
                'form_id': doc_dict['_id'],
                'error': e,
            }, )
        raise

    # 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:
        related_xform_indicators = []
        try:
            # first try to get the doc from the indicator DB
            xform_doc = IndicatorXForm.get(xform_id)
        except ResourceNotFound:
            # if that fails fall back to the main DB
            try:
                xform_dict = XFormInstance.get_db().get(xform_id)
                xform_doc = IndicatorXForm.wrap_for_indicator_db(xform_dict)
                related_xform_indicators = get_form_indicators(
                    namespaces, domain, xform_doc.xmlns)
            except ResourceNotFound:
                pillow_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

        for namespace in namespaces:
            related_xform_indicators.extend(
                CaseDataInFormIndicatorDefinition.get_all(
                    namespace, domain, xmlns=xform_doc.xmlns))
        xform_doc.update_indicators_in_bulk(related_xform_indicators,
                                            logger=pillow_logging,
                                            save_on_update=False)
        xform_doc.save()
예제 #11
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,
            )