def apply_date_criterion_to_column( column: SaQuery, criterion: criteria.BaseCriterion) -> SaQuery: if isinstance(criterion, criteria.PlainCriterion): min_date, max_date = util.parse_time_range(criterion.value) expr = column.between(min_date, max_date) elif isinstance(criterion, criteria.ArrayCriterion): expr = sa.sql.false() for value in criterion.values: min_date, max_date = util.parse_time_range(value) expr = expr | column.between(min_date, max_date) elif isinstance(criterion, criteria.RangedCriterion): assert criterion.min_value or criterion.max_value if criterion.min_value and criterion.max_value: min_date = util.parse_time_range(criterion.min_value)[0] max_date = util.parse_time_range(criterion.max_value)[1] expr = column.between(min_date, max_date) elif criterion.min_value: min_date = util.parse_time_range(criterion.min_value)[0] expr = column >= min_date elif criterion.max_value: max_date = util.parse_time_range(criterion.max_value)[1] expr = column <= max_date else: assert False return expr
def _prepare_db_query( self, db_query: SaQuery, search_query: SearchQuery, use_sort: bool) -> SaQuery: for anon_token in search_query.anonymous_tokens: if not self.config.anonymous_filter: raise errors.SearchError( 'Anonymous tokens are not valid in this context.') db_query = self.config.anonymous_filter( db_query, anon_token.criterion, anon_token.negated) for named_token in search_query.named_tokens: if named_token.name not in self.config.named_filters: raise errors.SearchError( 'Unknown named token: %r. Available named tokens: %r.' % ( named_token.name, _format_dict_keys(self.config.named_filters))) db_query = self.config.named_filters[named_token.name]( db_query, named_token.criterion, named_token.negated) for sp_token in search_query.special_tokens: if sp_token.value not in self.config.special_filters: raise errors.SearchError( 'Unknown special token: %r. ' 'Available special tokens: %r.' % ( sp_token.value, _format_dict_keys(self.config.special_filters))) db_query = self.config.special_filters[sp_token.value]( db_query, None, sp_token.negated) if use_sort: for sort_token in search_query.sort_tokens: if sort_token.name not in self.config.sort_columns: raise errors.SearchError( 'Unknown sort token: %r. ' 'Available sort tokens: %r.' % ( sort_token.name, _format_dict_keys(self.config.sort_columns))) column, default_order = ( self.config.sort_columns[sort_token.name]) order = _get_order(sort_token.order, default_order) if order == sort_token.SORT_ASC: db_query = db_query.order_by(column.asc()) elif order == sort_token.SORT_DESC: db_query = db_query.order_by(column.desc()) db_query = self.config.finalize_query(db_query) return db_query
def wrapper( query: SaQuery, criterion: Optional[criteria.BaseCriterion], negated: bool) -> SaQuery: assert criterion expr = apply_num_criterion_to_column(column, criterion, transformer) if negated: expr = ~expr return query.filter(expr)
def wrapper( query: SaQuery, criterion: Optional[criteria.BaseCriterion], negated: bool, ) -> SaQuery: assert criterion expr = apply_date_criterion_to_column(column, criterion) if negated: expr = ~expr return query.filter(expr)
def tumbleweed_filter( self, query: SaQuery, _criterion: Optional[criteria.BaseCriterion], negated: bool) -> SaQuery: expr = ( (model.Post.comment_count == 0) & (model.Post.favorite_count == 0) & (model.Post.score == 0)) if negated: expr = ~expr return query.filter(expr)
def _user_filter(query: SaQuery, criterion: Optional[criteria.BaseCriterion], negated: bool) -> SaQuery: assert criterion if isinstance(criterion, criteria.PlainCriterion) \ and not criterion.value: # pylint: disable=singleton-comparison expr = model.Post.user_id == None if negated: expr = ~expr return query.filter(expr) return search_util.create_subquery_filter( model.Post.user_id, model.User.user_id, model.User.name, search_util.create_str_filter)(query, criterion, negated)
def wrapper(query: SaQuery, criterion: Optional[criteria.BaseCriterion], negated: bool) -> SaQuery: assert criterion subquery = db.session.query(right_id_column.label('foreign_id')) if subquery_decorator: subquery = subquery_decorator(subquery) subquery = subquery.options(sa.orm.lazyload('*')) subquery = filter_func(subquery, criterion, False) subquery = subquery.subquery('t') expression = left_id_column.in_(subquery) if negated: expression = ~expression return query.filter(expression)
def _user_filter(query: SaQuery, criterion: Optional[criteria.BaseCriterion], negated: bool) -> SaQuery: assert criterion if isinstance(criterion, criteria.PlainCriterion) and not criterion.value: expr = model.Post.user_id == None # noqa: E711 if negated: expr = ~expr return query.filter(expr) return search_util.create_subquery_filter( model.Post.user_id, model.User.user_id, model.User.name, search_util.create_str_filter, )(query, criterion, negated)
def wrapper( query: SaQuery, criterion: Optional[criteria.BaseCriterion], negated: bool) -> SaQuery: assert criterion subquery = db.session.query(right_id_column.label('foreign_id')) if subquery_decorator: subquery = subquery_decorator(subquery) subquery = subquery.options(sa.orm.lazyload('*')) subquery = filter_func(subquery, criterion, False) subquery = subquery.subquery('t') expression = left_id_column.in_(subquery) if negated: expression = ~expression return query.filter(expression)
def _user_filter( query: SaQuery, criterion: Optional[criteria.BaseCriterion], negated: bool) -> SaQuery: assert criterion if isinstance(criterion, criteria.PlainCriterion) \ and not criterion.value: # pylint: disable=singleton-comparison expr = model.Post.user_id == None if negated: expr = ~expr return query.filter(expr) return search_util.create_subquery_filter( model.Post.user_id, model.User.user_id, model.User.name, search_util.create_str_filter)(query, criterion, negated)
def wrapper(query: SaQuery, criterion: Optional[criteria.BaseCriterion], negated: bool) -> SaQuery: assert criterion if not getattr(criterion, 'internal', False): raise errors.SearchError('투표는 공개적으로 확인할 수 없습니다. %r를 시도해보세요.' % 'special:liked') user_alias = sa.orm.aliased(model.User) score_alias = sa.orm.aliased(model.PostScore) expr = score_alias.score == score expr = expr & search_util.apply_str_criterion_to_column( user_alias.name, criterion) if negated: expr = ~expr ret = (query.join( score_alias, score_alias.post_id == model.Post.post_id).join( user_alias, user_alias.user_id == score_alias.user_id).filter(expr)) return ret
def wrapper( query: SaQuery, criterion: Optional[criteria.BaseCriterion], negated: bool, ) -> SaQuery: assert criterion if not getattr(criterion, "internal", False): raise errors.SearchError( "Votes cannot be seen publicly. Did you mean %r?" % "special:liked") user_alias = sa.orm.aliased(model.User) score_alias = sa.orm.aliased(model.PostScore) expr = score_alias.score == score expr = expr & search_util.apply_str_criterion_to_column( user_alias.name, criterion) if negated: expr = ~expr ret = (query.join( score_alias, score_alias.post_id == model.Post.post_id).join( user_alias, user_alias.user_id == score_alias.user_id).filter(expr)) return ret
def finalize_query(self, query: SaQuery) -> SaQuery: return query.order_by(model.Post.post_id.desc())
def finalize_query(self, query: SaQuery) -> SaQuery: return query.order_by(model.User.name.asc())
def finalize_query(self, query: SaQuery) -> SaQuery: return query.order_by(model.Tag.first_name.asc())
def finalize_query(self, query: SaQuery) -> SaQuery: return query.order_by(model.Snapshot.creation_time.desc())
def finalize_query(self, query: SaQuery) -> SaQuery: return query.order_by(model.Comment.creation_time.desc())