def report_context(self): context = super(CaseExportReport, self).report_context case_types = get_case_types_for_domain(self.domain) groups = HQGroupExportConfiguration.by_domain(self.domain) context.update( case_types=case_types, group_exports=[group.case_exports for group in groups if group.case_exports], report_slug=self.slug, ) context["case_format"] = self.request.GET.get("case_format") or "csv" return context
def report_context(self): context = super(CaseExportReport, self).report_context case_types = get_case_types_for_domain(self.domain) groups = HQGroupExportConfiguration.by_domain(self.domain) context.update( case_types=case_types, group_exports=[group.case_exports for group in groups if group.case_exports], report_slug=self.slug, ) context['case_format'] = self.request.GET.get('case_format') or 'csv' return context
def report_context(self): context = super(CaseExportReport, self).report_context cases = get_db().view("hqcase/types_by_domain", startkey=[self.domain], endkey=[self.domain, {}], reduce=True, group=True, group_level=2).all() groups = HQGroupExportConfiguration.by_domain(self.domain) context.update( case_types=[case['key'][1] for case in cases], group_exports=[group.case_exports for group in groups if group.case_exports], ) return context
def report_context(self): context = super(CaseExportReport, self).report_context cases = CommCareCase.get_db().view("hqcase/types_by_domain", startkey=[self.domain], endkey=[self.domain, {}], reduce=True, group=True, group_level=2).all() groups = HQGroupExportConfiguration.by_domain(self.domain) context.update( case_types=[case['key'][1] for case in cases], group_exports=[group.case_exports for group in groups if group.case_exports], report_slug=self.slug, ) context['case_format'] = self.request.GET.get('case_format') or 'csv' return context
def report_context(self): context = super(CaseExportReport, self).report_context cases = get_db().view("hqcase/types_by_domain", startkey=[self.domain], endkey=[self.domain, {}], reduce=True, group=True, group_level=2).all() groups = HQGroupExportConfiguration.by_domain(self.domain) context.update( case_types=[case['key'][1] for case in cases], group_exports=[ group.case_exports for group in groups if group.case_exports ], ) context['case_format'] = self.request.GET.get('case_format') or 'csv' return context
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 = [] startkey = [self.domain] db = Application.get_db() # the view emits from both forms and applications size_hash = self._get_domain_attachments_size() for f in db.view( "exports_forms/by_xmlns", startkey=startkey, endkey=startkey + [{}], group=True, stale=settings.COUCH_STALE_QUERY, ): 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") if "id" in form["app"]: key = (form["app"]["id"], form["xmlns"]) else: key = None if key in size_hash: form["size"] = size_hash[key] else: form["size"] = None forms.append(form) if unknown_forms: apps = db.view( "exports_forms/by_xmlns", startkey=["^Application", self.domain], endkey=["^Application", self.domain, {}], reduce=False, stale=settings.COUCH_STALE_QUERY, ) 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 key = (None, form["xmlns"]) form["size"] = size_hash.get(key, None) def _sortkey(form): app_id = form["app"]["id"] if form["has_app"]: order = 0 if not form.get("app_deleted") else 1 app_name = form["app"]["name"] module = form.get("module") if module: # module is sometimes wrapped json, sometimes a dict! module_id = module["id"] if "id" in module else module.id else: module_id = -1 if form.get("is_user_registration") else 1000 app_form = form.get("form") if app_form: # app_form is sometimes wrapped json, sometimes a dict! form_id = app_form["id"] if "id" in app_form else app_form.id else: form_id = -1 return (order, app_name, app_id, module_id, form_id) else: form_xmlns = form["xmlns"] return (2, form_xmlns, app_id) forms = sorted(forms, key=_sortkey) # if there is a custom group export defined grab it here groups = HQGroupExportConfiguration.by_domain(self.domain) context = super(ExcelExportReport, self).report_context # Check if any custom exports are in the size hash saved_exports_has_media = any((e.app_id, e.index[1]) in size_hash for e in context["saved_exports"]) context.update( forms=forms, edit=self.request.GET.get("edit") == "true", group_exports=[group.form_exports for group in groups if group.form_exports], group_export_cutoff=datetime.utcnow() - timedelta(days=settings.SAVED_EXPORT_ACCESS_CUTOFF), report_slug=self.slug, property_hash=self.properties(size_hash), exports_has_media=size_hash, saved_exports_has_media=saved_exports_has_media, ) return context
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
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 = [] startkey = [self.domain] db = Application.get_db() # the view emits from both forms and applications size_hash = self._get_domain_attachments_size() for f in db.view('exports_forms/by_xmlns', startkey=startkey, endkey=startkey + [{}], group=True, stale=settings.COUCH_STALE_QUERY): 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') if 'id' in form['app']: key = (form['app']['id'], form['xmlns']) else: key = None if key in size_hash: form['size'] = size_hash[key] else: form['size'] = None forms.append(form) if unknown_forms: apps = db.view('exports_forms/by_xmlns', startkey=['^Application', self.domain], endkey=['^Application', self.domain, {}], reduce=False, stale=settings.COUCH_STALE_QUERY, ) 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 key = (None, form['xmlns']) form['size'] = size_hash.get(key, None) def _sortkey(form): app_id = form['app']['id'] if form['has_app']: order = 0 if not form.get('app_deleted') else 1 app_name = form['app']['name'] module = form.get('module') if module: # module is sometimes wrapped json, sometimes a dict! module_id = module['id'] if 'id' in module else module.id else: module_id = -1 if form.get('is_user_registration') else 1000 app_form = form.get('form') if app_form: # app_form is sometimes wrapped json, sometimes a dict! form_id = app_form['id'] if 'id' in app_form else app_form.id else: form_id = -1 return (order, app_name, app_id, module_id, form_id) else: form_xmlns = form['xmlns'] return (2, form_xmlns, app_id) forms = sorted(forms, key=_sortkey) # 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=[group.form_exports for group in groups if group.form_exports], report_slug=self.slug, property_hash=self.properties(size_hash), ) return context
def emailed_export_groups(self): """The groups of saved exports by domain for daily emailed exports. """ return HQGroupExportConfiguration.by_domain(self.domain)
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("reports/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( "reports/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
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 = [] startkey = [self.domain] db = Application.get_db() # the view emits from both forms and applications size_hash = self._get_domain_attachments_size() for f in db.view('exports_forms/by_xmlns', startkey=startkey, endkey=startkey + [{}], group=True, stale=settings.COUCH_STALE_QUERY): 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') if 'id' in form['app']: key = (form['app']['id'], form['xmlns']) else: key = None if key in size_hash: form['size'] = size_hash[key] else: form['size'] = None forms.append(form) if unknown_forms: apps = db.view('exports_forms/by_xmlns', startkey=['^Application', self.domain], endkey=['^Application', self.domain, {}], reduce=False, stale=settings.COUCH_STALE_QUERY, ) 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 key = (None, form['xmlns']) form['size'] = size_hash.get(key, None) def _sortkey(form): app_id = form['app']['id'] if form['has_app']: order = 0 if not form.get('app_deleted') else 1 app_name = form['app']['name'] module = form.get('module') if module: # module is sometimes wrapped json, sometimes a dict! module_id = module['id'] if 'id' in module else module.id else: module_id = -1 if form.get('is_user_registration') else 1000 app_form = form.get('form') if app_form: # app_form is sometimes wrapped json, sometimes a dict! form_id = app_form['id'] if 'id' in app_form else app_form.id else: form_id = -1 return (order, app_name, app_id, module_id, form_id) else: form_xmlns = form['xmlns'] return (2, form_xmlns, app_id) forms = sorted(forms, key=_sortkey) # if there is a custom group export defined grab it here groups = HQGroupExportConfiguration.by_domain(self.domain) context = super(ExcelExportReport, self).report_context # Check if any custom exports are in the size hash saved_exports_has_media = any((e.app_id, e.index[1]) in size_hash for e in context['saved_exports']) context.update( forms=forms, edit=self.request.GET.get('edit') == 'true', group_exports=[group.form_exports for group in groups if group.form_exports], group_export_cutoff=datetime.utcnow() - timedelta(days=settings.SAVED_EXPORT_ACCESS_CUTOFF), report_slug=self.slug, property_hash=self.properties(size_hash), exports_has_media=size_hash, saved_exports_has_media=saved_exports_has_media ) return context