Exemplo n.º 1
0
        def _test():
            form, case = _save_form_and_case(self)
            process_indicators_for_case(['mvp_indicators'], case.domain, case.to_json())
            updated_form = IndicatorXForm.get(form._id)
            self.addCleanup(updated_form.delete)
            computed = updated_form.computed_['mvp_indicators']
            self.assertEqual(29, len(computed))
            self.assertEqual('child_visit_form', computed['child_visit_form']['value'])

            process_indicators_for_form(['mvp_indicators'], form.domain, form.to_json())
            updated_form = IndicatorXForm.get(form._id)
            updated_computed = updated_form.computed_['mvp_indicators']
            self.assertEqual(29, len(updated_computed))
            self.assertEqual('child_visit_form', updated_computed['child_visit_form']['value'])
Exemplo n.º 2
0
        def _test():
            form, case = _save_form_and_case(self)
            process_indicators_for_case(['mvp_indicators'], case.domain, case.to_json())
            updated_form = IndicatorXForm.get(form._id)
            self.addCleanup(updated_form.delete)
            computed = updated_form.computed_['mvp_indicators']
            self.assertEqual(29, len(computed))
            self.assertEqual('child_visit_form', computed['child_visit_form']['value'])

            process_indicators_for_form(['mvp_indicators'], form.domain, form.to_json())
            updated_form = IndicatorXForm.get(form._id)
            updated_computed = updated_form.computed_['mvp_indicators']
            self.assertEqual(29, len(updated_computed))
            self.assertEqual('child_visit_form', updated_computed['child_visit_form']['value'])
Exemplo n.º 3
0
    def test_form_pillow_indicators(self):
        form_id = self._save_doc_to_db("indicator_form.json", XFormInstance)
        form_instance = XFormInstance.get(form_id)

        # Form Label Indicator
        form_label = FormLabelIndicatorDefinition.increment_or_create_unique(
            INDICATOR_TEST_NAMESPACE,
            INDICATOR_TEST_DOMAIN,
            slug="create_form",
            xmlns="http://openrosa.org/formdesigner/indicator-create-xmlns",
        )
        form_label.save()

        # Form Alias
        form_alias = FormDataAliasIndicatorDefinition.increment_or_create_unique(
            INDICATOR_TEST_NAMESPACE,
            INDICATOR_TEST_DOMAIN,
            slug="club_name",
            question_id="location.club",
            xmlns="http://openrosa.org/formdesigner/indicator-create-xmlns",
        )
        form_alias.save()

        self.form_pillow.run_burst()

        indicator_form = IndicatorXForm.get(form_id)
        self.assertNotEqual(indicator_form.get_db().dbname, form_instance.get_db().dbname)
        self.assertNotEqual(indicator_form.computed_, {})
Exemplo n.º 4
0
    def get_results_with_key(self, key, user_id=None, datespan=None, date_group_level=None, reduce=False):
        view_kwargs = dict()
        if datespan:
            view_kwargs.update(
                startkey=key+datespan.startdate_key_utc,
                endkey=key+datespan.enddate_key_utc+[{}]
            )
        else:
            view_kwargs.update(
                startkey=key,
                endkey=key+[{}]
            )
        if date_group_level:
            base_level = 5 if user_id else 4
            view_kwargs.update(
                group=True,
                group_level=base_level+date_group_level
            )
        else:
            view_kwargs.update(
                reduce=reduce
            )

        # Pull Data from the MVP-only DB
        from mvp_docs.models import IndicatorXForm
        db = IndicatorXForm.get_db()
        section = self.couch_view.split('/')
        couch_view = "%s_indicators/%s" % (section[0], section[1])

        return cache_core.cached_view(db, couch_view, cache_expire=60*60*6, **view_kwargs)
Exemplo n.º 5
0
    def test_form_pillow_indicators(self):
        since = get_current_seq(XFormInstance.get_db())
        form_id = self._save_doc_to_db('indicator_form.json', XFormInstance)
        form_instance = XFormInstance.get(form_id)

        # Form Label Indicator
        form_label = FormLabelIndicatorDefinition.increment_or_create_unique(
            INDICATOR_TEST_NAMESPACE,
            INDICATOR_TEST_DOMAIN,
            slug='create_form',
            xmlns='http://openrosa.org/formdesigner/indicator-create-xmlns',
        )
        form_label.save()

        # Form Alias
        form_alias = FormDataAliasIndicatorDefinition.increment_or_create_unique(
            INDICATOR_TEST_NAMESPACE,
            INDICATOR_TEST_DOMAIN,
            slug='club_name',
            question_id='location.club',
            xmlns='http://openrosa.org/formdesigner/indicator-create-xmlns',
        )
        form_alias.save()
        self.form_pillow.process_changes(since=since, forever=False)

        indicator_form = IndicatorXForm.get(form_id)
        self.assertNotEqual(indicator_form.get_db().dbname,
                            form_instance.get_db().dbname)
        self.assertNotEqual(indicator_form.computed_, {})
Exemplo n.º 6
0
    def test_form_pillow_indicators(self):
        form_id = self._save_doc_to_db('indicator_form.json', XFormInstance)
        form_instance = XFormInstance.get(form_id)

        # Form Label Indicator
        form_label = FormLabelIndicatorDefinition.increment_or_create_unique(
            INDICATOR_TEST_NAMESPACE,
            INDICATOR_TEST_DOMAIN,
            slug='create_form',
            xmlns='http://openrosa.org/formdesigner/indicator-create-xmlns',
        )
        form_label.save()

        # Form Alias
        form_alias = FormDataAliasIndicatorDefinition.increment_or_create_unique(
            INDICATOR_TEST_NAMESPACE,
            INDICATOR_TEST_DOMAIN,
            slug='club_name',
            question_id='location.club',
            xmlns='http://openrosa.org/formdesigner/indicator-create-xmlns',
        )
        form_alias.save()
        self.form_pillow.process_changes(since=None, forever=False)

        indicator_form = IndicatorXForm.get(form_id)
        self.assertNotEqual(
            indicator_form.get_db().dbname, form_instance.get_db().dbname
        )
        self.assertNotEqual(indicator_form.computed_, {})
Exemplo n.º 7
0
    def get_results_with_key(self,
                             key,
                             user_id=None,
                             datespan=None,
                             date_group_level=None,
                             reduce=False):
        view_kwargs = dict()
        if datespan:
            view_kwargs.update(startkey=key + datespan.startdate_key_utc,
                               endkey=key + datespan.enddate_key_utc + [{}])
        else:
            view_kwargs.update(startkey=key, endkey=key + [{}])
        if date_group_level:
            base_level = 5 if user_id else 4
            view_kwargs.update(group=True,
                               group_level=base_level + date_group_level)
        else:
            view_kwargs.update(reduce=reduce)

        # Pull Data from the MVP-only DB
        from mvp_docs.models import IndicatorXForm
        db = IndicatorXForm.get_db()
        section = self.couch_view.split('/')
        couch_view = "%s_indicators/%s" % (section[0], section[1])

        return cache_core.cached_view(db,
                                      couch_view,
                                      cache_expire=60 * 60 * 6,
                                      **view_kwargs)
Exemplo n.º 8
0
    def process_indicators(self, namespaces, domain, doc_dict):
        if not doc_dict.get('initial_processing_complete', False):
            return
        xmlns = doc_dict.get('xmlns')
        if not xmlns:
            return

        form_indicator_defs = []
        for namespace in namespaces:
            form_indicator_defs.extend(
                FormIndicatorDefinition.get_all(namespace, domain, xmlns=xmlns)
            )

        if not form_indicator_defs:
            return

        try:
            indicator_form = IndicatorXForm.get_or_create_from_dict(doc_dict)[0]
            indicator_form.update_indicators_in_bulk(
                form_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,
                },
            )
Exemplo n.º 9
0
def process_indicators_for_form(namespaces, domain, doc_dict):
    if not doc_dict.get('initial_processing_complete', False):
        return
    xmlns = doc_dict.get('xmlns')
    if not xmlns:
        return

    form_indicator_defs = get_form_indicators(namespaces, domain, xmlns)
    if not form_indicator_defs:
        return

    try:
        indicator_form = IndicatorXForm.wrap_for_indicator_db(doc_dict)
        indicator_form.update_indicators_in_bulk(
            form_indicator_defs, logger=pillow_logging,
            save_on_update=False
        )
        indicator_form.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
Exemplo n.º 10
0
def process_indicators_for_form(namespaces, domain, doc_dict):
    if not doc_dict.get('initial_processing_complete', False):
        return
    xmlns = doc_dict.get('xmlns')
    if not xmlns:
        return

    form_indicator_defs = get_form_indicators(namespaces, domain, xmlns)
    if not form_indicator_defs:
        return

    try:
        indicator_form = IndicatorXForm.wrap_for_indicator_db(doc_dict)
        indicator_form.update_indicators_in_bulk(form_indicator_defs,
                                                 logger=pillow_logging,
                                                 save_on_update=False)
        indicator_form.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
Exemplo n.º 11
0
        def _test():
            form, case = _save_form_and_case()
            MVPCaseIndicatorPillow().change_transform(case.to_json())
            updated_form = IndicatorXForm.get(form._id)
            computed = updated_form.computed_['mvp_indicators']
            self.assertEqual(29, len(computed))
            self.assertEqual('child_visit_form', computed['child_visit_form']['value'])

            MVPFormIndicatorPillow().change_transform(form.to_json())
            updated_form = IndicatorXForm.get(form._id)
            updated_computed = updated_form.computed_['mvp_indicators']
            self.assertEqual(29, len(updated_computed))
            self.assertEqual('child_visit_form', updated_computed['child_visit_form']['value'])

            # cleanup
            updated_form.delete()
            form.delete()
            case.delete()
Exemplo n.º 12
0
        def _test():
            form, case = _save_form_and_case()
            MVPCaseIndicatorPillow().change_transform(case.to_json())
            updated_form = IndicatorXForm.get(form._id)
            computed = updated_form.computed_['mvp_indicators']
            self.assertEqual(29, len(computed))
            self.assertEqual('child_visit_form',
                             computed['child_visit_form']['value'])

            MVPFormIndicatorPillow().change_transform(form.to_json())
            updated_form = IndicatorXForm.get(form._id)
            updated_computed = updated_form.computed_['mvp_indicators']
            self.assertEqual(29, len(updated_computed))
            self.assertEqual('child_visit_form',
                             updated_computed['child_visit_form']['value'])

            # cleanup
            updated_form.delete()
            form.delete()
            case.delete()
Exemplo n.º 13
0
 def test_delete_doc(self):
     form = IndicatorXForm()
     form.save()
     self.assertTrue(IndicatorXForm.get_db().doc_exist(form._id))
     self.form_pillow.change_transform({
         '_id': form._id,
         'doc_type': 'XFormArchived'
     })
     self.assertFalse(IndicatorXForm.get_db().doc_exist(form._id))
Exemplo n.º 14
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()
Exemplo n.º 15
0
 def test_delete_doc(self):
     form = IndicatorXForm()
     form.save()
     self.assertTrue(IndicatorXForm.get_db().doc_exist(form._id))
     self.form_pillow.change_transform({'_id': form._id, 'doc_type': 'XFormArchived'})
     self.assertFalse(IndicatorXForm.get_db().doc_exist(form._id))
Exemplo n.º 16
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()
Exemplo n.º 17
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,
            )