コード例 #1
0
ファイル: views.py プロジェクト: mchampanis/core-hq
def _all_domain_stats():
    webuser_counts = defaultdict(lambda: 0)
    commcare_counts = defaultdict(lambda: 0)
    form_counts = defaultdict(lambda: 0)
    case_counts = defaultdict(lambda: 0)

    for row in get_db().view("users/by_domain", startkey=["active"], endkey=["active", {}], group_level=3).all():
        _, domain, doc_type = row["key"]
        value = row["value"]
        {"WebUser": webuser_counts, "CommCareUser": commcare_counts}[doc_type][domain] = value

    form_counts.update(
        dict(
            [
                (row["key"][0], row["value"])
                for row in get_db().view("reports/all_submissions", group=True, group_level=1).all()
            ]
        )
    )

    case_counts.update(
        dict(
            [
                (row["key"][0], row["value"])
                for row in get_db().view("hqcase/types_by_domain", group=True, group_level=1).all()
            ]
        )
    )

    return {"web_users": webuser_counts, "commcare_users": commcare_counts, "forms": form_counts, "cases": case_counts}
コード例 #2
0
ファイル: models.py プロジェクト: NoahCarnahan/commcare-hq
 def get_translations(cls, lang, key=None, one=False):
     if key:
         translations = []
         r = get_db().view('translations/popularity',
             startkey=[lang, key],
             endkey=[lang, key, {}],
             group=True
         ).all()
         r.sort(key=lambda x: -x['value'])
         for row in r:
             _, _, translation = row['key']
             translations.append(translation)
         if one:
             return translations[0] if translations else None
         return translations
     else:
         translations = defaultdict(list)
         r = get_db().view('translations/popularity',
             startkey=[lang],
             endkey=[lang, {}],
             group=True
         ).all()
         r.sort(key=lambda x: (x['key'][1], -x['value']))
         for row in r:
             _, key, translation = row['key']
             translations[key].append(translation)
         if one:
             return dict([(key, val[0]) for key, val in translations.items()])
         else:
             return translations
コード例 #3
0
ファイル: views.py プロジェクト: esoergel/commcare-hq
 def is_valid_user_id(user_id):
     if not user_id: return False
     try:
         get_db().get(user_id)
         return True
     except Exception:
         return False
コード例 #4
0
ファイル: utils.py プロジェクト: saketkanth/commcare-hq
def delete_indicator_doc():
    with CriticalSection([INDICATOR_CONFIG_LOCK_KEY]):
        try:
            get_db().delete_doc(INDICATOR_CONFIG_DOC_ID)
            get_indicator_config.clear()
        except ResourceNotFound:
            pass
コード例 #5
0
ファイル: views.py プロジェクト: nikhilvarma22/commcare-hq
def active_users(request):
    keys = []
    number_threshold = 15
    date_threshold_days_ago = 90
    date_threshold = json_format_datetime(datetime.utcnow() - timedelta(days=date_threshold_days_ago))
    key = make_form_couch_key(None, user_id="")
    for line in get_db().view("reports_forms/all_forms", startkey=key, endkey=key + [{}], group_level=3):
        if line["value"] >= number_threshold:
            keys.append(line["key"])

    final_count = defaultdict(int)

    def is_valid_user_id(user_id):
        if not user_id:
            return False
        try:
            get_db().get(user_id)
            return True
        except Exception:
            return False

    for time_type, domain, user_id in keys:
        if get_db().view(
            "reports_forms/all_forms", reduce=False, startkey=[time_type, domain, user_id, date_threshold], limit=1
        ):
            if True or is_valid_user_id(user_id):
                final_count[domain] += 1

    return json_response({"break_down": final_count, "total": sum(final_count.values())})
コード例 #6
0
ファイル: forms.py プロジェクト: aristide/commcare-hq
    def get_unknown_form_name(self, xmlns, app_id=None, none_if_not_found=False):
        if app_id is not None and app_id != '_MISSING_APP_ID':
            try:
                app = get_db().get(app_id)
            except ResourceNotFound:
                # must have been a weird app id, don't fail hard
                pass
            else:
                for module in app.get('modules', []):
                    for form in module['forms']:
                        if form['xmlns'] == xmlns:
                            return form['name'].values()[0]


        key = ["xmlns", self.domain, xmlns]
        results = cache_core.cached_view(
            get_db(),
            'reports_forms/name_by_xmlns',
            reduce=False,
            startkey=key,
            endkey=key + [{}],
            limit=1,
            cache_expire=60
        )

        try:
            data = list(results)[0]
        except IndexError:
            data = None

        if data:
            return data['value']
        return None if none_if_not_found else "Name Unknown"
コード例 #7
0
ファイル: calculations.py プロジェクト: elbowink/commcare-hq
def _all_domain_stats():
    webuser_counts = defaultdict(lambda: 0)
    commcare_counts = defaultdict(lambda: 0)
    form_counts = defaultdict(lambda: 0)
    case_counts = defaultdict(lambda: 0)

    for row in get_db().view('users/by_domain', startkey=["active"],
                             endkey=["active", {}], group_level=3).all():
        _, domain, doc_type = row['key']
        value = row['value']
        {
            'WebUser': webuser_counts,
            'CommCareUser': commcare_counts
        }[doc_type][domain] = value

    key = make_form_couch_key(None)
    form_counts.update(dict([(row["key"][1], row["value"]) for row in \
                                get_db().view("reports_forms/all_forms",
                                    group=True,
                                    group_level=2,
                                    startkey=key,
                                    endkey=key+[{}]
                             ).all()]))

    case_counts.update(dict([(row["key"][0], row["value"]) for row in \
                             get_db().view("hqcase/types_by_domain",
                                           group=True,group_level=1).all()]))

    return {"web_users": webuser_counts,
            "commcare_users": commcare_counts,
            "forms": form_counts,
            "cases": case_counts}
コード例 #8
0
ファイル: views.py プロジェクト: dimagi/carehq
def my_patient_activity(request, template_name="pactcarehq/patients_dashboard.html"):
    """Return a list of all the patients in the system"""
    #using per patient instance lookup...slow, but reuasable
    context= RequestContext(request)

    if request.user.is_superuser == True:
        #patients = Patient.objects.all()
        assignments = get_db().view('pactcarehq/chw_assigned_patients').all()
    else:
        assignments = get_db().view('pactcarehq/chw_assigned_patients', key=request.user.username).all()

    chw_patient_dict = {}
    for res in assignments:
        chw = res['key']
        pact_id = res['value'].encode('ascii')
        if not chw_patient_dict.has_key(chw):
            chw_patient_dict[chw] = []
        chw_patient_dict[chw].append(PactPatient.view('pactcarehq/patient_pact_ids', key=pact_id, include_docs=True).first())

    #sorted_pts = sorted(patients, key=lambda p: p.couchdoc.last_name)
    #keys = [p.couchdoc.pact_id for p in sorted_pts]
    #context= RequestContext(request)

    chws = sorted(chw_patient_dict.keys())
    #patients = sorted(patients, key=lambda x: x.couchdoc.last_name)
    context['chw_patients_arr'] = [(x, chw_patient_dict[x]) for x in chws]
    #context['chw_patients'] = chw_patient_dict
    return render_to_response(template_name, context_instance=context)
コード例 #9
0
ファイル: caselogic.py プロジェクト: dimagi/bhoma
def get_pats_with_updated_cases(clinic_id, zone, last_seq):
    """
    Given a clinic, zone, and last_seq id from couch, get the patients whose
    cases have been updated, returning a tuple of updated ids (in a list) and
    the last seq number.
    """
    CACHE_TIME = 4 * 60 * 60 # cache for 4 hours, in seconds
    
    # first try to get this from the cache
    def _build_cache_key(clinic_id, zone, last_seq):
        return "sync_patient_list:%s:%s:%s" % (clinic_id, zone, last_seq)
    cached_data = cache.get(_build_cache_key(clinic_id, zone, last_seq))
    if cached_data:
        return cached_data
    
    # first get the patient list of potential matches.  use the open case
    # list to get patient ids
    potential_case_list = get_db().view("case/open_for_chw_for_phone", key=[clinic_id, zone])
    possible_pat_ids = []
    for row in potential_case_list:
        if row["id"] not in possible_pat_ids:
            possible_pat_ids.append(row["id"])
    # new consumer
    consumer = Consumer(get_db())
    view_results = consumer.fetch(since=last_seq)
    pats_with_open_cases = []
    for res in view_results["results"]:
        id = res["id"]
        if id in possible_pat_ids and not id in pats_with_open_cases:
            pats_with_open_cases.append(id)
    
    updated_last_seq = view_results["last_seq"]
    ret = (pats_with_open_cases, updated_last_seq)
    cache.set(_build_cache_key(clinic_id, zone, last_seq), ret, CACHE_TIME)
    return ret
コード例 #10
0
ファイル: test_all_docs.py プロジェクト: nnestle/commcare-hq
 def test_get_all_doc_ids_for_domain_grouped_by_db(self):
     self.assertEqual(
         {key.uri: list(value) for key, value in
          get_all_doc_ids_for_domain_grouped_by_db(self.domain)},
         {get_db(None).uri: ['main_db_doc'],
          get_db('users').uri: ['users_db_doc'],
          get_db('fixtures').uri: []}
     )
コード例 #11
0
def get_dbs_from_app_label_and_map(app_label, app_db_map):
    if app_db_map and app_label in app_db_map:
        db_names = app_db_map[app_label]
        if not isinstance(db_names, (list, tuple, set)):
            db_names = [db_names]

        return [get_db(db_name) for db_name in set(db_names)]
    return [get_db()]
コード例 #12
0
ファイル: pactmodels.py プロジェクト: dimagi/carehq
    def ghetto_xml(self):
        xml_dict = {}
        xml_dict['case_id'] = self.case_id
        xml_dict['date_modified'] = self.date_modified.strftime("%Y-%m-%dT%H:%M:%S.000")
        xml_dict['patient_name'] = "%s, %s" % (self.last_name, self.first_name)
        xml_dict['patient_id'] = self.django_uuid
        xml_dict['pact_id'] = self.pact_id
        xml_dict['sex'] = self.gender
        xml_dict['arm'] = self.arm
        xml_dict['patient_note'] = html_escape(self.notes) if self.notes != None else ""
        xml_dict['dob'] = self.birthdate.strftime("%Y-%m-%d")
        xml_dict['pt_initials'] = "%s%s" % (self.first_name[0], self.last_name[0])
        xml_dict['hp'] = self.primary_hp
        xml_dict['phones'] = self.get_ghetto_phone_xml()
        xml_dict['addresses'] = self.get_ghetto_address_xml()

        #todo: this ought to also use a view to verify that the dots and progress notes are dynamically queried for last status.  This patient placeholder is for testing and legacy purposes

        #check the view if any exist.  if it's empty, check this property.

        xml_dict['last_dot'] = ''
        last_dots = get_db().view('pactcarehq/last_dots_form', key=self.pact_id, group=True).all()
        if len(last_dots) != 1:
            if self.last_dot != None:
                xml_dict['last_dot'] = self.last_dot
        else:
            last_dot = last_dots[0]['value']
            if len(last_dot) > 0:
                xml_dict['last_dot'] = last_dot[0][1] #array is [[doc_id, encounter_date_string], ...]


        #for last_note, check the view if any exist.  if it's null, check self property.
        xml_dict['last_note'] = ''
        last_notes = get_db().view('pactcarehq/last_progress_note', key=self.pact_id, group=True).all()
        if len(last_notes) != 1:
            if self.last_note != None:
                xml_dict['last_note'] = self.last_note
        else:
            last_note = last_notes[0]['value']
            if len(last_note) > 0:
                xml_dict['last_note'] = last_note[0][1] #array is [[doc_id, encounter_date_string], ...]

        #todo: check bloodwork view
        xml_dict['last_bw_xml'] = self.get_bloodwork_xml()



        if self.arm.lower() == 'dot':
            xml_dict['dot_schedule'] = self.get_ghetto_schedule_xml()
            xml_dict['regimens'] = self.get_ghetto_regimen_xml()
        else:
            xml_dict['dot_schedule'] = ''
            xml_dict['regimens'] = ''
        ret = ghetto_patient_xml % (xml_dict)
        return ret
コード例 #13
0
ファイル: mixins.py プロジェクト: dimagi/bhoma
 def copy(cls, instance):
     """
     Create a backup copy of a instance object, returning the id of the 
     newly created document
     """
     instance_json = instance.to_json()
     backup_id = uid.new()
     instance_json["_id"] = backup_id 
     instance_json.pop("_rev")
     get_db().save_doc(instance_json)
     return backup_id
コード例 #14
0
ファイル: migrator.py プロジェクト: ansarbek/commcare-hq
 def __init__(self, doc_types, source_db_name, target_db_name, slug):
     assert doc_types
     doc_types = list(doc_types)
     self.doc_types = doc_types + [doc_type + '-Deleted' for doc_type in doc_types]
     self.slug = slug
     self.source_db_name = source_db_name
     self.source_db = get_db(source_db_name)
     self.target_db_name = target_db_name
     self.target_db = get_db(target_db_name)
     # shared by the class
     self.instances[self.slug] = self
コード例 #15
0
ファイル: smspillow.py プロジェクト: NoahCarnahan/commcare-hq
 def add_case_data(self, doc_dict):
     reminder_id = doc_dict.get("reminder_id")
     reminder = get_db().get(reminder_id) if reminder_id else {}
     case_id = reminder.get('case_id')
     case = get_db().get(case_id) if case_id else {}
     doc_dict[TC_STUB + 'case_data'] = {
         "case_id": case_id,
         "case_type": case.get("type"),
         "response_state": case.get("response_state") # todo: need to get response_state at time of sms log
     }
     return doc_dict
コード例 #16
0
ファイル: test_all_docs.py プロジェクト: ansarbek/commcare-hq
 def setUpClass(cls):
     cls.main_db = get_db(None)
     cls.users_db = get_db('users')
     delete_all_docs_by_doc_type(cls.main_db, ('Application', 'CommCareUser'))
     delete_all_docs_by_doc_type(cls.users_db, ('Application', 'CommCareUser'))
     cls.domain = 'all-docs-domain'
     cls.main_db_doc = {'_id': 'main_db_doc', 'domain': cls.domain,
                        'doc_type': 'Application'}
     cls.users_db_doc = {'_id': 'users_db_doc', 'domain': cls.domain,
                         'doc_type': 'CommCareUser'}
     cls.main_db.save_doc(cls.main_db_doc)
     cls.users_db.save_doc(cls.users_db_doc)
コード例 #17
0
ファイル: views.py プロジェクト: kamilk161/commcare-hq
def submissions_errors(request, template="hqadmin/submissions_errors_report.html"):
    show_dates = "true"
    datespan = request.datespan
    domains = Domain.get_all()

    rows = []
    for domain in domains:
        key = ["active", domain.name]
        data = get_db().view('users/by_domain',
            startkey=key,
            endkey=key+[{}],
            reduce=True
        ).all()
        num_active_users = data[0].get('value', 0) if data else 0

        key = make_form_couch_key(domain.name)
        data = get_db().view('reports_forms/all_forms',
            startkey=key+[datespan.startdate_param_utc],
            endkey=key+[datespan.enddate_param_utc, {}],
            reduce=True
        ).all()
        num_forms_submitted = data[0].get('value', 0) if data else 0

        phonelogs = DeviceReportEntry.objects.filter(domain__exact=domain.name,
            date__range=[datespan.startdate_param_utc, datespan.enddate_param_utc])
        num_errors = phonelogs.filter(type__in=TAGS["error"]).count()
        num_warnings = phonelogs.filter(type__in=TAGS["warning"]).count()

        rows.append(dict(domain=domain.name,
                        active_users=num_active_users,
                        submissions=num_forms_submitted,
                        errors=num_errors,
                        warnings=num_warnings))

    context = get_hqadmin_base_context(request)
    context.update({
        "show_dates": show_dates,
        "datespan": datespan,
        "layout_flush_content": True,
        "rows": rows
    })

    headers = DataTablesHeader(
        DataTablesColumn("Domain"),
        DataTablesColumn("Active Users", sort_type=DTSortType.NUMERIC),
        DataTablesColumn("Forms Submitted", sort_type=DTSortType.NUMERIC),
        DataTablesColumn("Errors", sort_type=DTSortType.NUMERIC),
        DataTablesColumn("Warnings", sort_type=DTSortType.NUMERIC)
    )
    context["headers"] = headers
    context["aoColumns"] = headers.render_aoColumns

    return render(request, template, context)
コード例 #18
0
ファイル: test_pillows.py プロジェクト: jmaina/commcare-hq
 def setUp(self):
     try:
         get_db().delete_doc("INDICATOR_CONFIGURATION")
     except ResourceNotFound:
         pass
     get_db().save_doc(
         {
             "_id": "INDICATOR_CONFIGURATION",
             "namespaces": {INDICATOR_TEST_DOMAIN: [[INDICATOR_TEST_NAMESPACE, "INDICATOR TEST Namespace"]]},
         }
     )
     self.form_pillow = MVPFormIndicatorPillow()
     self.case_pillow = MVPCaseIndicatorPillow()
コード例 #19
0
ファイル: models.py プロジェクト: hashimoto-hb/commcare-hq
 def get_active_slugs(cls, domain, section):
     all_slugs = list()
     key = ["defaults domain slug", domain, section]
     domain_defaults = (
         get_db().view(cls.defaults_couch_view(), group=True, group_level=4, startkey=key, endkey=key + [{}]).all()
     )
     all_slugs.extend([item.get("key", [])[-1] for item in domain_defaults])
     key = ["defaults global slug", section]
     global_defaults = (
         get_db().view(cls.defaults_couch_view(), group=True, group_level=3, startkey=key, endkey=key + [{}]).all()
     )
     all_slugs.extend([item.get("key", [])[-1] for item in global_defaults])
     return list(set(all_slugs))
コード例 #20
0
ファイル: smspillow.py プロジェクト: NoahCarnahan/commcare-hq
 def add_session_data(self, doc_dict):
     session_id = doc_dict.get('xforms_session_couch_id')
     session = get_db().get(session_id) if session_id else {}
     reminder_id = session.get("reminder_id")
     reminder = get_db().get(reminder_id) if reminder_id else {}
     case_id = reminder.get('case_id')
     case = get_db().get(case_id) if case_id else {}
     xform_id = session.get('submission_id')
     doc_dict[TC_STUB + 'session_data'] = {
         "session_id": session_id,
         "case_type": case.get("type"),
         "response_state": get_db().get(xform_id).get('form', {}).get('response_state') if xform_id else None
     }
     return doc_dict
コード例 #21
0
ファイル: reports.py プロジェクト: kennknowles/phonelog
    def rows(self):
#        for user_type in [HQUserType.DEMO_USER, HQUserType.ADMIN]:
#            if self.user_filter[user_type].show\
#            and not HQUserType.human_readable[user_type] in self.usernames:
#                temp_user = TempCommCareUser(self.domain, HQUserType.human_readable[user_type], "unknownID")
#                self._users.append(temp_user)
        rows = []
        query_string = self.request.META['QUERY_STRING']
        child_report_url = DeviceLogDetailsReport.get_url(domain=self.domain)
        for user in self.users:
            key = [self.domain, "errors_only", user.get('raw_username')]
            data = get_db().view("phonelog/devicelog_data",
                    reduce=True,
                    startkey=key+[self.datespan.startdate_param_utc],
                    endkey=key+[self.datespan.enddate_param_utc]
                ).first()
            warning_count = 0
            error_count = 0
            if data:
                data = data.get('value', {})
                error_count = data.get('errors', 0)
                warning_count = data.get('warnings', 0)

            formatted_warning_count = '<span class="label label-warning">%d</span>' % warning_count if warning_count > 0\
                                        else '<span class="label">%d</span>' % warning_count
            formatted_error_count = '<span class="label label-important">%d</span>' % error_count if error_count > 0\
                                        else '<span class="label">%d</span>' % error_count

            from corehq.apps.reports.util import make_form_couch_key
            key = make_form_couch_key(self.domain, user_id=user.get('user_id'))
            data = get_db().view("reports_forms/all_forms",
                startkey=key + [self.datespan.startdate_param_utc],
                endkey=key + [self.datespan.enddate_param_utc, {}],
                reduce=True
            ).all()
            form_count = data[0]['value'] if data else 0
            username_formatted = '<a href="%(url)s?%(query_string)s%(error_slug)s=True&%(username_slug)s=%(raw_username)s">%(username)s</a>' % {
                "url": child_report_url,
                "error_slug": DeviceLogTagField.errors_only_slug,
                "username_slug": DeviceLogUsersField.slug,
                "username": user.get('username_in_report'),
                "raw_username": user.get('raw_username'),
                "query_string": "%s&" % query_string if query_string else ""
            }
            rows.append([self.table_cell(user.get('raw_username'), username_formatted),
                         self.table_cell(form_count),
                         self.table_cell(warning_count, formatted_warning_count),
                         self.table_cell(error_count, formatted_error_count)])
        return rows
コード例 #22
0
 def verified_numbered_users(owner_type, ids=None, check_filters=False):
     if not ids and not check_filters:
         data = get_db().view('sms/verified_number_by_domain',
             reduce=True,
             startkey=[self.domain, owner_type],
             endkey=[self.domain, owner_type, {}],
         ).one()
         return data["value"] if data else 0
     else:
         owners = get_db().view('sms/verified_number_by_domain',
             reduce=False,
             startkey=[self.domain, owner_type],
             endkey=[self.domain, owner_type, {}],
         ).all()
         return len(filter(lambda oid: oid in ids, [o["key"][2] for o in owners]))
コード例 #23
0
ファイル: punchcard.py プロジェクト: dimagi/bhoma
def get_users(clinic):
    view = get_db().view("reports/form_time_by_user", 
                         startkey=[clinic],
                         endkey=[clinic,{}],
                         group=True,
                         group_level=2,
                         reduce=True)
    res = []
    for row in view:
        try:
            user_obj = get_db().get(row["key"][1])
            user_obj["get_id"] = user_obj["_id"] # hack this on, otherwise django templates get mad
            res.append((user_obj, row["value"]))
        except ResourceNotFound, e:
            logging.error("No user with id (%s) found.  What the dilly?" % row["key"][1])
コード例 #24
0
ファイル: utils.py プロジェクト: kkrampa/commcare-hq
def set_domain_namespace_entry(domain, entry):
    with CriticalSection([INDICATOR_CONFIG_LOCK_KEY]):
        try:
            doc = get_db().open_doc(INDICATOR_CONFIG_DOC_ID)
        except ResourceNotFound:
            doc = {
                '_id': INDICATOR_CONFIG_DOC_ID,
            }

        if 'namespaces' not in doc:
            doc['namespaces'] = {}

        doc['namespaces'][domain] = entry
        get_db().save_doc(doc)
        get_indicator_config.clear()
コード例 #25
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
         )
     return get_db().view(self.couch_view,
         **view_kwargs
     ).all()
コード例 #26
0
ファイル: calculations.py プロジェクト: tsinkala/core-hq
def cases_in_last(domain, days):
    now = datetime.now()
    then = (now - timedelta(days=int(days))).strftime(DATE_FORMAT)
    now = now.strftime(DATE_FORMAT)

    row = get_db().view("hqcase/all_cases", startkey=[domain, {}, {}, then], endkey=[domain, {}, {}, now]).one()
    return {"value": row["value"] if row else 0}
コード例 #27
0
ファイル: test_cleanup.py プロジェクト: kkrampa/commcare-hq
    def testComputedRemoved(self):
        db = get_db()
        db.save_doc({
                '#export_tag': 'tag',
                'tag': 'computed-test',
                'p1': 'v1',
                'computed_': {
                    'ignore': 'this stuff'
                }
            },
            **get_safe_write_kwargs()
        )

        config = ExportConfiguration(db, ['computed-test'], cleanup_fn=None)
        schema = config.get_latest_schema()
        self.assertTrue('computed_' in schema)
        docs = list(config.get_docs())
        self.assertEqual(1, len(docs))
        self.assertTrue('computed_' in docs[0])

        # check that it works both explicitly and by default
        explicit_config = ExportConfiguration(db, ['computed-test'], cleanup_fn=clear_computed)
        default_config = ExportConfiguration(db, ['computed-test'])
        for config in (explicit_config, default_config):
            schema = config.get_latest_schema()
            self.assertFalse('computed_' in schema)
            docs = list(config.get_docs())
            self.assertEqual(1, len(docs))
            self.assertFalse('computed_' in docs[0])
コード例 #28
0
ファイル: test_cleanup.py プロジェクト: kkrampa/commcare-hq
    def testAttachmentsRemoved(self):
        db = get_db()
        res = db.save_doc({
            '#export_tag': 'tag',
            'tag': 'attachments-test',
            'p1': 'v1',
            },
            **get_safe_write_kwargs()
        )
        doc = db.get(res['id'])
        doc['_attachments'] = {
            "attach.txt": {
                "content_type": "text/plain",
                "data": "some content",
            },
        }
        db.save_doc(doc)

        config = ExportConfiguration(db, ['attachments-test'], cleanup_fn=None)
        schema = config.get_latest_schema()
        self.assertTrue('_attachments' in schema)
        docs = list(config.get_docs())
        self.assertEqual(1, len(docs))
        self.assertTrue('_attachments' in docs[0])

        # check that it works both explicitly and by default
        explicit_config = ExportConfiguration(db, ['attachments-test'], cleanup_fn=clear_attachments)
        default_config = ExportConfiguration(db, ['attachments-test'])
        for config in (explicit_config, default_config):
            schema = config.get_latest_schema()
            self.assertFalse('_attachments' in schema)
            docs = list(config.get_docs())
            self.assertEqual(1, len(docs))
            self.assertFalse('_attachments' in docs[0])
コード例 #29
0
ファイル: views.py プロジェクト: nikhilvarma22/commcare-hq
def commcare_version_report(request, template="hqadmin/commcare_version.html"):
    apps = get_db().view("app_manager/applications_brief").all()
    menu = CommCareBuildConfig.fetch().menu
    builds = [item.build.to_string() for item in menu]
    by_build = dict([(item.build.to_string(), {"label": item.label, "apps": []}) for item in menu])

    for app in apps:
        app = app["value"]
        app["id"] = app["_id"]
        if app.get("build_spec"):
            build_spec = BuildSpec.wrap(app["build_spec"])
            build = build_spec.to_string()
            if by_build.has_key(build):
                by_build[build]["apps"].append(app)
            else:
                by_build[build] = {"label": build_spec.get_label(), "apps": [app]}
                builds.append(build)

    tables = []
    for build in builds:
        by_build[build]["build"] = build
        tables.append(by_build[build])
    context = get_hqadmin_base_context(request)
    context.update({"tables": tables})
    context["hide_filters"] = True
    return render(request, template, context)
コード例 #30
0
ファイル: models.py プロジェクト: johan--/commcare-hq
 def search(cls, query, limit=10):
     results = get_db().search(cls.Config.search_view,
         q=query,
         limit=limit,
         #stale='ok',
     )
     return map(cls.get, [r['id'] for r in results])
コード例 #31
0
ファイル: pillow_reset.py プロジェクト: zbidi/commcare-hq
    def handle(self, *args, **options):
        if len(args) != 1:
            raise CommandError("Usage is ./manage.py pillow_reset [config_file]!")

        file_path = args[0]
        db = get_db()
        checkpoints = []
        with open(file_path) as f:
            config = PillowResetConfig.wrap(json.loads(f.read()))
            for pillow in config.pillows:
                checkpoint_doc_name = pillow.checkpoint.checkpoint_id
                try:
                    checkpoint_doc = db.get(checkpoint_doc_name)
                except ResourceNotFound:
                    print 'ERROR - checkpoint {} not found!'.format(checkpoint_doc_name)
                    continue

                def _fmt(seq_id):
                    if isinstance(seq_id, basestring) and len(seq_id) > 20:
                        return '{}...'.format(seq_id[:20])
                    else:
                        return seq_id

                print 'resetting {} from {} to {}...'.format(
                    checkpoint_doc_name,
                    _fmt(checkpoint_doc['seq']),
                    _fmt(config.seq),
                )
                # add metadata properties in case we need to revert this for any reason
                checkpoint_doc['reset_from'] = checkpoint_doc['seq']
                checkpoint_doc['reset_on'] = json_format_datetime(datetime.utcnow())
                checkpoint_doc['seq'] = config.seq
                checkpoints.append(checkpoint_doc)

        if raw_input('Commit the above resets to the database? (y/n) \n').lower() == 'y':
            db.bulk_save(checkpoints)
        else:
            print 'pillow checkpoints not saved.'
コード例 #32
0
ファイル: copy_domain.py プロジェクト: tsinkala/commcare-hq
    def copy_docs(self, sourcedb, domain, startkey, endkey, simulate, type=None, since=None, exclude_types=None):
        doc_ids = [result["id"] for result in sourcedb.view("domain/docs", startkey=startkey,
                                                            endkey=endkey, reduce=False)]

        total = len(doc_ids)
        count = 0
        targetdb = get_db()
        msg = "Found %s matching documents in domain: %s" % (total, domain)
        msg += " of type: %s" % (type) if type else ""
        msg += " since: %s" % (since) if since else ""
        print msg

        queue = Queue(150)
        for i in range(NUM_PROCESSES):
            Worker(queue, sourcedb, targetdb, exclude_types, total, simulate).start()

        for doc in iter_docs(sourcedb, doc_ids, chunksize=100):
            count += 1
            queue.put((doc, count))

        # shutdown workers
        for i in range(NUM_PROCESSES):
            queue.put(None)
コード例 #33
0
ファイル: views.py プロジェクト: kkaczmarczyk/commcare-hq
def quick_find(request):
    query = request.GET.get('q')
    redirect = request.GET.get('redirect') != 'false'
    if not query:
        return HttpResponseBadRequest('GET param "q" must be provided')

    def deal_with_couch_doc(doc):
        domain = doc.get('domain') or doc.get('domains', [None])[0]
        if request.couch_user.is_superuser or (
                domain and request.couch_user.is_domain_admin(domain)):
            doc_info = get_doc_info(doc, domain_hint=domain)
        else:
            raise Http404()
        if redirect and doc_info.link:
            messages.info(
                request,
                _("We've redirected you to the %s matching your query") %
                doc_info.type_display)
            return HttpResponseRedirect(doc_info.link)
        else:
            return json_response(doc_info)

    try:
        doc = get_db().get(query)
    except ResourceNotFound:
        pass
    else:
        return deal_with_couch_doc(doc)

    try:
        doc = Repeater.get_db().get(query)
    except ResourceNotFound:
        pass
    else:
        return deal_with_couch_doc(doc)

    raise Http404()
コード例 #34
0
def update_patients_with_followups(domain, patients, caseid_set, year, month):
    """
    Given a set of patients, update them with info from followup forms.
    """
    followup_keys = [[domain, x, year, month] for x in list(caseid_set)]
    followups = get_db().view('pathfinder/pathfinder_gov_followup_by_caseid',
                              keys=followup_keys,
                              include_docs=True).all()
    for f in followups:
        if not f['key'][1] in patients:
            continue
        p = patients[f['key'][1]]
        f = f['doc']['form']
        fp = f['patient']
        p['provider'] = f['meta']['userID']

        # The code below exists to hack around issues like people coming back to life after being dead,
        # multiple followups in a single month having different services, medication, and/or referral values,
        # and so on.

        if not p.has_key(
                'registration_and_followup_hiv'
        ) or p['registration_and_followup_hiv'] == 'continuing' or p[
                'registration_and_followup_hiv'] == '':  # don't allow a value of 'dead' to change
            if fp.has_key('reg_followup'):
                p['registration_and_followup_hiv'] = fp['reg_followup']
        if p.has_key('followup_this_month'):
            p['followup_this_month'] += 1
        else:
            p['followup_this_month'] = 1
        if "medication_given" in fp and fp['medication_given']:
            p['medication_given'] += fp['medication_given'] + " "
        if "services_given" in fp and fp['services_given']:
            p['services'] += fp['services_given'] + " "
        if "any_referral" in fp and fp['any_referral'] == "yes":
            p['referrals'] += fp['referrals'] + " "
        p.update(fp)
コード例 #35
0
 def add_session_data(self, doc_dict):
     session_id = doc_dict.get('xforms_session_couch_id')
     session = get_db().get(session_id) if session_id else {}
     reminder_id = session.get("reminder_id")
     reminder = get_db().get(reminder_id) if reminder_id else {}
     case_id = reminder.get('case_id')
     case = get_db().get(case_id) if case_id else {}
     xform_id = session.get('submission_id')
     doc_dict[TC_STUB + 'session_data'] = {
         "session_id":
         session_id,
         "case_type":
         case.get("type"),
         "response_state":
         get_db().get(xform_id).get('form', {}).get('response_state')
         if xform_id else None
     }
     return doc_dict
コード例 #36
0
    def handle(self, file_path, **options):
        db = get_db()
        checkpoints = []
        with open(file_path, encoding='utf-8') as f:
            config = PillowResetConfig.wrap(json.loads(f.read()))
            for pillow in config.pillows:
                checkpoint_doc_name = pillow.checkpoint.checkpoint_id
                try:
                    checkpoint_doc = db.get(checkpoint_doc_name)
                except ResourceNotFound:
                    print('ERROR - checkpoint {} not found!'.format(
                        checkpoint_doc_name))
                    continue

                def _fmt(seq_id):
                    if isinstance(seq_id, str) and len(seq_id) > 20:
                        return '{}...'.format(seq_id[:20])
                    else:
                        return seq_id

                print('resetting {} from {} to {}...'.format(
                    checkpoint_doc_name,
                    _fmt(checkpoint_doc['seq']),
                    _fmt(config.seq),
                ))
                # add metadata properties in case we need to revert this for any reason
                checkpoint_doc['reset_from'] = checkpoint_doc['seq']
                checkpoint_doc['reset_on'] = json_format_datetime(
                    datetime.utcnow())
                checkpoint_doc['seq'] = config.seq
                checkpoints.append(checkpoint_doc)

        if input('Commit the above resets to the database? (y/n) \n').lower(
        ) == 'y':
            db.bulk_save(checkpoints)
        else:
            print('pillow checkpoints not saved.')
コード例 #37
0
def get_data(domain, user=None, datespan=None):
    """
    Returns a data structure like:
    
    { <Form display name>:
         { <date>: { count: <count>, 
                     max: <time in ms>, 
                     min: <time in ms>,
                     sum: <time in ms> 
                   }
         }   
    }
    """
    if datespan is None:
        datespan = DateSpan.since(days=30, format="%Y-%m-%dT%H:%M:%S")
    
    all_data = defaultdict(lambda: defaultdict(lambda: 0))
    startkey = ["udx", domain, user, datespan.startdate_param] if user \
                else ["dx", domain, datespan.startdate_param]
    endkey = ["udx", domain, user, datespan.enddate_param] if user \
                else ["dx", domain, datespan.enddate_param]
    view = get_db().view("formtrends/form_duration_by_user", 
                         startkey=startkey,
                         endkey=endkey,
                         group=True,
                         reduce=True)
    for row in view:
        date = row["key"][-2]
        xmlns = row["key"][-1]
        form_name = xmlns_to_name(domain, xmlns, app_id=None)
        data = all_data[form_name]
        if not date in data:
            data[date] = defaultdict(lambda: 0)
        thisrow = row["value"]
        for key, val in thisrow.items():
            data[date][key] = data[date][key] + thisrow[key]
    return all_data
コード例 #38
0
ファイル: sync_domains.py プロジェクト: tsinkala/commcare-hq
        def sync_if_necessary(line):
            try:
                change = Change(line)
                # don't bother with deleted or old documents
                if change.deleted or not change.is_current(db):
                    return

                # get doc
                doc = get_db().get(change.id)

                # check if transforms, and if so, save to new domain/db
                transforms = global_config.get_transforms(doc)
                for transform in transforms:
                    global_config.save(transform)

                # update the checkpoint, somewhat arbitrarily
                global domainsync_counter
                domainsync_counter = domainsync_counter + 1
                if domainsync_counter % CHECKPOINT_FREQUENCY == 0:
                    Checkpoint.set_checkpoint(CHECKPOINT_ID, change.seq)

            except Exception, e:
                logging.exception("problem in domain sync for line: %s\n%s" %
                                  (line, e))
コード例 #39
0
        def row(rowname, workflow=None):
            additional_workflow_facets = {
                WORKFLOW_KEYWORD: ['xforms_session_couch_id'],
                WORKFLOW_REMINDER: ['reminder_id'],
            }
            additional_facets = additional_workflow_facets.get(workflow)
            facets = self.workflow_query(workflow, additional_facets)['facets']
            to_cases, to_users, outgoing, incoming = 0, 0, 0, 0

            for term in facets['couch_recipient_doc_type']['terms']:
                if term['term'] == 'commcarecase':
                    to_cases = term['count']
                elif term['term'] == 'commcareuser':
                    to_users = term['count']

            for term in facets['direction']['terms']:
                if term['term'] == 'o':
                    outgoing = term['count']
                elif term['term'] == 'i':
                    incoming = term['count']

            number = NA
            if workflow in additional_workflow_facets:
                number = len(
                    facets[additional_workflow_facets[workflow][0]]["terms"])
            elif workflow == WORKFLOW_BROADCAST:
                key = [self.domain, REMINDER_TYPE_ONE_TIME]
                data = get_db().view(
                    'reminders/handlers_by_reminder_type',
                    reduce=True,
                    startkey=key + [self.datespan.startdate_param_utc],
                    endkey=key + [self.datespan.enddate_param_utc],
                ).one()
                number = data["value"] if data else 0

            return [rowname, number, to_users, to_cases, incoming, outgoing]
コード例 #40
0
ファイル: views.py プロジェクト: kkaczmarczyk/commcare-hq
def system_info(request):
    environment = settings.SERVER_ENVIRONMENT

    context = get_hqadmin_base_context(request)
    context['couch_update'] = request.GET.get('couch_update', 5000)
    context['celery_update'] = request.GET.get('celery_update', 10000)
    context['db_update'] = request.GET.get('db_update', 30000)
    context['celery_flower_url'] = getattr(settings, 'CELERY_FLOWER_URL', None)

    # recent changes
    recent_changes = int(request.GET.get('changes', 50))
    context['recent_changes'] = get_recent_changes(get_db(), recent_changes)
    context['rabbitmq_url'] = get_rabbitmq_management_url()
    context['hide_filters'] = True
    context['current_system'] = socket.gethostname()
    context['deploy_history'] = HqDeploy.get_latest(environment, limit=5)

    context.update(check_redis())
    context.update(check_rabbitmq())
    context.update(check_celery_health())
    context.update(check_memcached())
    context.update(check_es_cluster_health())

    return render(request, "hqadmin/system_info.html", context)
コード例 #41
0
def pending_exams(request, domain, scanner_serial, **kwargs):
    """
    Return a list of exam id's that have not had either successful
    or failed image upload attempts.
    """

    if not scanner_serial:
        response_data = {}
        response_data['result'] = 'failed'
        response_data['message'] = 'Missing scanner serial'
        return HttpResponse(json.dumps(response_data), content_type="application/json")

    results = get_db().view(
        'uth/uth_lookup',
        startkey=[UTH_DOMAIN, scanner_serial],
        endkey=[UTH_DOMAIN, scanner_serial, {}],
    ).all()

    exam_ids = [r['key'][-1] for r in results]

    response_data = {}
    response_data['result'] = 'success'
    response_data['exam_ids'] = exam_ids
    return HttpResponse(json.dumps(response_data), content_type="application/json")
コード例 #42
0
ファイル: calculations.py プロジェクト: tsinkala/commcare-hq
def num_web_users(domain, *args):
    key = ["active", domain, 'WebUser']
    row = get_db().view('users/by_domain', startkey=key,
                        endkey=key + [{}]).one()
    return row["value"] if row else 0
コード例 #43
0
def _handle_user_form(request, domain, couch_user=None):
    context = {}
    if couch_user:
        create_user = False
    else:
        create_user = True
    can_change_admin_status = ((request.user.is_superuser or request.couch_user.can_edit_web_users(domain=domain))
        and request.couch_user.user_id != couch_user.user_id)

    if couch_user.is_commcare_user():
        role_choices = UserRole.commcareuser_role_choices(domain)
    else:
        role_choices = UserRole.role_choices(domain)

    results = get_db().view('languages/list', startkey=[domain], endkey=[domain, {}], group='true').all()
    language_choices = []

    if results:
        for result in results:
            lang_code = result['key'][1]
            label = result['key'][1]
            long_form = langcodes.get_name(lang_code)
            if long_form:
                label += " (" + langcodes.get_name(lang_code) + ")"
            language_choices.append((lang_code, label))
    else:
        language_choices = langcodes.get_all_langs_for_select()

    if request.method == "POST" and request.POST['form_type'] == "basic-info":
        if couch_user.is_commcare_user():
            form = UserForm(request.POST, role_choices=role_choices, language_choices=language_choices)
        else:
            form = WebUserForm(request.POST, role_choices=role_choices, language_choices=language_choices)
        if form.is_valid():
            if create_user:
                django_user = User()
                django_user.username = form.cleaned_data['email']
                django_user.save()
                couch_user = CouchUser.from_django_user(django_user)
            if couch_user.is_current_web_user(request) or couch_user.is_commcare_user():
                couch_user.first_name = form.cleaned_data['first_name']
                couch_user.last_name = form.cleaned_data['last_name']
                couch_user.email = form.cleaned_data['email']
                if not couch_user.is_commcare_user():
                    couch_user.email_opt_in = form.cleaned_data['email_opt_in']
                couch_user.language = form.cleaned_data['language']
            if can_change_admin_status:
                role = form.cleaned_data['role']
                if role:
                    couch_user.set_role(domain, role)
            couch_user.save()
            if request.couch_user.get_id == couch_user.get_id and couch_user.language:
                # update local language in the session
                request.session['django_language'] = couch_user.language

            messages.success(request, 'Changes saved for user "%s"' % couch_user.username)
    else:
        form = UserForm(role_choices=role_choices, language_choices=language_choices)
        if couch_user.is_commcare_user():
            form = UserForm(role_choices=role_choices, language_choices=language_choices)
        else:
            form = WebUserForm(role_choices=role_choices, language_choices=language_choices)

        if not create_user:
            form.initial['first_name'] = couch_user.first_name
            form.initial['last_name'] = couch_user.last_name
            form.initial['email'] = couch_user.email
            form.initial['email_opt_in'] = couch_user.email_opt_in
            form.initial['language'] = couch_user.language
            if can_change_admin_status:
                if couch_user.is_commcare_user():
                    role = couch_user.get_role(domain)
                    if role is None:
                        initial = "none"
                    else:
                        initial = role.get_qualified_id()
                    form.initial['role'] = initial
                else:
                    form.initial['role'] = couch_user.get_role(domain, include_teams=False).get_qualified_id() or ''

    if not can_change_admin_status:
        del form.fields['role']

    context.update({"form": form,
                    "current_users_page": couch_user.is_current_web_user(request)})
    return context
コード例 #44
0
    def setUpClass(cls):
        super(TestInputStockView, cls).setUpClass()
        cls.domain = initial_bootstrap(TEST_DOMAIN)
        db = get_db()
        if db.doc_exist(DOMAIN_MODULE_KEY):
            module_config = db.open_doc(DOMAIN_MODULE_KEY)
            module_map = module_config.get('module_map')
            if module_map:
                module_map[TEST_DOMAIN] = 'custom.ewsghana'
            else:
                module_config['module_map'][TEST_DOMAIN] = 'custom.ewsghana'
        else:
            module_config = db.save_doc(
                {
                    '_id': DOMAIN_MODULE_KEY,
                    'module_map': {
                        'ewsghana-test-input-stock': 'custom.ewsghana'
                    }
                }
            )
        db.save_doc(module_config)

        cls.setup_subscription(TEST_DOMAIN, SoftwarePlanEdition.ENTERPRISE)

        test_utils.prepare_commtrack_config(TEST_DOMAIN)
        test_utils.prepare_custom_fields(TEST_DOMAIN)
        test_utils.create_or_edit_roles(TEST_DOMAIN)
        test_utils.create_test_products(TEST_DOMAIN)
        test_utils.create_test_locations(TEST_DOMAIN)

        cls.test_facility3 = SQLLocation.objects.get(domain=TEST_DOMAIN, site_code='tsactive')
        cls.testregion2 = SQLLocation.objects.get(domain=TEST_DOMAIN, site_code='testregion2')
        cls.rsp = SQLLocation.objects.get(domain=TEST_DOMAIN, site_code='rsp')
        cls.test_district = SQLLocation.objects.get(domain=TEST_DOMAIN, site_code='testdistrict')

        cls.username1 = 'ews_user1'
        cls.password1 = 'dummy'
        cls.web_user1 = WebUser.create(TEST_DOMAIN, cls.username1, cls.password1)

        cls.web_user1.eula.signed = True
        cls.web_user1.save()

        cls.username2 = 'ews_user2'
        cls.password2 = 'dummy'
        cls.web_user2 = WebUser.create(TEST_DOMAIN, cls.username2, cls.password2)
        cls.web_user2.get_domain_membership(TEST_DOMAIN).location_id = cls.test_facility3.location_id

        cls.web_user2.eula.signed = True
        cls.web_user2.save()

        cls.username3 = 'ews_user3'
        cls.password3 = 'dummy'
        cls.web_user3 = WebUser.create(TEST_DOMAIN, cls.username3, cls.password3)
        cls.web_user3.get_domain_membership(TEST_DOMAIN).location_id = cls.testregion2.location_id

        cls.web_user3.eula.signed = True
        cls.web_user3.save()

        cls.username4 = 'ews_user4'
        cls.password4 = 'dummy'
        cls.web_user4 = WebUser.create(TEST_DOMAIN, cls.username4, cls.password4)
        cls.web_user4.get_domain_membership(TEST_DOMAIN).location_id = cls.rsp.location_id

        cls.web_user4.eula.signed = True
        cls.web_user4.save()

        cls.username5 = 'ews_user5'
        cls.password5 = 'dummy'
        cls.web_user5 = WebUser.create(TEST_DOMAIN, cls.username5, cls.password5, first_name='test',
                                       last_name='test2')
        cls.web_user5.set_default_phone_number('1111')
        cls.web_user5.save()
        domain_membership = cls.web_user5.get_domain_membership(TEST_DOMAIN)
        domain_membership.location_id = cls.test_district.location_id
        domain_membership.role_id = UserRole.get_read_only_role_by_domain(cls.domain.name).get_id

        cls.web_user5.eula.signed = True
        cls.web_user5.save()

        cls.username6 = 'ews_user6'
        cls.password6 = 'dummy'
        cls.web_user6 = WebUser.create(TEST_DOMAIN, cls.username6, cls.password6)
        domain_membership = cls.web_user6.get_domain_membership(TEST_DOMAIN)
        domain_membership.role_id = UserRole.get_read_only_role_by_domain(cls.domain.name).get_id

        cls.web_user6.eula.signed = True
        cls.web_user6.save()

        cls.admin_username = '******'
        cls.admin_password = '******'
        cls.admin = WebUser.create(TEST_DOMAIN, cls.admin_username, cls.admin_password)
        domain_membership = cls.admin.get_domain_membership(TEST_DOMAIN)
        domain_membership.role_id = UserRole.by_domain_and_name(cls.domain.name, 'Administrator')[0].get_id

        cls.admin.eula.signed = True
        cls.admin.save()

        EWSExtension.objects.create(
            user_id=cls.web_user6.get_id,
            domain=TEST_DOMAIN,
            location_id=cls.test_facility3.get_id
        )

        cls.ad = SQLProduct.objects.get(domain=TEST_DOMAIN, code='ad')
        cls.al = SQLProduct.objects.get(domain=TEST_DOMAIN, code='al')

        cls.backend, cls.mapping = setup_default_sms_test_backend()
        cls.client = Client()
コード例 #45
0
    def rows(self):
        rows = []
        if not self.selected_site_map:
            self._selected_site_map = self.site_map

        if self.request_params.get(
                SelectReferredInStatusField.slug) == 'referred':
            keys = self.generate_keys(["site referred_in"])
        else:
            keys = self.generate_keys(["site"])

        for key in keys:
            data = get_db().view(
                'hsph/data_summary_old',
                reduce=True,
                startkey=key + [self.datespan.startdate_param_utc],
                endkey=key + [self.datespan.enddate_param_utc]).all()

            for item in data:

                item = item['value']
                region, district, site = self.get_site_table_values(key[1:4])
                birth_events = item['totalBirthRegistrationEvents']
                referred_in_birth_events = item['totalReferredInBirths']
                row = [
                    region, district, site, birth_events,
                    referred_in_birth_events
                ]

                discharge_stats = item['atDischarge']
                on7days_stats = item['on7Days']
                discharge = []
                seven_days = []
                total = []

                stat_keys = [
                    'maternalDeaths', 'maternalNearMisses', 'stillBirthEvents',
                    'neonatalMortalityEvents'
                ]
                for stat in stat_keys:
                    discharge.append(
                        self.table_cell(
                            discharge_stats[stat],
                            '<span class="label">%d</span>' %
                            discharge_stats[stat]))
                    seven_days.append(
                        self.table_cell(
                            on7days_stats[stat],
                            '<span class="label label-info">%d</span>' %
                            on7days_stats[stat]))
                    total.append(
                        self.table_cell(
                            item[stat],
                            '<span class="label label-inverse">%d</span>' %
                            item[stat]))

                row.extend(discharge)
                row.extend(seven_days)
                row.extend(total)

                negative_outcomes = item['totalBirthRegistrationEvents'] - \
                                    item['positiveOutcomeEvents'] - \
                                    item['lostToFollowUp']

                row.extend([
                    item['positiveOutcomeEvents'], negative_outcomes,
                    item['lostToFollowUp']
                ])

                rows.append(row)
        return rows
コード例 #46
0
 def view_results(self, reduce=False, **kwargs):
     try:
         data = get_db().view(self.couch_view, reduce=reduce, **kwargs)
     except Exception:
         data = None
     return data
コード例 #47
0
 def keys(self):
     return [row['key'] for row in get_db().view("care_benin/danger_signs",
                                                    reduce=True, group=True,
                                                    group_level=2)]
コード例 #48
0
ファイル: data_summary.py プロジェクト: tsinkala/hsph-reports
    def get_values(cls, domain, daterange, site_ids=None, user_ids=None):
        """
        Gets reduced results per unique process_sbr_no for each key and sums
        them together, adding percentage occurences out of total_forms for all
        indicators.

        """

        site_ids = site_ids or []
        user_ids = user_ids or []
        startdate, enddate = daterange
        keys = []
        keys.extend([(["site", site_id, startdate], ["site", site_id, enddate])
                     for site_id in site_ids])
        keys.extend([(["user", user_id, startdate], ["user", user_id, enddate])
                     for user_id in user_ids])

        data_keys = [
            "total_forms", "pp1_observed", "pp1_maternal_temp",
            "pp1_maternal_bp", "pp1_partograph", "pp1_scc_used_pp1",
            "pp1_pp_1_birth_companion_present", "pp2_observed",
            "pp2_oxycotin_started", "pp2_soap", "pp2_water", "pp2_gloves_used",
            "pp2_scc_used_pp2", "pp2_pp3_birth_companion_present",
            "pp3_observed", "pp3_oxycotin", "pp3_baby_apneic",
            "pp3_baby_intervention", "pp3_pp_3_birth_companion_present",
            "pp4_observed", "pp4_baby_wt", "pp4_baby_temp",
            "pp4_breastfeeding", "pp4_scc_used_pp4",
            "pp4_pp_4_birth_companion_present", "medication_observed",
            "med_oxycotin_admin", "med_ab_mother", "med_mgsulp", "med_ab_baby",
            "med_art_mother", "med_art_baby", "pp2_soap_and_water"
        ]

        values = dict((k, 0) for k in data_keys)
        db = get_db()

        all_results = []

        for startkey, endkey in keys:
            results = db.view("hsph/fada_observations",
                              reduce=True,
                              group_level=5,
                              startkey=[domain] + startkey,
                              endkey=[domain] + endkey)

            all_results.extend(results)

        # "The same sbr no forms are definitely not going to come from
        # different users." -- Sheel
        seen_process_sbr_nos = set()
        for result in all_results:
            value = result['value']
            process_sbr_no = result['key'][4]

            if process_sbr_no not in seen_process_sbr_nos:
                if value['site_id'] in site_ids and value[
                        'user_id'] in user_ids:
                    for k, v in value.items():
                        if k not in ('site_id', 'user_id'):
                            values[k] += v
                seen_process_sbr_nos.add(process_sbr_no)

        for k, v in values.items():
            pp = ('medication' if k[:3] == 'med' else k[:3]) + "_observed"
            if pp in values:
                if values[pp]:
                    values[k + '_pct'] = round(float(v) * 100 / values[pp], 1)
                else:
                    values[k + '_pct'] = '---'

        # used by secondary outcome report
        if values['pp3_baby_apneic']:
            values['pp3_apneic_intervention_pct'] = round(
                100 * float(values['pp3_baby_intervention']) /
                values['pp3_baby_apneic'], 1)
        else:
            values['pp3_apneic_intervention_pct'] = '---'

        values['unique_sbrs'] = values['total_forms']

        return values
コード例 #49
0
    def report_context(self):
        # This map for this view emits twice, once with app_id and once with {}, letting you join across all app_ids.
        # However, we want to separate out by (app_id, xmlns) pair not just xmlns so we use [domain] to [domain, {}]
        forms = []
        unknown_forms = []
        for f in get_db().view('exports_forms/by_xmlns', startkey=[self.domain], endkey=[self.domain, {}], group=True):
            form = f['value']
            if form.get('app_deleted') and not form.get('submissions'):
                continue
            if 'app' in form:
                form['has_app'] = True
            else:
                app_id = f['key'][1] or ''
                form['app'] = {
                    'id': app_id
                }
                form['has_app'] = False
                form['show_xmlns'] = True
                unknown_forms.append(form)

            form['current_app'] = form.get('app')
            forms.append(form)

        if unknown_forms:
            apps = get_db().view('exports_forms/by_xmlns',
                startkey=['^Application', self.domain],
                endkey=['^Application', self.domain, {}],
                reduce=False,
            )
            possibilities = defaultdict(list)
            for app in apps:
                # index by xmlns
                x = app['value']
                x['has_app'] = True
                possibilities[app['key'][2]].append(x)

            class AppCache(dict):
                def __init__(self, domain):
                    super(AppCache, self).__init__()
                    self.domain = domain

                def __getitem__(self, item):
                    if not self.has_key(item):
                        try:
                            self[item] = get_app(app_id=item, domain=self.domain)
                        except Http404:
                            pass
                    return super(AppCache, self).__getitem__(item)

            app_cache = AppCache(self.domain)

            for form in unknown_forms:
                app = None
                if form['app']['id']:
                    try:
                        app = app_cache[form['app']['id']]
                        form['has_app'] = True
                    except KeyError:
                        form['app_does_not_exist'] = True
                        form['possibilities'] = possibilities[form['xmlns']]
                        if form['possibilities']:
                            form['duplicate'] = True
                    else:
                        if app.domain != self.domain:
                            logging.error("submission tagged with app from wrong domain: %s" % app.get_id)
                        else:
                            if app.copy_of:
                                try:
                                    app = app_cache[app.copy_of]
                                    form['app_copy'] = {'id': app.get_id, 'name': app.name}
                                except KeyError:
                                    form['app_copy'] = {'id': app.copy_of, 'name': '?'}
                            if app.is_deleted():
                                form['app_deleted'] = {'id': app.get_id}
                            try:
                                app_forms = app.get_xmlns_map()[form['xmlns']]
                            except AttributeError:
                                # it's a remote app
                                app_forms = None
                            if app_forms:
                                app_form = app_forms[0]
                                if app_form.doc_type == 'UserRegistrationForm':
                                    form['is_user_registration'] = True
                                else:
                                    app_module = app_form.get_module()
                                    form['module'] = app_module
                                    form['form'] = app_form
                                form['show_xmlns'] = False

                            if not form.get('app_copy') and not form.get('app_deleted'):
                                form['no_suggestions'] = True
                    if app:
                        form['app'] = {'id': app.get_id, 'name': app.name, 'langs': app.langs}

                else:
                    form['possibilities'] = possibilities[form['xmlns']]
                    if form['possibilities']:
                        form['duplicate'] = True
                    else:
                        form['no_suggestions'] = True

        forms = sorted(forms, key=lambda form:\
        (0 if not form.get('app_deleted') else 1,
         form['app']['name'],
         form['app']['id'],
         form.get('module', {'id': -1 if form.get('is_user_registration') else 1000})['id'], form.get('form', {'id': -1})['id']
            ) if form['has_app'] else\
        (2, form['xmlns'], form['app']['id'])
        )

        # if there is a custom group export defined grab it here
        groups = HQGroupExportConfiguration.by_domain(self.domain)
        context = super(ExcelExportReport, self).report_context
        context.update(
            forms=forms,
            edit=self.request.GET.get('edit') == 'true',
            group_exports=groups
        )
        return context
コード例 #50
0
def download_recent_changes(request):
    count = int(request.GET.get('changes', 10000))
    resp = HttpResponse(content_type='text/csv')
    resp['Content-Disposition'] = 'attachment; filename="recent_changes.csv"'
    download_changes(get_db(), count, resp)
    return resp
コード例 #51
0
ファイル: calculations.py プロジェクト: tsinkala/commcare-hq
def forms(domain, *args):
    key = make_form_couch_key(domain)
    row = get_db().view("reports_forms/all_forms",
                        startkey=key,
                        endkey=key + [{}]).one()
    return row["value"] if row else 0
コード例 #52
0
 def tearDownClass(cls):
     for doc in cls.docs:
         doc.delete()
     get_db(None).delete_docs(cls.legacy_sync_logs)
コード例 #53
0
 def setUpClass(cls):
     super(DBAccessorsTest, cls).setUpClass()
     cls.domain = 'domain-domain'
     cls.project = create_domain(cls.domain)
     cls.db = get_db()
コード例 #54
0
ファイル: calculations.py プロジェクト: tsinkala/commcare-hq
def num_mobile_users(domain, *args):
    row = get_db().view('users/by_domain',
                        startkey=[domain],
                        endkey=[domain, {}]).one()
    return row["value"] if row else 0
コード例 #55
0
ファイル: views.py プロジェクト: rodneylubwama/commcare-hq
 def save_doc(self):
     db = get_db()
     return db.save_doc(self.doc)
コード例 #56
0
ファイル: calculations.py プロジェクト: tsinkala/commcare-hq
def cases(domain, *args):
    row = get_db().view("hqcase/types_by_domain",
                        startkey=[domain],
                        endkey=[domain, {}]).one()
    return row["value"] if row else 0
コード例 #57
0
 def del_attachment():
     get_db(None).delete_attachment(legacy_sync_log._doc,
                                    attachment_name)
     del legacy_sync_log._attachments
コード例 #58
0
ファイル: views.py プロジェクト: tsinkala/commcare-hq
 def query(limit=100, userID=None, group=False, username__exclude=["demo_user", "admin"], **kwargs):
     if group:
         if userID is None:
             key = [domain]
         else:
             key = [domain, userID]
         subs = [dict(
             userID = r['key'][1],
             username = r['key'][2],
             deviceID = r['key'][3],
             submissions = r['value'],
         ) for r in get_db().view('cleanup/submissions',
             startkey = key,
             endkey   = key + [{}],
             group    = True,
         )]
         total = len(subs)
     else:
         if userID is None:
             key = make_form_couch_key(domain)
             subs = XFormInstance.view('reports_forms/all_forms',
                 startkey=key+[{}],
                 endkey=key,
                 reduce=False,
                 include_docs=True,
                 descending=True,
                 limit=limit
             )
             total = get_db().view('reports_forms/all_forms',
                 startkey=key,
                 endkey=key+[{}],
                 group_level=1
             ).one()
             total = total['value'] if total else 0
         else:
             key = make_form_couch_key(domain, user_id=userID)
             subs = XFormInstance.view('reports_forms/all_forms',
                 startkey=key+[{}],
                 endkey=key,
                 reduce=False,
                 include_docs=True,
                 descending=True,
                 limit=limit
             )
             total = get_db().view('reports_forms/all_forms',
                 startkey=key,
                 endkey=key+[{}],
                 group_level=2
             ).one()
             total = total['value'] if total else 0
         _subs = []
         for s in subs:
             try:
                 _subs.append(dict(
                     username = s['form']['meta']['username'],
                     userID = s['form']['meta']['userID'],
                     received_on = unicode(s['received_on']),
                     deviceID = s['form']['meta']['deviceID'],
                 ))
             except:
                 continue
         subs = _subs
     if username__exclude:
         username__exclude = set(username__exclude)
         subs = filter(lambda sub: sub['username'] not in username__exclude, subs)
     return json_response({
         "results": subs,
         "total": total,
     })
コード例 #59
0
    def rows(self):
        # ordered keys with default values
        keys = SortedDict([
            ('catiName', None),
            ('followedUp', 0),
            ('noFollowUpAfter6Days', 0),
            ('waitlisted', 0),
            ('transferredToTeamLeader', 0),
            ('timedOut', 0),
            ('workingDays', 0),
            ('followUpTime', 0),
            ('avgTimePerFollowUp', None),
        ])

        db = get_db()

        startdate = self.datespan.startdate_param_utc[:10]
        enddate = self.datespan.enddate_param_utc[:10]

        def get_form_data(user_id):
            row = db.view(
                'hsph/cati_performance',
                startkey=["followUpForm", self.domain, user_id, startdate],
                endkey=["followUpForm", self.domain, user_id, enddate],
                reduce=True,
                wrapper=lambda r: r['value']).first() or {}

            if row.get('followUpTime'):

                def format(seconds):
                    return time.strftime('%M:%S', time.gmtime(seconds))

                row['avgTimePerFollowUp'] = format(row['followUpTime'] //
                                                   row['followUpForms'])
                row['followUpTime'] = format(row['followUpTime'])

            row['workingDays'] = len(
                set(
                    db.view(
                        'hsph/cati_performance',
                        startkey=[
                            "submissionDay", self.domain, user_id, startdate
                        ],
                        endkey=[
                            "submissionDay", self.domain, user_id, enddate
                        ],
                        reduce=False,
                        wrapper=lambda r: r['value']['submissionDay']).all()))
            return row

        def get_case_data(group_id):
            row = db.view('hsph/cati_performance',
                          startkey=["all", self.domain, group_id, startdate],
                          endkey=["all", self.domain, group_id, enddate],
                          reduce=True,
                          wrapper=lambda r: r['value']).first() or {}

            # These queries can fail if startdate is less than N days before
            # enddate.  We just catch and supply a default value.
            extra_keys = [
                ('noFollowUpAfter6Days', 0, 13),
                ('timedOut', 0, 21),
            ]
            for key in extra_keys:
                key, default, days = key
                try:
                    row[key] = db.view(
                        'hsph/cati_performance',
                        startkey=[key, self.domain, group_id, startdate],
                        endkey=[
                            key, self.domain, group_id,
                            datestring_minus_days(enddate, days)
                        ],
                        reduce=True,
                        wrapper=lambda r: r['value'][key]).first()
                except restkit.errors.RequestFailed:
                    row[key] = default
            return row

        def sum_dicts(*args):
            res = {}
            for d in args:
                for k, v in d.items():
                    res[k] = res.get(k, 0) + (v or 0)
            return res

        rows = []
        cati_finder = CATIFinder(self.domain)

        for data in cati_finder.get_cati_users_data():
            user = data['user']
            row = get_form_data(user._id)
            row.update(
                sum_dicts(*[get_case_data(id) for id in data['group_ids']]))
            row['catiName'] = self.table_cell(user.raw_username,
                                              user.username_in_report)

            list_row = []
            for k, v in keys.items():
                val = row.get(k, v)
                if val is None:
                    val = '---'
                list_row.append(numeric_cell(val))

            rows.append(list_row)

        return rows
コード例 #60
0
 def rows(self):
     db = get_db()
     startkey, endkey = self.start_and_end_keys
     for row in self.row_list:
         value = row["view"].get_value([], startkey=startkey, endkey=endkey, couch_view=self.couch_view, db=db)
         yield [row["name"], fdd(value, value)]