def prepare_case_json(planning_db): case_ids = planning_db.get_all_case_ids(valid_only=False) for case_json in iter_docs(CommCareCase.get_db(), case_ids): case = CommCareCase.wrap(case_json) if case.doc_type != 'CommCareCase': assert case.doc_type == 'CommCareCase-Deleted' continue
def _migrate_case(case_id): print('getting case %s' % case_id) case = CommCareCase.wrap( source_couch.get_db_for_class(CommCareCase).get(case_id)) original_domain = case.domain if domain is not None: case.domain = domain
def rows(self): audit_log_id_ref = {'id': 0} # To exclude audit logs, set `custom.openclinica.const.AUDIT_LOGS = False` query = self._build_query().case_type(CC_SUBJECT_CASE_TYPE) for result in query.scroll(): case = CommCareCase.wrap(result) if not self.is_subject_selected(case): continue
def handle(self, **options): with open('bihar_case_cleanup.csv', 'wb') as f: csv_file = csv.writer(f) csv_file.writerow(CaseRow.headers) blank_case_ids = get_case_ids_in_domain('care-bihar', type=('', None)) task_case_ids = get_case_ids_in_domain('care-bihar', type='task') case_ids = set(blank_case_ids) | set(task_case_ids) to_save = [] logger.info("Total cases to process: {}".format(len(case_ids))) for i, doc in enumerate(iter_docs(CommCareCase.get_db(), case_ids)): case = CommCareCase.wrap(doc) if case.type and case.type != "task": continue parent = None if case.indices: parent_id = case.indices[0].referenced_id try: parent = CommCareCase.get(parent_id) except ResourceNotFound: parent = MissingParent(get_id=parent_id, owner_id='Parent Missing') case_row = CaseRow(case, parent) if case.type != 'task': if case.user_id == MOTECH_ID: case_row.update_type('task')
def deidentify_case(doc): assert(doc.doc["doc_type"] == "CommCareCase") case = CommCareCase.wrap(doc.doc) case.name = arbitrary_fullname() for action in case.actions: deidentify_case_action(action) doc.doc = case._doc return doc
def test_process_doc_from_couch(self, datetime_mock): datetime_mock.utcnow.return_value = self.fake_time_now sample_doc, expected_indicators = get_sample_doc_and_indicators(self.fake_time_now) # make sure case is in DB case = CommCareCase.wrap(sample_doc) with drop_connected_signals(case_post_save): case.save()
def _get_data_case(self, params, filters): MAX_RESULTS = 200 # TODO vary by domain (cc-plus gets a higher limit?) # bleh _get = self.request.GET.copy() _get['iDisplayStart'] = '0' _get['iDisplayLength'] = str(MAX_RESULTS) self.request.GET = _get source = CaseListReport(self.request, domain=self.domain) total_count = source.es_results['hits']['total'] if total_count > MAX_RESULTS: # can't really think of a better way to return out-of-band # metadata from a generator yield {'_meta': { 'total_rows': total_count, 'capped_rows': MAX_RESULTS, }} # TODO ideally we'd want access to all the data shown on the # case detail report. certain case types can override this via # case.to_full_dict(). however, there is currently no efficient # way to call this over a large block of cases. so now we (via the # CaseListReport/DataSource) limit ourselves only to that which # can be queried in bulk for data in source.get_data(): case = CommCareCase.wrap(data['_case']).get_json() del data['_case'] data['num_forms'] = len(case['xform_ids']) standard_props = ( 'case_name', 'case_type', 'date_opened', 'external_id', 'owner_id', ) data.update(('prop_%s' % k, v) for k, v in case['properties'].iteritems() if k not in standard_props) GEO_DEFAULT = 'gps' # case property geo = None geo_directive = params['geo_fetch'].get(data['case_type'], GEO_DEFAULT) if geo_directive.startswith('link:'): # TODO use linked case pass elif geo_directive == '_random': # for testing -- just map the case to a random point import random import math geo = '%s %s' % (math.degrees(math.asin(random.uniform(-1, 1))), random.uniform(-180, 180)) elif geo_directive: # case property geo = data.get('prop_%s' % geo_directive) if geo: data['geo'] = geo yield data
def _save_form_and_case(test): form = XFormInstance.wrap(_get_doc_data('bug_form.json')) form.save() test.addCleanup(form.delete) case = CommCareCase.wrap(_get_doc_data('bug_case.json')) case.save() test.addCleanup(case.delete) return form, case
def populate(self, case_ids): def _iter_raw_cases(case_ids): if self.strip_history: for ids in chunked(case_ids, 100): for row in CommCareCase.get_db().view("case/get_lite", keys=ids, include_docs=False): yield row['value'] else: for raw_case in iter_docs(CommCareCase.get_db(), case_ids): yield raw_case for raw_case in _iter_raw_cases(case_ids): case = CommCareCase.wrap(raw_case) self.set(case._id, case)
def dynamic_case_properties(self): from casexml.apps.case.models import CommCareCase if self.case_json is not None: dynamic_props = self.case_json else: dynamic_props = CommCareCase.wrap(self._data).dynamic_case_properties() return dynamic_props
def cases(self): if "debug_case" in self.request.GET: case = CommCareCase.get(self.request.GET["debug_case"]) if case.domain != DOMAIN: raise Http404() return [case] query = ( case_es.CaseES() .domain(self.domain) .exclude_source() .opened_range(lte=self.datespan.enddate_utc) .case_type(self.default_case_type) ) query.index = "report_cases" if self.case_status == "open": query = query.filter( es_filters.OR(case_es.is_closed(False), case_es.closed_range(gte=self.datespan.enddate_utc)) ) elif self.case_status == "closed": query = query.filter(case_es.closed_range(lte=self.datespan.enddate_utc)) query = query.owner([user["doc_id"] for user in self.users_matching_filter]) result = query.run() return [CommCareCase.wrap(doc) for doc in iter_docs(CommCareCase.get_db(), result.doc_ids)]
def search(request, domain): """ Accepts search criteria as GET params, e.g. "https://www.commcarehq.org/a/domain/phone/search/?a=b&c=d" Returns results as a fixture with the same structure as a casedb instance. """ criteria = request.GET.dict() try: case_type = criteria.pop('case_type') except KeyError: return HttpResponse('Search request must specify case type', status=400) try: case_search_criteria = CaseSearchCriteria(domain, case_type, criteria) search_es = case_search_criteria.search_es except QueryMergeException as e: return _handle_query_merge_exception(request, e) try: hits = search_es.run().raw_hits except Exception as e: return _handle_es_exception( request, e, case_search_criteria.query_addition_debug_details) # Even if it's a SQL domain, we just need to render the hits as cases, so CommCareCase.wrap will be fine cases = [ CommCareCase.wrap(flatten_result(result, include_score=True)) for result in hits ] fixtures = CaseDBFixture(cases).fixture return HttpResponse(fixtures, content_type="text/xml; charset=utf-8")
def handle(self, *args, **options): if len(args) < 2: raise CommandError('Usage is copy_case, %s' % self.args) sourcedb = Database(args[0]) case_id = args[1] domain = args[2] if len(args) > 2 else None print 'getting case' case = CommCareCase.wrap(sourcedb.get(case_id)) if domain is not None: case.domain = domain case.save(force_update=True) print 'copying %s xforms' % len(case.xform_ids) def form_wrapper(row): doc = row['doc'] doc.pop('_attachments', None) return XFormInstance.wrap(doc) xforms = sourcedb.all_docs( keys=case.xform_ids, include_docs=True, wrapper=form_wrapper, ).all() for form in xforms: if domain is not None: form.domain = domain form.save(force_update=True) print 'saved %s' % form._id
def get_cases(case_ids, ordered=False): return [ CommCareCase.wrap(doc) for doc in iter_docs( CommCareCase.get_db(), case_ids ) ]
def _copy_unprocessed_cases(self): doc_types = ['CommCareCase-Deleted'] changes = _get_case_iterator(self.domain, doc_types=doc_types).iter_all_changes() for change in self._with_progress(doc_types, changes): couch_case = CommCareCase.wrap(change.get_document()) self.log_debug('Processing doc: {}({})'.format(couch_case['doc_type'], change.id)) sql_case = CommCareCaseSQL( case_id=couch_case.case_id, domain=self.domain, type=couch_case.type, name=couch_case.name, owner_id=couch_case.owner_id, opened_on=couch_case.opened_on, opened_by=couch_case.opened_by, modified_on=couch_case.modified_on, modified_by=couch_case.modified_by, server_modified_on=couch_case.server_modified_on, closed=couch_case.closed, closed_on=couch_case.closed_on, closed_by=couch_case.closed_by, deleted=True, deletion_id=couch_case.deletion_id, deleted_on=couch_case.deletion_date, external_id=couch_case.external_id, case_json=couch_case.dynamic_case_properties() ) _migrate_case_actions(couch_case, sql_case) _migrate_case_indices(couch_case, sql_case) _migrate_case_attachments(couch_case, sql_case) CaseAccessorSQL.save_case(sql_case)
def get_related_case_results(domain, cases, paths): """ Given a set of cases and a set of case property paths, fetches ES documents for all cases referenced by those paths. """ if not cases: return [] results_cache = {} for path in paths: current_cases = cases parts = path.split("/") for index, identifier in enumerate(parts): fragment = "/".join(parts[:index + 1]) if fragment in results_cache: current_cases = results_cache[fragment] else: indices = [ case.get_index(identifier) for case in current_cases ] related_case_ids = {i.referenced_id for i in indices if i} results = CaseSearchES().domain(domain).case_ids( related_case_ids).run().hits current_cases = [ CommCareCase.wrap(flatten_result(result)) for result in results ] results_cache[fragment] = current_cases
def get_all_rows(self): """ Rows to appear in the "Subjects" sheet of export_table(). CdiscOdmExportWriter will render this using the odm_export.xml template to combine subjects into a single ODM XML document. The values are also used to register new subjects if the web service is enabled. """ audit_log_id_ref = {'id': 0} # To exclude audit logs, set `custom.openclinica.const.AUDIT_LOGS = False` query = self._build_query().case_type(CC_SUBJECT_CASE_TYPE).start(0).size(SIZE_LIMIT) rows = [] for result in query.scroll(): case = CommCareCase.wrap(result) if not self.is_subject_selected(case): continue subject = Subject.wrap(case, audit_log_id_ref) row = [ 'SS_' + subject.subject_key, # OpenClinica prefixes subject key with "SS_" to make the OID subject.study_subject_id, subject.enrollment_date, subject.sex, subject.dob, subject.get_export_data(), ] rows.append(row) return rows
def get_all_rows(self): """ Rows to appear in the "Subjects" sheet of export_table(). CdiscOdmExportWriter will render this using the odm_export.xml template to combine subjects into a single ODM XML document. The values are also used to register new subjects if the web service is enabled. """ audit_log_id_ref = {'id': 0} # To exclude audit logs, set `custom.openclinica.const.AUDIT_LOGS = False` query = self._build_query().case_type(CC_SUBJECT_CASE_TYPE).start(0).size(SIZE_LIMIT) rows = [] for result in query.scroll(): case = CommCareCase.wrap(result) if not self.is_subject_selected(case): continue
def app_aware_search(request, domain, app_id): """ Accepts search criteria as GET params, e.g. "https://www.commcarehq.org/a/domain/phone/search/?a=b&c=d" Returns results as a fixture with the same structure as a casedb instance. """ criteria = request.GET.dict() try: case_type = criteria.pop('case_type') except KeyError: return HttpResponse('Search request must specify case type', status=400) try: case_search_criteria = CaseSearchCriteria(domain, case_type, criteria) except TooManyRelatedCasesError: return HttpResponse(_('Search has too many results. Please try a more specific search.'), status=400) search_es = case_search_criteria.search_es try: hits = search_es.run().raw_hits except Exception as e: notify_exception(request, str(e), details=dict( exception_type=type(e), )) return HttpResponse(status=500) # Even if it's a SQL domain, we just need to render the hits as cases, so CommCareCase.wrap will be fine cases = [CommCareCase.wrap(flatten_result(result, include_score=True)) for result in hits] if app_id: cases.extend(get_related_cases(domain, app_id, case_type, cases)) fixtures = CaseDBFixture(cases).fixture return HttpResponse(fixtures, content_type="text/xml; charset=utf-8")
def create_or_update_cases(self, case_structures, form_extras=None): def _get_case_block(substructure): return self.get_case_block(substructure.case_id, index=substructure.index, **substructure.attrs) def _get_case_blocks(substructure): blocks = [_get_case_block(substructure)] if substructure.walk_related: blocks += [ block for relationship in substructure.relationships for block in _get_case_blocks(relationship.related_structure) ] return blocks self.post_case_blocks( [ block for structure in case_structures for block in _get_case_blocks(structure) ], form_extras, ) return [ CommCareCase.wrap(doc) for doc in iter_docs(CommCareCase.get_db(), [ id for structure in case_structures for id in structure.walk_ids() ]) ]
def get_rows(self, paged=True): location_ids = self._get_voucher_location_ids() if location_ids: vouchers = [] for location_id_chunk in chunked(location_ids, ES_MAX_CLAUSE_COUNT): vouchers += [ CommCareCase.wrap(flatten_result(result)) for result in self._search_results(paged, location_id_chunk).raw_hits ] else: vouchers = [ CommCareCase.wrap(flatten_result(result)) for result in self._search_results(paged).raw_hits ] return [row for voucher in vouchers for row in self._make_rows(voucher)]
def test_get_related_case_results(self): # Note that cases must be defined before other cases can reference them cases = [ { '_id': 'c1', 'case_type': 'monster', 'description': 'grandparent of first person' }, { '_id': 'c2', 'case_type': 'monster', 'description': 'parent of first person', 'index': { 'parent': ('monster', 'c1') } }, { '_id': 'c3', 'case_type': 'monster', 'description': 'parent of host' }, { '_id': 'c4', 'case_type': 'monster', 'description': 'host of second person', 'index': { 'parent': ('monster', 'c3') } }, { '_id': 'c5', 'description': 'first person', 'index': { 'parent': ('monster', 'c2') } }, { '_id': 'c6', 'description': 'second person', 'index': { 'host': ('monster', 'c4') } }, ] self._bootstrap_cases_in_es_for_domain(self.domain, cases) hits = CaseSearchES().domain(self.domain).case_type( self.case_type).run().hits cases = [CommCareCase.wrap(flatten_result(result)) for result in hits] self.assertEqual({case.case_id for case in cases}, {'c5', 'c6'}) self._assert_related_case_ids(cases, set(), set()) self._assert_related_case_ids(cases, {"parent"}, {"c2"}) self._assert_related_case_ids(cases, {"host"}, {"c4"}) self._assert_related_case_ids(cases, {"parent/parent"}, {"c1"}) self._assert_related_case_ids(cases, {"host/parent"}, {"c3"}) self._assert_related_case_ids(cases, {"host", "parent"}, {"c2", "c4"}) self._assert_related_case_ids(cases, {"host", "parent/parent"}, {"c4", "c1"})
def search(request, domain): """ Accepts search criteria as GET params, e.g. "https://www.commcarehq.org/a/domain/phone/search/?a=b&c=d" Returns results as a fixture with the same structure as a casedb instance. """ criteria = request.GET.dict() try: case_type = criteria.pop('case_type') except KeyError: return HttpResponse('Search request must specify case type', status=400) try: include_closed = criteria.pop('include_closed') except KeyError: include_closed = False search_es = (CaseSearchES().domain(domain).case_type(case_type).size( CASE_SEARCH_MAX_RESULTS)) if include_closed != 'True': search_es = search_es.is_closed(False) try: config = CaseSearchConfig.objects.get(domain=domain) except CaseSearchConfig.DoesNotExist as e: from corehq.util.soft_assert import soft_assert _soft_assert = soft_assert(to="{}@{}.com".format('frener', 'dimagi'), notify_admins=False, send_to_ops=False) _soft_assert( False, u"Someone in domain: {} tried accessing case search without a config" .format(domain), e) config = CaseSearchConfig(domain=domain) query_addition_id = criteria.pop(SEARCH_QUERY_ADDITION_KEY, None) fuzzies = config.config.get_fuzzy_properties_for_case_type(case_type) for key, value in criteria.items(): search_es = search_es.case_property_query(key, value, fuzzy=(key in fuzzies)) query_addition_debug_details = {} try: search_es = _add_case_search_addition(request, domain, search_es, query_addition_id, query_addition_debug_details) except QueryMergeException as e: return _handle_query_merge_exception(request, e) try: results = search_es.values() except Exception as e: return _handle_es_exception(request, e, query_addition_debug_details) # Even if it's a SQL domain, we just need to render the results as cases, so CommCareCase.wrap will be fine cases = [CommCareCase.wrap(flatten_result(result)) for result in results] fixtures = CaseDBFixture(cases).fixture return HttpResponse(fixtures, content_type="text/xml")
def _actual_owned_cases(self): def _case_domain_match(case): return not self.domain or self.domain == case.get('domain') return [ CommCareCase.wrap(result['value']) for result in self._view_results() if _case_domain_match(result['value']) ]
def _get_assigned_cases(checkin_case): query = (CaseSearchES().domain(checkin_case.domain).filter( filters.OR(case_type("patient"), case_type("contact"))).case_property_query( "assigned_to_primary_checkin_case_id", checkin_case.case_id)) return [CommCareCase.wrap(flatten_result(hit)) for hit in query.run().hits]
def iter_cases(case_ids, strip_history=False, wrap=True): from casexml.apps.case.models import CommCareCase if not strip_history: for doc in iter_docs(CommCareCase.get_db(), case_ids): yield CommCareCase.wrap(doc) if wrap else doc else: for case in CommCareCase.bulk_get_lite(case_ids, wrap=wrap): yield case
def get_indexed_cases(domain, case_ids): """ Given a base list of cases, gets all wrapped cases that they reference (parent cases). """ from casexml.apps.case.models import CommCareCase return [CommCareCase.wrap(doc) for doc in iter_docs(CommCareCase.get_db(), get_indexed_case_ids(domain, case_ids))]
def _migrate_case(case_id): print 'getting case %s' % case_id case = CommCareCase.wrap(source_couch.get_db_for_class(CommCareCase).get(case_id)) original_domain = case.domain if domain is not None: case.domain = domain case.save(force_update=True) return case, original_domain
def get_cases(case_ids, ordered=False, prefetched_indices=None): # prefetched_indices is ignored sinces cases already have them return [ CommCareCase.wrap(doc) for doc in iter_docs( CommCareCase.get_db(), case_ids ) ]
def get_rows(self, paged=True): vouchers = [ CommCareCase.wrap(flatten_result(result)) for result in self._search_results(paged).raw_hits ] return [ row for voucher in vouchers for row in self._make_rows(voucher) ]
def iter_cases(self, ids): database = CommCareCase.get_db() if not self.strip_history: for doc in iter_docs(database, ids): yield CommCareCase.wrap(doc) else: for doc_ids in chunked(ids, 100): for case in CommCareCase.bulk_get_lite(doc_ids): yield case
def cases(self): _assert = soft_assert('@'.join(['droberts', 'dimagi.com'])) _assert(False, "I'm surprised GroupReferenceMixIn ever gets called!") case_ids = get_case_ids_in_domain_by_owner( self.domain, owner_id__in=self.all_owner_ids, closed=False) # really inefficient, but can't find where it's called # and this is what it was doing before return [CommCareCase.wrap(doc) for doc in iter_docs(CommCareCase.get_db(), case_ids)]
def setUp(self): self.case = CommCareCase.wrap({ "domain": DOMAIN, "case_id": "123456", "type": "person", "name": "Eric Idle", "external_id": "94d60c79-59b5-4a2c-90a5-325d6e32b3db", }) self.case.save()
def gen_children_only_ours(domain): """ Returns a list of child_gmp cases where external_id is not set """ result = (CaseES().domain(domain).case_type(CASE_TYPE).empty( 'external_id').run()) if result.total: for doc in result.hits: yield CommCareCase.wrap(doc)
def filter_cases(request, domain, app_id, module_id): app = Application.get(app_id) module = app.get_module(module_id) delegation = request.GET.get('task-list') == 'true' auth_cookie = request.COOKIES.get('sessionid') suite_gen = SuiteGenerator(app) xpath = suite_gen.get_filter_xpath(module, delegation=delegation) extra_instances = [{'id': inst.id, 'src': inst.src} for inst in suite_gen.get_instances_for_module(module, additional_xpaths=[xpath])] # touchforms doesn't like this to be escaped xpath = HTMLParser.HTMLParser().unescape(xpath) if delegation: case_type = DELEGATION_STUB_CASE_TYPE else: case_type = module.case_type if xpath: # if we need to do a custom filter, send it to touchforms for processing additional_filters = { "properties/case_type": case_type, "footprint": True } helper = SessionDataHelper(domain, request.couch_user) result = helper.filter_cases(xpath, additional_filters, DjangoAuth(auth_cookie), extra_instances=extra_instances) if result.get('status', None) == 'error': return HttpResponseServerError( result.get("message", _("Something went wrong filtering your cases."))) case_ids = result.get("cases", []) else: # otherwise just use our built in api with the defaults case_ids = [res.id for res in get_filtered_cases( domain, status=CASE_STATUS_OPEN, case_type=case_type, user_id=request.couch_user._id, ids_only=True )] cases = [CommCareCase.wrap(doc) for doc in iter_docs(CommCareCase.get_db(), case_ids)] # refilter these because we might have accidentally included footprint cases # in the results from touchforms. this is a little hacky but the easiest # (quick) workaround. should be revisted when we optimize the case list. cases = filter(lambda c: c.type == case_type, cases) cases = [c.get_json(lite=True) for c in cases if c] parents = [] if delegation: for case in cases: parent_id = case['indices']['parent']['case_id'] parents.append(CommCareCase.get(parent_id)) return json_response({ 'cases': cases, 'parents': parents }) else: return json_response(cases)
def get_study_subject_cases(self): cases = (CommCareCase.wrap(res['_source']) for res in self.es_results['hits'].get('hits', [])) for case in cases: if case.type != CC_SUBJECT_CASE_TYPE: # Skip cases that are not subjects. continue if not (hasattr(case, CC_SUBJECT_KEY) and hasattr(case, CC_STUDY_SUBJECT_ID)): # Skip subjects that have not been selected for the study continue yield case
def get_cases(self, limit=None, skip=None): case_ids = self.cases if skip is not None: case_ids = case_ids[skip:] if limit is not None: case_ids = case_ids[:limit] for case_doc in iter_docs(CommCareCase.get_db(), case_ids): # don't let CommCareCase-Deleted get through if case_doc['doc_type'] == 'CommCareCase': yield CommCareCase.wrap(case_doc)
def filter_cases(request, domain, app_id, module_id, parent_id=None): app = Application.get(app_id) module = app.get_module(module_id) auth_cookie = request.COOKIES.get('sessionid') suite_gen = SuiteGenerator(app) xpath = SuiteGenerator.get_filter_xpath(module) extra_instances = [{'id': inst.id, 'src': inst.src} for inst in suite_gen.get_instances_for_module(module, additional_xpaths=[xpath])] # touchforms doesn't like this to be escaped xpath = HTMLParser.HTMLParser().unescape(xpath) case_type = module.case_type if xpath: # if we need to do a custom filter, send it to touchforms for processing additional_filters = { "properties/case_type": case_type, "footprint": True } helper = SessionDataHelper(domain, request.couch_user) result = helper.filter_cases(xpath, additional_filters, DjangoAuth(auth_cookie), extra_instances=extra_instances) if result.get('status', None) == 'error': code = result.get('code', 500) message = result.get('message', _("Something went wrong filtering your cases.")) if code == 500: notify_exception(None, message=message) return json_response(message, status_code=code) case_ids = result.get("cases", []) else: # otherwise just use our built in api with the defaults case_ids = [res.id for res in get_filtered_cases( domain, status=CASE_STATUS_OPEN, case_type=case_type, user_id=request.couch_user._id, footprint=True, ids_only=True, )] cases = [CommCareCase.wrap(doc) for doc in iter_docs(CommCareCase.get_db(), case_ids)] if parent_id: cases = filter(lambda c: c.parent and c.parent.case_id == parent_id, cases) # refilter these because we might have accidentally included footprint cases # in the results from touchforms. this is a little hacky but the easiest # (quick) workaround. should be revisted when we optimize the case list. cases = filter(lambda c: c.type == case_type, cases) cases = [c.get_json(lite=True) for c in cases if c] return json_response(cases)
def get_subject_cases(self): cases = (CommCareCase.wrap(res['_source']) for res in self.es_results['hits'].get('hits', [])) for case in cases: if case.type != CC_SUBJECT_CASE_TYPE: # Skip cases that are not subjects. TODO: Remove case type selection from UI continue if not (hasattr(case, CC_SUBJECT_KEY) and hasattr(case, CC_STUDY_SUBJECT_ID)): raise OpenClinicaIntegrationError( 'Mandatory property "{}" or "{}" missing from subject "{}"'.format( CC_SUBJECT_KEY, CC_STUDY_SUBJECT_ID, case.name)) yield case
def get_app_info(self, data): app = None app_link = "" if "doc" in data: app = CommCareCase.wrap(data["doc"]) elif "id" in data: app = CommCareCase.get(data["id"]) if app: app_link = '<a href="%s">%s</a>' %\ (reverse('project_info', args=[app.domain]), app.domain) return app, app_link
def _fetch_missing_cases_and_wrap(self, casedoc_list): cases = [] to_fetch = [] for doc in casedoc_list: if doc['doc_type'] == 'CommCareCase': cases.append(CommCareCase.wrap(doc)) else: to_fetch.append(doc['_id']) cases.extend(CommCareCase.bulk_get_lite(to_fetch, wrap=True, chunksize=self.chunksize)) return cases
def es_filter_cases(domain, filters=None): """ Filter cases using elastic search (Domain, Filters?) -> [CommCareCase] """ q = ElasticCaseQuery(domain, filters) meta = ES_META['cases'] res = get_es_new().search(meta.index, body=q.get_query()) # this is ugly, but for consistency / ease of deployment just # use this to return everything in the expected format for now return [CommCareCase.wrap(r["_source"]) for r in res['hits']['hits'] if r["_source"]]
def _get_case_info(self, data): case = None case_link = "" if "doc" in data: case = CommCareCase.wrap(data["doc"]) elif "id" in data: case = CommCareCase.get(data["id"]) if case: case_link = '<a href="%s">%s</a>' %\ (reverse('case_details', args=[self.domain, case._id]), case.name) return case, case_link
def es_filter_cases(domain, filters=None): """ Filter cases using elastic search """ q = ElasticCaseQuery(domain, filters) res = get_es().get('hqcases/_search', data=q.get_query()) # this is ugly, but for consistency / ease of deployment just # use this to return everything in the expected format for now return [CommCareCase.wrap(r["_source"]).get_json() for r in res['hits']['hits'] if r["_source"]]
def gen_children_only_ours(domain): """ Returns a list of child_gmp cases where external_id is not set """ result = (CaseES() .domain(domain) .case_type(CASE_TYPE) .empty('external_id') .run()) if result.total: for doc in result.hits: yield CommCareCase.wrap(doc)
def get_case(self, row): if "doc" in row: case = CommCareCase.wrap(row["doc"]) elif "id" in row: case = CommCareCase.get(row["id"]) else: raise ValueError("Can't construct case object from row result %s" % row) if case.domain != self.domain: raise Exception("case.domain != self.domain; %r and %r, respectively" % (case.domain, self.domain)) return case
def transform_loadtest_update(update, factor): """ Returns a new CaseSyncUpdate object (from an existing one) with all the case IDs and names mapped to have the factor appended. """ def _map_id(id, count): return u'{}-{}'.format(id, count) case = CommCareCase.wrap(deepcopy(update.case._doc)) case._id = _map_id(case._id, factor) for index in case.indices: index.referenced_id = _map_id(index.referenced_id, factor) case.name = u'{} ({})'.format(case.name, factor) return CaseSyncUpdate(case, update.sync_token, required_updates=update.required_updates)
def _compare_cases(self, actual_json, expected_json, msg): expected_json.update({ 'domain': self.domain, '_rev': actual_json['_rev'], 'initial_processing_complete': True, '#export_tag': actual_json['#export_tag'], 'server_modified_on': actual_json['server_modified_on'] }) for expected_action, actual_action in zip(expected_json['actions'], actual_json['actions']): expected_action['server_date'] = actual_action['server_date'] expected_json = CommCareCase.wrap(expected_json).to_json() self.assertDictEqual(actual_json, expected_json, msg)
def populate(self, case_ids): def _iter_raw_cases(case_ids): if self.strip_history: for ids in chunked(case_ids, 100): for row in CommCareCase.get_db().view("case/get_lite", keys=ids, include_docs=False): yield row["value"] else: for raw_case in iter_docs(CommCareCase.get_db(), case_ids): yield raw_case for raw_case in _iter_raw_cases(case_ids): case = CommCareCase.wrap(raw_case) self.set(case._id, case)