def get_default_category_name(): if cache.has(DEFAULT_CATEGORY_NAME_CACHE_KEY): return cache.get(DEFAULT_CATEGORY_NAME_CACHE_KEY) default_category = try_get_default_category() default_category_name = default_category.name if default_category else None cache.put(DEFAULT_CATEGORY_NAME_CACHE_KEY, default_category_name) return default_category_name
def get_default_category_name() -> str: if cache.has(DEFAULT_CATEGORY_NAME_CACHE_KEY): return cache.get(DEFAULT_CATEGORY_NAME_CACHE_KEY) default_category = get_default_category() default_category_name = default_category.name cache.put(DEFAULT_CATEGORY_NAME_CACHE_KEY, default_category_name) return default_category_name
def execute(self, query_text: str, offset: int, limit: int) -> Tuple[int, List[model.Base]]: search_query = self.parser.parse(query_text) self.config.on_search_query_parsed(search_query) if offset < 0: limit = max(0, limit + offset) offset = 0 disable_eager_loads = False for token in search_query.sort_tokens: if token.name == 'random': disable_eager_loads = True key = (id(self.config), hash(search_query), offset, limit) if cache.has(key): return cache.get(key) filter_query = self.config.create_filter_query(disable_eager_loads) filter_query = filter_query.options(sa.orm.lazyload('*')) filter_query = self._prepare_db_query(filter_query, search_query, True) entities = (filter_query.offset(offset).limit(limit).all()) count_query = self.config.create_count_query(disable_eager_loads) count_query = count_query.options(sa.orm.lazyload('*')) count_query = self._prepare_db_query(count_query, search_query, False) count_statement = (count_query.statement.with_only_columns( [sa.func.count()]).order_by(None)) count = db.session.execute(count_statement).scalar() ret = (count, entities) cache.put(key, ret) return ret
def execute( self, query_text: str, offset: int, limit: int ) -> Tuple[int, List[model.Base]]: search_query = self.parser.parse(query_text) self.config.on_search_query_parsed(search_query) if offset < 0: limit = max(0, limit + offset) offset = 0 disable_eager_loads = False for token in search_query.sort_tokens: if token.name == 'random': disable_eager_loads = True key = (id(self.config), hash(search_query), offset, limit) if cache.has(key): return cache.get(key) filter_query = self.config.create_filter_query(disable_eager_loads) filter_query = filter_query.options(sa.orm.lazyload('*')) filter_query = self._prepare_db_query(filter_query, search_query, True) entities = ( filter_query .offset(offset) .limit(limit) .all()) count_query = self.config.create_count_query(disable_eager_loads) count_query = count_query.options(sa.orm.lazyload('*')) count_query = self._prepare_db_query(count_query, search_query, False) count_statement = ( count_query .statement .with_only_columns([sa.func.count()]) .order_by(None)) count = db.session.execute(count_statement).scalar() ret = (count, entities) cache.put(key, ret) return ret
def execute(self, query_text, page, page_size): ''' Parse input and return tuple containing total record count and filtered entities. ''' search_query = self.parser.parse(query_text) self.config.on_search_query_parsed(search_query) disable_eager_loads = False for token in search_query.sort_tokens: if token.name == 'random': disable_eager_loads = True key = (id(self.config), hash(search_query), page, page_size) if cache.has(key): return cache.get(key) filter_query = self.config.create_filter_query(disable_eager_loads) filter_query = filter_query.options(sqlalchemy.orm.lazyload('*')) filter_query = self._prepare_db_query(filter_query, search_query, True) entities = filter_query \ .offset(max(page - 1, 0) * page_size) \ .limit(page_size) \ .all() count_query = self.config.create_count_query(disable_eager_loads) count_query = count_query.options(sqlalchemy.orm.lazyload('*')) count_query = self._prepare_db_query(count_query, search_query, False) count_statement = count_query \ .statement \ .with_only_columns([sqlalchemy.func.count()]) \ .order_by(None) count = db.session.execute(count_statement).scalar() ret = (count, entities) cache.put(key, ret) return ret