本文整理汇总了Python中casexml.apps.case.models.CommCareCase.get_all_cases方法的典型用法代码示例。如果您正苦于以下问题:Python CommCareCase.get_all_cases方法的具体用法?Python CommCareCase.get_all_cases怎么用?Python CommCareCase.get_all_cases使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类casexml.apps.case.models.CommCareCase
的用法示例。
在下文中一共展示了CommCareCase.get_all_cases方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: open_cases_json
# 需要导入模块: from casexml.apps.case.models import CommCareCase [as 别名]
# 或者: from casexml.apps.case.models.CommCareCase import get_all_cases [as 别名]
def open_cases_json(request, domain):
delete_ids = json.loads(request.GET.get('delete_ids', 'false'))
cases = CommCareCase.get_all_cases(domain, status='open', include_docs=True)
user_id_to_type_to_cases = defaultdict(lambda:defaultdict(list))
for case in cases:
case_json = deepcopy(case.to_json())
user_id_to_type_to_cases[case.user_id][case.type].append(case_json)
del case_json['domain']
if delete_ids:
del case_json['_id']
del case_json['_rev']
del case_json['user_id']
# del case_json['type']
del case_json['doc_type']
case_json['actions'] = [action.action_type for action in case.actions]
case_json['referrals'] = [referral.type for referral in case.referrals]
usercases = [{
"username": user_id_to_username(user_id),
"cases": [{"type": type, "cases":cases} for (type, cases) in type_to_cases.items()]
} for (user_id, type_to_cases) in user_id_to_type_to_cases.items()]
usercases.sort(key=lambda x: x['username'])
return HttpResponse(json.dumps(usercases))
示例2: generate_case_export_payload
# 需要导入模块: from casexml.apps.case.models import CommCareCase [as 别名]
# 或者: from casexml.apps.case.models.CommCareCase import get_all_cases [as 别名]
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))
示例3: reassign_cases_to_correct_owner
# 需要导入模块: from casexml.apps.case.models import CommCareCase [as 别名]
# 或者: from casexml.apps.case.models.CommCareCase import get_all_cases [as 别名]
def reassign_cases_to_correct_owner(request, domain, template='cleanup/reassign_cases_to_correct_owner.html'):
log = {'unaffected': [], 'affected': [], 'unsure': []}
@memoized
def get_correct_group_id(user_id, group_id):
group_ids = [group.get_id for group in Group.by_user(user_id) if group.case_sharing]
if group_id in group_ids:
return group_id
else:
try:
g, = group_ids
return g
except ValueError:
# too many values to unpack
return None
@memoized
def get_meta(id):
try:
doc = get_db().get(id)
except (ResourceNotFound, AttributeError):
return {'name': None, 'doc_type': None}
return {
'name': {
'CommCareUser': lambda user: CommCareUser.wrap(user).raw_username,
'WebUser': lambda user: user['username'],
'Group': lambda group: group['name']
}.get(doc['doc_type'], lambda x: None)(doc),
'doc_type': doc['doc_type']
}
for case in CommCareCase.get_all_cases(domain, include_docs=True):
group_id = get_correct_group_id(case.user_id, case.owner_id)
case_data = {
'case': {'id': case.case_id, 'meta': {'name': case.name, 'doc_type': case.doc_type}, 'modified': case.modified_on},
'user': {'id': case.user_id, 'meta': get_meta(case.user_id)},
'owner': {'id': case.owner_id, 'meta': get_meta(case.owner_id)},
'suggested': {'id': group_id, 'meta': get_meta(group_id)},
}
if group_id:
if group_id != case.owner_id:
log['affected'].append(case_data)
# else:
# log['unaffected'].append(case_data)
# else:
# log['unsure'].append(case_data)
if request.GET.get('ajax'):
return json_response(log)
else:
return render(request, template, {
'domain': domain,
'results': log
})
示例4: obj_get_list
# 需要导入模块: from casexml.apps.case.models import CommCareCase [as 别名]
# 或者: from casexml.apps.case.models.CommCareCase import get_all_cases [as 别名]
def obj_get_list(self, bundle, **kwargs):
domain = kwargs['domain']
closed_only = {
'true': True,
'false': False,
'any': True
}[bundle.request.GET.get('closed', 'false')]
case_type = bundle.request.GET.get('case_type')
key = [domain]
if case_type:
key.append(case_type)
status = 'all' if closed_only else 'open'
cases = CommCareCase.get_all_cases(domain, case_type=case_type, status=status, include_docs=True)
return list(cases)
示例5: query
# 需要导入模块: from casexml.apps.case.models import CommCareCase [as 别名]
# 或者: from casexml.apps.case.models.CommCareCase import get_all_cases [as 别名]
def query(stale="ok", **kwargs):
subs = [dict(
userID = r['key'][1],
username = r['key'][2],
deviceID = r['key'][3],
submissions = r['value']['count'],
start = r['value']['start'].split('T')[0],
end = r['value']['end' ].split('T')[0]
) for r in get_db().view('cleanup/case_submissions',
startkey=[domain],
endkey=[domain, {}],
group=True,
# stale=stale
)]
subs.sort(key=lambda sub: (sub['userID'], sub['end']))
# Try and help identify lost devices
latest_start = defaultdict(lambda: None)
for sub in subs:
latest_start[sub['userID']] = max(sub['start'], latest_start[sub['userID']])
for sub in subs:
if sub['end'] < latest_start[sub['userID']]:
sub['old'] = True
else:
sub['old'] = False
# show the number of cases made by these xforms
# for sub in subs:
# cases = _get_cases(_get_submissions(domain, [sub]))
# sub['cases'] = len([None for case in cases if not case.closed])
open_cases = CommCareCase.get_all_cases(domain, status='open', include_docs=True)
xform_ids = [case.xform_ids[0] for case in open_cases]
case_count = defaultdict(int)
for xform_id in xform_ids:
xform = XFormInstance.get(xform_id)
meta = xform.form['meta']
case_count[(meta['userID'], meta['username'], meta['deviceID'])] += 1
for sub in subs:
sub['cases'] = case_count[(sub['userID'], sub['username'], sub['deviceID'])]
return json_response({
"results": subs,
"total": len(subs),
})
示例6: handle
# 需要导入模块: from casexml.apps.case.models import CommCareCase [as 别名]
# 或者: from casexml.apps.case.models.CommCareCase import get_all_cases [as 别名]
def handle(self, *args, **options):
if len(args) != 1:
raise CommandError("Usage: %s\n%s" % (self.args, self.help))
domain = args[0]
case_ids = CommCareCase.get_all_cases(domain, wrapper=lambda r: r["id"])
print "loading %s cases in %s" % (len(case_ids), domain)
def stream_cases(all_case_ids):
for case_ids in chunked(all_case_ids, 1000):
for case in wrapped_docs(CommCareCase, keys=case_ids):
# for case in CommCareCase.view('_all_docs', keys=case_ids, include_docs=True):
yield case
count = 0
for c in stream_cases(case_ids):
count += 1
print "read %s cases" % count
示例7: get_all
# 需要导入模块: from casexml.apps.case.models import CommCareCase [as 别名]
# 或者: from casexml.apps.case.models.CommCareCase import get_all_cases [as 别名]
def get_all(self):
view_results = CommCareCase.get_all_cases(self.domain, case_type=self.case_type, status=self.status)
ids = [res["id"] for res in view_results]
return self._case_results(ids)
示例8: _clearData
# 需要导入模块: from casexml.apps.case.models import CommCareCase [as 别名]
# 或者: from casexml.apps.case.models.CommCareCase import get_all_cases [as 别名]
def _clearData(self):
for case_type in self.case_types:
for subtype in [case_type, _child_case_type(case_type)]:
for c in CommCareCase.get_all_cases(self.domain, case_type=subtype, include_docs=True):
c.delete()