def download_cases(request, domain): include_closed = json.loads(request.GET.get('include_closed', 'false')) format = Format.from_format(request.GET.get('format') or Format.XLS_2007) view_name = 'hqcase/all_cases' if include_closed else 'hqcase/open_cases' key = [domain, {}, {}] cases = CommCareCase.view(view_name, startkey=key, endkey=key + [{}], reduce=False, include_docs=True) # group, users = util.get_group_params(domain, **json_request(request.GET)) group = request.GET.get('group', None) user_filter, _ = FilterUsersField.get_user_filter(request) # todo deal with cached user dict here users = get_all_users_by_domain(domain, group=group, user_filter=user_filter) groups = Group.get_case_sharing_groups(domain) # if not group: # users.extend(CommCareUser.by_domain(domain, is_active=False)) workbook = WorkBook() export_cases_and_referrals(cases, workbook, users=users, groups=groups) export_users(users, workbook) response = HttpResponse(workbook.format(format.slug)) response['Content-Type'] = "%s" % format.mimetype response['Content-Disposition'] = "attachment; filename={domain}_data.{ext}".format(domain=domain, ext=format.extension) return response
def generate_case_export_payload(domain, include_closed, format, group, user_filter): """ Returns a FileWrapper object, which only the file backend in django-soil supports """ view_name = 'hqcase/all_cases' if include_closed else 'hqcase/open_cases' key = [domain, {}, {}] case_ids = CommCareCase.view(view_name, startkey=key, endkey=key + [{}], reduce=False, include_docs=False, wrapper=lambda r: r['id'] ) def stream_cases(all_case_ids): for case_ids in chunked(all_case_ids, 500): for case in CommCareCase.view('_all_docs', keys=case_ids, include_docs=True): yield case # todo deal with cached user dict here users = get_all_users_by_domain(domain, group=group, user_filter=user_filter) groups = Group.get_case_sharing_groups(domain) fd, path = tempfile.mkstemp() with os.fdopen(fd, 'wb') as file: workbook = WorkBook(file, format) export_cases_and_referrals(domain, stream_cases(case_ids), workbook, users=users, groups=groups) export_users(users, workbook) workbook.close() return FileWrapper(open(path))
def generate_case_export_payload(domain, include_closed, format, group, user_filter, process=None): """ Returns a FileWrapper object, which only the file backend in django-soil supports """ status = "all" if include_closed else "open" case_ids = CommCareCase.get_all_cases(domain, status=status, wrapper=lambda r: r["id"]) class stream_cases(object): def __init__(self, all_case_ids): self.all_case_ids = all_case_ids def __iter__(self): for case_ids in chunked(self.all_case_ids, 500): for case in wrapped_docs(CommCareCase, case_ids): yield case def __len__(self): return len(self.all_case_ids) # todo deal with cached user dict here users = get_all_users_by_domain(domain, group=group, user_filter=user_filter) groups = Group.get_case_sharing_groups(domain) fd, path = tempfile.mkstemp() with os.fdopen(fd, "wb") as file: workbook = WorkBook(file, format) export_cases_and_referrals( domain, stream_cases(case_ids), workbook, users=users, groups=groups, process=process ) export_users(users, workbook) workbook.close() return FileWrapper(open(path))
def generate_case_export_payload(domain, include_closed, format, group, user_filter, process=None): """ Returns a FileWrapper object, which only the file backend in django-soil supports """ view_name = 'hqcase/all_cases' if include_closed else 'hqcase/open_cases' key = [domain, {}, {}] case_ids = CommCareCase.view(view_name, startkey=key, endkey=key + [{}], reduce=False, include_docs=False, wrapper=lambda r: r['id']) class stream_cases(object): def __init__(self, all_case_ids): self.all_case_ids = all_case_ids def __iter__(self): for case_ids in chunked(self.all_case_ids, 500): for case in wrapped_docs(CommCareCase, case_ids): yield case def __len__(self): return len(self.all_case_ids) # todo deal with cached user dict here users = get_all_users_by_domain(domain, group=group, user_filter=user_filter) groups = Group.get_case_sharing_groups(domain) fd, path = tempfile.mkstemp() with os.fdopen(fd, 'wb') as file: workbook = WorkBook(file, format) export_cases_and_referrals(domain, stream_cases(case_ids), workbook, users=users, groups=groups, process=process) export_users(users, workbook) workbook.close() return FileWrapper(open(path))