Example #1
0
 def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
              initial=None, error_class=ErrorList, label_suffix=':',
              empty_permitted=False, doc_id=None, domain=None):
     super(BaseIndicatorDefinitionForm, self).__init__(data, files, auto_id, prefix, initial, error_class,
                                                       label_suffix, empty_permitted, doc_id)
     self.domain = domain
     self.fields['namespace'].widget = forms.Select(choices=get_namespaces(self.domain, as_choices=True))
Example #2
0
    def get(self, request, domain, *args, **kwargs):
        namespaces = get_namespaces(domain)
        db = IndicatorDefinition.get_db()

        def _clean_indicator(doc):
            del doc['_id']
            del doc['_rev']
            del doc['domain']
            return doc

        data = {}
        for view_type in [
                'indicator_definitions',
                'dynamic_indicator_definitions',
        ]:
            data[view_type] = []
            for namespace in namespaces:
                key = ["type", namespace, domain]
                result = db.view(
                    'indicators/%s' % view_type,
                    reduce=False,
                    startkey=key,
                    endkey=key + [{}],
                    include_docs=True,
                ).all()
                data[view_type].extend(
                    [_clean_indicator(d['doc']) for d in result])

        response = HttpResponse(json.dumps(data),
                                content_type='application/json')
        response[
            'Content-Disposition'] = 'attachment; filename=%(domain)s-indicators.json' % {
                'domain': domain,
            }
        return response
Example #3
0
    def get(self, request, domain, *args, **kwargs):
        namespaces = get_namespaces(domain)
        db = IndicatorDefinition.get_db()

        def _clean_indicator(doc):
            del doc['_id']
            del doc['_rev']
            del doc['domain']
            return doc

        data = {}
        for view_type in [
            'indicator_definitions',
            'dynamic_indicator_definitions',
        ]:
            data[view_type] = []
            for namespace in namespaces:
                key = ["type", namespace, domain]
                result = db.view(
                    'indicators/%s' % view_type,
                    reduce=False,
                    startkey=key,
                    endkey=key+[{}],
                    include_docs=True,
                ).all()
                data[view_type].extend([_clean_indicator(d['doc']) for d in result])

        response = HttpResponse(json.dumps(data), mimetype='application/json')
        response['Content-Disposition'] = 'attachment; filename=%(domain)s-indicators.json' % {
            'domain': domain,
        }
        return response
Example #4
0
 def copy_indicators(self):
     failed = []
     success = []
     destination_domain = self.cleaned_data['destination_domain']
     override = self.cleaned_data['override_existing']
     available_namespaces = get_namespaces(destination_domain)
     indicator_ids = self.cleaned_data['indicator_ids']
     for indicator_id in indicator_ids:
         try:
             indicator = self.indicator_class.get(indicator_id)
             excluded_properties = ['last_modified', 'base_doc', 'namespace', 'domain', 'class_path']
             if indicator.namespace not in available_namespaces:
                 failed.append(dict(indicator=indicator.slug,
                                    reason='Indicator namespace not available for destination project.'))
                 continue
             properties = set(indicator.properties().keys())
             copied_properties = properties.difference(excluded_properties)
             copied_properties = dict([(p, getattr(indicator, p)) for p in copied_properties])
             copied_indicator = self.indicator_class.update_or_create_unique(indicator.namespace, destination_domain,
                                                                             save_on_update=override,
                                                                             **copied_properties)
             if copied_indicator and not copied_indicator.version:
                 copied_indicator.version = 1
                 copied_indicator.save()
             if copied_indicator:
                 success.append(copied_indicator.slug)
         except Exception as e:
             failed.append(dict(indicator=indicator_id,
                                reason='Could not retrieve indicator %s due to error %s:' % (indicator_id, e)))
     return {
         'success': success,
         'failure': failed,
     }
Example #5
0
    def change_transform(self, doc_dict):
        domain = doc_dict.get('domain')
        if not domain:
            return

        namespaces = get_namespaces(domain)
        if namespaces and 'computed_' in doc_dict:
            self.process_indicators(doc_dict, domain, namespaces)
Example #6
0
 def available_indicators(self):
     indicators = []
     for namespace in get_namespaces(self.domain):
         indicators.extend(
             self.indicator_class.get_all_of_type(namespace, self.domain))
     return [(i._id, "%s | v. %d | n: %s" %
              (i.slug, i.version if i.version else 0,
               get_namespace_name(i.domain, i.namespace)))
             for i in indicators]
Example #7
0
 def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
              initial=None, error_class=ErrorList, label_suffix=':',
              empty_permitted=False, doc_id=None, domain=None):
     super(BaseIndicatorDefinitionForm, self).__init__(data, files, auto_id, prefix, initial, error_class,
                                                       label_suffix, empty_permitted, doc_id)
     self.domain = domain
     self.fields['namespace'].widget = forms.Select(choices=get_namespaces(self.domain, as_choices=True))
     self.helper = FormHelper()
     self.helper.form_tag = False
     self.helper.label_class = 'col-sm-3'
     self.helper.field_class = 'col-sm-9'
Example #8
0
    def change_transform(self, doc_dict):
        from corehq.apps.indicators.utils import get_namespaces
        doc_type = doc_dict.get('doc_type')
        if doc_type in self._deleted_doc_types:
            self._delete_doc(doc_dict)

        else:
            domain = doc_dict.get('domain')
            if not domain:
                return
            namespaces = get_namespaces(domain)
            self.process_indicators(namespaces, domain, doc_dict)
Example #9
0
    def change_transform(self, doc_dict):
        from corehq.apps.indicators.utils import get_namespaces
        doc_type = doc_dict.get('doc_type')
        if doc_type in self._deleted_doc_types:
            self._delete_doc(doc_dict)

        else:
            domain = doc_dict.get('domain')
            if not domain:
                return
            namespaces = get_namespaces(domain)
            self.process_indicators(namespaces, domain, doc_dict)
Example #10
0
    def change_transform(self, doc_dict):
        domain = doc_dict.get('domain')
        if not domain:
            return

        namespaces = get_namespaces(domain)
        if namespaces and 'computed_' in doc_dict:
            self.process_indicators(doc_dict, domain, namespaces)
        else:
            pillow_eval_logging.info("Did not process indicator due to no namespace or computed for domain %(domain)s, "
                                     "doc id: %(doc_id)s" % {
                                         'domain': domain,
                                         'doc_id': doc_dict['_id'],
                                     })
Example #11
0
    def process_change(self, pillow_instance, change):
        from corehq.apps.indicators.utils import get_namespaces

        with _lock_manager(self._get_lock(change)):
            domain = _domain_for_change(change)
            doc_type = _doc_type_for_change(change)
            if self._should_process_change(domain, doc_type):
                doc_dict = change.get_document()
                if change.deleted or doc_type in self._deleted_doc_types:
                    self._delete_doc(change.get_document())
                    return

                namespaces = get_namespaces(domain)
                self.process_indicators(namespaces, domain, doc_dict)
Example #12
0
    def process_change(self, pillow_instance, change):
        from corehq.apps.indicators.utils import get_namespaces

        with _lock_manager(self._get_lock(change)):
            domain = _domain_for_change(change)
            doc_type = _doc_type_for_change(change)
            if self._should_process_change(domain, doc_type):
                doc_dict = change.get_document()
                if change.deleted or doc_type in self._deleted_doc_types:
                    self._delete_doc(change.get_document())
                    return

                namespaces = get_namespaces(domain)
                self.process_indicators(namespaces, domain, doc_dict)
Example #13
0
    def handle(self, *args, **options):
        xform_db = XFormInstance.get_db()

        for domain in get_indicator_domains():
            namespaces = get_namespaces(domain)
            indicators = []
            for namespace in namespaces:
                indicators.extend(FormIndicatorDefinition.get_all(namespace, domain))

            form_ids = get_form_ids_by_type(domain, 'XFormInstance',
                                            start=datetime.date(2013, 8, 1),
                                            end=datetime.date(2013, 10, 15))

            for doc in iter_docs(xform_db, form_ids):
                xfrom_doc = XFormInstance.wrap(doc)
                xfrom_doc.update_indicators_in_bulk(indicators, logger=logging)
Example #14
0
    def change_transform(self, doc_dict):
        domain = doc_dict.get('domain')
        if not domain:
            return

        namespaces = get_namespaces(domain)
        if namespaces and 'computed_' in doc_dict:
            self.process_indicators(doc_dict, domain, namespaces)
        else:
            pillow_eval_logging.info(
                "Did not process indicator due to no namespace "
                "or computed for domain %(domain)s, "
                "doc id: %(doc_id)s" % {
                    'domain': domain,
                    'doc_id': doc_dict['_id'],
                })
Example #15
0
 def copy_indicators(self):
     failed = []
     success = []
     destination_domain = self.cleaned_data['destination_domain']
     override = self.cleaned_data['override_existing']
     available_namespaces = get_namespaces(destination_domain)
     indicator_ids = self.cleaned_data['indicator_ids']
     for indicator_id in indicator_ids:
         try:
             indicator = self.indicator_class.get(indicator_id)
             excluded_properties = [
                 'last_modified', 'base_doc', 'namespace', 'domain',
                 'class_path'
             ]
             if indicator.namespace not in available_namespaces:
                 failed.append(
                     dict(
                         indicator=indicator.slug,
                         reason=
                         'Indicator namespace not available for destination project.'
                     ))
                 continue
             properties = set(indicator.properties().keys())
             copied_properties = properties.difference(excluded_properties)
             copied_properties = dict([(p, getattr(indicator, p))
                                       for p in copied_properties])
             copied_indicator = self.indicator_class.update_or_create_unique(
                 indicator.namespace,
                 destination_domain,
                 save_on_update=override,
                 **copied_properties)
             if copied_indicator and not copied_indicator.version:
                 copied_indicator.version = 1
                 copied_indicator.save()
             if copied_indicator:
                 success.append(copied_indicator.slug)
         except Exception as e:
             failed.append(
                 dict(indicator=indicator_id,
                      reason=
                      'Could not retrieve indicator %s due to error %s:' %
                      (indicator_id, e)))
     return {
         'success': success,
         'failure': failed,
     }
Example #16
0
    def handle(self, *args, **options):
        xform_db = XFormInstance.get_db()

        for domain in get_indicator_domains():
            namespaces = get_namespaces(domain)
            indicators = []
            for namespace in namespaces:
                indicators.extend(
                    FormIndicatorDefinition.get_all(namespace, domain))

            key = [domain, "by_type", "XFormInstance"]
            data = xform_db.view('couchforms/all_submissions_by_domain',
                                 startkey=key + ["2013-08-01"],
                                 endkey=key + ["2013-10-15"],
                                 reduce=False,
                                 include_docs=False).all()
            form_ids = [d['id'] for d in data]

            for doc in iter_docs(xform_db, form_ids):
                xfrom_doc = XFormInstance.wrap(doc)
                xfrom_doc.update_indicators_in_bulk(indicators, logger=logging)
    def handle(self, *args, **options):
        xform_db = XFormInstance.get_db()

        for domain in get_indicator_domains():
            namespaces = get_namespaces(domain)
            indicators = []
            for namespace in namespaces:
                indicators.extend(FormIndicatorDefinition.get_all(namespace, domain))

            key = [domain, "by_type", "XFormInstance"]
            data = xform_db.view(
                'couchforms/all_submissions_by_domain',
                startkey=key+["2013-08-01"],
                endkey=key+["2013-10-15"],
                reduce=False,
                include_docs=False
            ).all()
            form_ids = [d['id'] for d in data]

            for doc in iter_docs(xform_db, form_ids):
                xfrom_doc = XFormInstance.wrap(doc)
                xfrom_doc.update_indicators_in_bulk(indicators, logger=logging)
Example #18
0
    def get(self, request, domain, *args, **kwargs):
        namespaces = get_namespaces(domain)
        db = IndicatorDefinition.get_db()

        def _clean_indicator(doc):
            del doc["_id"]
            del doc["_rev"]
            del doc["domain"]
            return doc

        data = {}
        for view_type in ["indicator_definitions", "dynamic_indicator_definitions"]:
            data[view_type] = []
            for namespace in namespaces:
                key = ["type", namespace, domain]
                result = db.view(
                    "indicators/%s" % view_type, reduce=False, startkey=key, endkey=key + [{}], include_docs=True
                ).all()
                data[view_type].extend([_clean_indicator(d["doc"]) for d in result])

        response = HttpResponse(json.dumps(data), content_type="application/json")
        response["Content-Disposition"] = "attachment; filename=%(domain)s-indicators.json" % {"domain": domain}
        return response
Example #19
0
 def indicator_namespaces(self):
     return get_namespaces(self.domain)
Example #20
0
 def change_transform(self, doc_dict):
     domain = doc_dict.get('domain')
     if not domain:
         return
     namespaces = get_namespaces(domain)
     self.process_indicators(namespaces, domain, doc_dict)
Example #21
0
 def indicator_namespaces(self):
     return get_namespaces(self.domain)
Example #22
0
 def available_indicators(self):
     indicators = []
     for namespace in get_namespaces(self.domain):
         indicators.extend(self.indicator_class.get_all_of_type(namespace, self.domain))
     return [(i._id, "%s | v. %d | n: %s" % (i.slug, i.version if i.version else 0,
                                             get_namespace_name(i.domain, i.namespace))) for i in indicators]