def search(cls, params, limit, offset=None, cursor=None, message=False, count=False): """Returns (records, cursor). Arguments args - Dictionary with Darwin Core concept keys keywords - list of keywords to search on """ ctx = tasklets.get_context() ctx.set_memcache_policy(False) qry = RecordIndex.query() # Add darwin core name filters args = params['terms'] if len(args) > 0: gql = 'SELECT * FROM RecordIndex WHERE' for k,v in args.iteritems(): gql = "%s %s = '%s' AND " % (gql, k, v) gql = gql[:-5] # Removes trailing AND logging.info(gql) # qry = query.parse_gql(gql)[0] qry = ndb.gql(gql) # Add full text keyword filters keywords = params['keywords'] for keyword in keywords: qry = qry.filter(RecordIndex.corpus == keyword) # Add sort orders: #qry = qry.order(RecordIndex.institutioncode) #qry = qry.order(RecordIndex.genus) #qry = qry.order(RecordIndex.specificepithet) #qry = qry.order(RecordIndex.country) #qry = qry.order(RecordIndex.year) logging.info('QUERY='+str(qry)) # Setup query paging #limit = params['limit'] #cursor = params['cursor'] if count: return qry.count(); if cursor: index_keys, next_cursor, more = qry.fetch_page(limit, start_cursor=cursor, keys_only=True) record_keys = [x.parent() for x in index_keys] else: index_keys, next_cursor, more = qry.fetch_page(limit, offset=offset, keys_only=True) record_keys = [x.parent() for x in index_keys] # Return results records = [x for x in ndb.get_multi(record_keys) if x] if message: records = [x.message for x in records if x] count = qry.count(limit=1000) return (records, next_cursor, more, count)
def setup_context_cache(self): """Set up the context cache. We only need cache active when testing the cache, so the default behavior is to disable it to avoid misleading test results. Override this when needed. """ ctx = tasklets.get_context() ctx.set_cache_policy(False) ctx.set_memcache_policy(False)
def get_async(self, **ctx_options): """Return a Future whose result is the entity for this Key. If no such entity exists, a Future is still returned, and the Future's eventual return result be None. """ from google.appengine.ext.ndb import model, tasklets ctx = tasklets.get_context() cls = model.Model._kind_map.get(self.kind()) if cls: cls._pre_get_hook(self) fut = ctx.get(self, **ctx_options) if cls: post_hook = cls._post_get_hook if not cls._is_default_hook(model.Model._default_post_get_hook, post_hook): fut.add_immediate_callback(post_hook, self, fut) return fut
def delete_async(self, **ctx_options): """Schedule deletion of the entity for this Key. This returns a Future, whose result becomes available once the deletion is complete. If no such entity exists, a Future is still returned. In all cases the Future's result is None (i.e. there is no way to tell whether the entity existed or not). """ from google.appengine.ext.ndb import tasklets, model ctx = tasklets.get_context() cls = model.Model._kind_map.get(self.kind()) if cls: cls._pre_delete_hook(self) fut = ctx.delete(self, **ctx_options) if cls: post_hook = cls._post_delete_hook if not cls._is_default_hook(model.Model._default_post_delete_hook, post_hook): fut.add_immediate_callback(post_hook, self, fut) return fut
def ctx(): ctx = tasklets.get_context() ctx.set_cache_policy(False) ctx.set_memcache_policy(False) return ctx
def _finish(): ctx = tasklets.get_context() tasklets.set_context(None) ctx.flush().check_success() eventloop.run()