def get_document(self, doc_id): try: doc = self.es_interface.get_doc(self.es_alias, '_all', doc_id) except NotFoundError: raise object_does_not_exist(self.doc_type, doc_id) if doc.get('domain') != self.domain: raise object_does_not_exist(self.doc_type, doc_id) return self.model(doc) if self.model else doc
def get_document(self, doc_id): try: result = self.es.get(self.index, doc_id) except NotFoundError: raise object_does_not_exist(self.doc_type, doc_id) doc = result['_source'] if doc.get('domain') != self.domain: raise object_does_not_exist(self.doc_type, doc_id) return self.model(doc) if self.model else doc
def get_document(self, doc_id): try: doc = self.docs[doc_id] except KeyError: raise object_does_not_exist('document', doc_id) if self.wrapper: return self.wrapper(doc) return doc
def obj_get(self, bundle, **kwargs): instance_id = kwargs['pk'] try: form = FormAccessors(kwargs['domain']).get_form(instance_id) es_form = form_to_es_form(form) if es_form: return es_form else: raise XFormNotFound except XFormNotFound: raise object_does_not_exist("XFormInstance", instance_id)
def obj_get(self, bundle, **kwargs): domain = kwargs['domain'] doc_id = kwargs['pk'] doc_type = 'XFormInstance' # Logic borrowed from util.get_object_or_not_exist try: doc = couchforms.fetch_and_wrap_form(doc_id) if doc and doc.domain == domain: return doc except ResourceNotFound: pass # covered by the below except AttributeError: # there's a weird edge case if you reference a form with a case id # that explodes on the "version" property. might as well swallow that # too. pass raise object_does_not_exist(doc_type, doc_id)
class CommCareCaseResource(SimpleSortableResourceMixin, v0_3.CommCareCaseResource, DomainSpecificResourceMixin): xforms_by_name = UseIfRequested(ToManyListDictField( 'corehq.apps.api.resources.v0_4.XFormInstanceResource', attribute='xforms_by_name' )) xforms_by_xmlns = UseIfRequested(ToManyListDictField( 'corehq.apps.api.resources.v0_4.XFormInstanceResource', attribute='xforms_by_xmlns' )) child_cases = UseIfRequested( ToManyDictField( 'corehq.apps.api.resources.v0_4.CommCareCaseResource', attribute='child_cases' ) ) parent_cases = UseIfRequested( ToManyDictField( 'corehq.apps.api.resources.v0_4.CommCareCaseResource', attribute='parent_cases' ) ) domain = fields.CharField(attribute='domain') # Fields that v0.2 assumed were pre-transformed but we are now operating on straight CommCareCase objects again date_modified = fields.CharField(attribute='modified_on', default="1900-01-01") server_date_modified = fields.CharField(attribute='server_modified_on', default="1900-01-01") server_date_opened = fields.CharField(attribute='server_opened_on', default="1900-01-01") def obj_get(self, bundle, **kwargs): case_id = kwargs['pk'] try: case = CaseAccessors(kwargs['domain']).get_case(case_id) return case_to_es_case(case) except CaseNotFound: raise object_does_not_exist("CommCareCase", case_id)
def get_location_or_not_exist(location_id, domain): try: return SQLLocation.objects.get(location_id=location_id, domain=domain) except SQLLocation.DoesNotExist: raise object_does_not_exist('Location', location_id)
def obj_get(self, bundle, **kwargs): case_id = kwargs['pk'] try: return CaseAccessors(kwargs['domain']).get_case(case_id) except CaseNotFound: raise object_does_not_exist("CommCareCase", case_id)
def obj_get(self, bundle, **kwargs): instance_id = kwargs['pk'] try: return FormAccessors(kwargs['domain']).get_form(instance_id) except XFormNotFound: raise object_does_not_exist("XFormInstance", instance_id)
return {'pk': get_obj(bundle_or_obj).case_id} def case_es(self, domain): # Note that CaseESView is used only as an ES client, for `run_query` against the proper index return MOCK_CASE_ES_VIEW or CaseESView(domain) def obj_get(self, bundle, **kwargs): case_id = kwargs['pk'] domain = kwargs['domain'] try: case = CaseAccessors(domain).get_case(case_id) if case.domain != domain: raise CaseNotFound return case except CaseNotFound: raise object_does_not_exist("CommCareCase", case_id) def obj_get_list(self, bundle, domain, **kwargs): try: es_query = es_query_from_get_params(bundle.request.GET, domain, doc_type='case') except Http400 as e: raise BadRequest(str(e)) return ElasticAPIQuerySet( payload=es_query, model=ESCase, es_client=self.case_es(domain)).order_by('server_modified_on') class Meta(CustomResourceMeta): authentication = RequirePermissionAuthentication(Permissions.edit_data)