def handle_info_json(self, model, info, fulltextsearch=None): """Handle info JSON query filter.""" clauses = [] headlines = [] order_by_ranks = [] if '::' in info: pairs = info.split('|') for pair in pairs: if pair != '': k, v = pair.split("::") if fulltextsearch == '1': vector = _entity_descriptor(model, 'info')[k].astext clause = func.to_tsvector(vector).match(v) clauses.append(clause) if len(headlines) == 0: headline = func.ts_headline( self.language, vector, func.to_tsquery(v)) headlines.append(headline) order = func.ts_rank_cd(func.to_tsvector(vector), func.to_tsquery(v), 4).label('rank') order_by_ranks.append(order) else: clauses.append( _entity_descriptor(model, 'info')[k].astext == v) else: info = json.dumps(info) clauses.append( cast(_entity_descriptor(model, 'info'), Text) == info) return clauses, headlines, order_by_ranks
def handle_info_json(self, model, info, fulltextsearch=None): """Handle info JSON query filter.""" clauses = [] headlines = [] order_by_ranks = [] if '::' in info: pairs = info.split('|') for pair in pairs: if pair != '': k,v = pair.split("::") if fulltextsearch == '1': vector = _entity_descriptor(model, 'info')[k].astext clause = func.to_tsvector(vector).match(v) clauses.append(clause) if len(headlines) == 0: headline = func.ts_headline(self.language, vector, func.to_tsquery(v)) headlines.append(headline) order = func.ts_rank_cd(func.to_tsvector(vector), func.to_tsquery(v), 4).label('rank') order_by_ranks.append(order) else: clauses.append(_entity_descriptor(model, 'info')[k].astext == v) else: info = json.dumps(info) clauses.append(cast(_entity_descriptor(model, 'info'), Text) == info) return clauses, headlines, order_by_ranks
def generate_query_from_keywords(self, model, fulltextsearch=None, **kwargs): clauses = [_entity_descriptor(model, key) == value for key, value in list(kwargs.items()) if (key != 'info' and key != 'fav_user_ids' and key != 'created' and key != 'project_id')] queries = [] headlines = [] order_by_ranks = [] or_clauses = [] if 'info' in list(kwargs.keys()): queries, headlines, order_by_ranks = self.handle_info_json(model, kwargs['info'], fulltextsearch) clauses = clauses + queries if 'created' in list(kwargs.keys()): like_query = kwargs['created'] + '%' clauses.append( _entity_descriptor(model, 'created').like(like_query)) if 'project_id' in list(kwargs.keys()): tmp = "%s" % kwargs['project_id'] project_ids = re.findall(r'\d+', tmp) for project_id in project_ids: or_clauses.append((_entity_descriptor(model, 'project_id') == project_id)) all_clauses = and_(and_(*clauses), or_(*or_clauses)) return (all_clauses,), queries, headlines, order_by_ranks
def generate_query_from_keywords(self, model, fulltextsearch=None, **kwargs): clauses = [_entity_descriptor(model, key) == value for key, value in kwargs.items() if (key != 'info' and key != 'fav_user_ids' and key != 'created' and key != 'project_id')] queries = [] headlines = [] order_by_ranks = [] or_clauses = [] if 'info' in kwargs.keys(): queries, headlines, order_by_ranks = self.handle_info_json(model, kwargs['info'], fulltextsearch) clauses = clauses + queries if 'created' in kwargs.keys(): like_query = kwargs['created'] + '%' clauses.append(_entity_descriptor(model,'created').like(like_query)) if 'project_id' in kwargs.keys(): tmp = "%s" % kwargs['project_id'] project_ids = re.findall(r'\d+', tmp) for project_id in project_ids: or_clauses.append((_entity_descriptor(model, 'project_id') == project_id)) all_clauses = and_(and_(*clauses), or_(*or_clauses)) return (all_clauses,), queries, headlines, order_by_ranks
def generate_query_from_keywords(model, **kwargs): clauses = [_entity_descriptor(model, key) == value for key, value in kwargs.items() if key != 'info'] if 'info' in kwargs.keys(): info = json.dumps(kwargs['info']) clauses.append(cast(_entity_descriptor(model, 'info'), Text) == info) return and_(*clauses) if len(clauses) != 1 else (and_(*clauses), )
def wtfilter_by(self, prune_none_values=False, **kwargs): if prune_none_values: kwargs = {k: v for k, v in kwargs.items() if v is not None} filters = [] for key, value in kwargs.items(): descriptor = _entity_descriptor(self._joinpoint_zero(), key) if isinstance(value, str): descriptor = _entity_descriptor(self._joinpoint_zero(), key) filters.extend(get_filters_list([(value, descriptor)])) else: filters.append(descriptor == value) return self.filter(*filters)
def get_column(joinpoint, token, joins_needed): try: result = _entity_descriptor(get_mapper(joinpoint), token) if type(result) != property: return result except exc.InvalidRequestError: pass for join in joins_needed: try: return _entity_descriptor(join, token) except exc.InvalidRequestError: pass
def _attribute(self, name): """Return keyword expressions extracted from the primary entity of the query, or the last entity that was the target of a call to `.Query.join`. """ return _entity_descriptor(self._joinpoint_zero(), name)
def order_by(self, *args): args = list(args) joins_needed = [] for idx, arg in enumerate(args): q = self if not isinstance(arg, six.string_types): continue if arg[0] in '+-': desc = arg[0] == '-' arg = arg[1:] else: desc = False q = self column = None for token in arg.split('__'): column = _entity_descriptor(q._joinpoint_zero(), token) if column.impl.uses_objects: q = q.join(column) joins_needed.append(column) column = None if column is None: raise ValueError('Tried to order by table, column expected') if desc: column = column.desc() args[idx] = column q = super(DjangoQueryMixin, self).order_by(*args) for join in joins_needed: q = q.join(join) return q
def order_by(self, *args): args = list(args) joins_needed = [] for idx, arg in enumerate(args): q = self if not isinstance(arg, str): continue if arg[0] in '+-': desc = arg[0] == '-' arg = arg[1:] else: desc = False q = self column = None for token in arg.split('__'): column = _entity_descriptor(q._joinpoint_zero(), token) if column.impl.uses_objects: q = q.join(column) joins_needed.append(column) column = None if column is None: raise ValueError('Tried to order by table, column expected') if desc: column = column.desc() args[idx] = column q = super(DjangoQueryMixin, self).order_by(*args) for join in joins_needed: q = q.join(join) return q
def _filter_or_exclude(self, negate, kwargs): q = self negate_if = lambda expr: expr if not negate else ~expr column = None for arg, value in kwargs.items(): for token in arg.split('__'): if column is None: column = _entity_descriptor(q._joinpoint_zero(), token) if column.impl.uses_objects: q = q.join(column) column = None elif token in self._underscore_operators: op = self._underscore_operators[token] q = q.filter(negate_if(op(column, *to_list(value)))) column = None elif token in self._underscore_list_operators: op = self._underscore_list_operators[token] q = q.filter(negate_if(op(column, to_list(value)))) column = None else: raise ValueError('No idea what to do with %r' % token) if column is not None: q = q.filter(negate_if(column == value)) column = None q = q.reset_joinpoint() return q
def _filter_or_exclude(self, negate, kwargs): q = self negate_if = lambda expr: expr if not negate else ~expr column = None for arg, value in kwargs.iteritems(): for token in arg.split('__'): if column is None: column = _entity_descriptor(q._joinpoint_zero(), token) if column.impl.uses_objects: q = q.join(column) column = None elif token in self.OPERATORS: op = self.OPERATORS[token] if isinstance(value, (list, tuple)): value = [value] q = q.filter(negate_if(op(column, *to_list(value)))) column = None else: raise ValueError('No idea what to do with %r' % token) if column is not None: q = q.filter(negate_if(column == value)) column = None q = q.reset_joinpoint() return q
def _attribute(self, name): """ Return keyword expressions extracted from the primary entity of the query, or the last entity that was the target of a call to `.Query.join`. """ return _entity_descriptor(self._joinpoint_zero(), name)
def filter_like_by(self, **kwargs): """like方法""" clauses = [ _entity_descriptor(self._joinpoint_zero(), key).like("%{}%".format(value)) for key, value in kwargs.items() ] return self.filter(*clauses)
def generate_query_from_keywords(self, model, fulltextsearch=None, **kwargs): clauses = [_entity_descriptor(model, key) == value for key, value in kwargs.items() if key != 'info'] if 'info' in kwargs.keys(): clauses = clauses + self.handle_info_json(model, kwargs['info'], fulltextsearch) return and_(*clauses) if len(clauses) != 1 else (and_(*clauses), )
def _get_batch_clause(self, model_cls, ids): """Return OR clauses for batch operations by ID.""" clauses = [] for _id in ids: clauses.append(_entity_descriptor(model_cls, 'id') == _id) batch_clause = or_(*clauses) self._validate_batch_clause(model_cls, batch_clause, ids) return batch_clause
def handle_info_json(self, model, info, fulltextsearch=None): """Handle info JSON query filter.""" clauses = [] if '::' in info: pairs = info.split('|') for pair in pairs: if pair != '': k,v = pair.split("::") if fulltextsearch == '1': vector = _entity_descriptor(model, 'info')[k].astext clause = func.to_tsvector(vector).match(v) clauses.append(clause) else: clauses.append(_entity_descriptor(model, 'info')[k].astext == v) else: info = json.dumps(info) clauses.append(cast(_entity_descriptor(model, 'info'), Text) == info) return clauses
def handle_info_json(self, model, info, fulltextsearch=None): """Handle info JSON query filter.""" clauses = [] headlines = [] order_by_ranks = [] if info and '::' in info: pairs = info.split('|') for pair in pairs: if pair != '': k, v = pair.split("::") if fulltextsearch == '1': vector = _entity_descriptor(model, 'info')[k].astext clause = func.to_tsvector(vector).match(v) clauses.append(clause) if len(headlines) == 0: headline = func.ts_headline( self.language, vector, func.to_tsquery(v)) headlines.append(headline) order = func.ts_rank_cd( func.to_tsvector(vector), func.to_tsquery(v), 4).label('rank') order_by_ranks.append(order) else: clauses.append( _entity_descriptor(model, 'info')[k].astext == v) else: if type(info) == dict: clauses.append(_entity_descriptor(model, 'info') == info) if type(info) == str or type(info) == str: try: info = json.loads(info) if type(info) == int or type(info) == float: info = '"%s"' % info except ValueError: info = '"%s"' % info clauses.append(_entity_descriptor(model, 'info').contains(info)) return clauses, headlines, order_by_ranks
def search_by_name_orfilters(self, keyword, **filters): if len(keyword) == 0: return [] keyword = '%' + keyword.lower() + '%' query = self.db.session.query(User).filter( or_( func.lower(User.name).like(keyword), func.lower(User.fullname).like(keyword))) if filters: or_clauses = [] for k in filters.keys(): or_clauses.append(_entity_descriptor(User, k) == filters[k]) query = query.filter(or_(*or_clauses)) return query.all()
def handle_info_json(self, model, info, fulltextsearch=None): """Handle info JSON query filter.""" clauses = [] headlines = [] order_by_ranks = [] if info and '::' in info: pairs = info.split('|') for pair in pairs: if pair != '': k,v = pair.split("::") if fulltextsearch == '1': vector = _entity_descriptor(model, 'info')[k].astext clause = func.to_tsvector(vector).match(v) clauses.append(clause) if len(headlines) == 0: headline = func.ts_headline(self.language, vector, func.to_tsquery(v)) headlines.append(headline) order = func.ts_rank_cd(func.to_tsvector(vector), func.to_tsquery(v), 4).label('rank') order_by_ranks.append(order) else: clauses.append(_entity_descriptor(model, 'info')[k].astext == v) else: if type(info) == dict: clauses.append(_entity_descriptor(model, 'info') == info) if type(info) == str or type(info) == unicode: try: info = json.loads(info) if type(info) == int or type(info) == float: info = '"%s"' % info except ValueError: info = '"%s"' % info clauses.append(_entity_descriptor(model, 'info').contains(info)) return clauses, headlines, order_by_ranks
def generate_query_from_keywords(self, model, fulltextsearch=None, **kwargs): clauses = [ _entity_descriptor(model, key) == value for key, value in kwargs.items() if key != 'info' ] queries = [] headlines = [] order_by_ranks = [] if 'info' in kwargs.keys(): #clauses = clauses + self.handle_info_json(model, kwargs['info'], # fulltextsearch) queries, headlines, order_by_ranks = self.handle_info_json( model, kwargs['info'], fulltextsearch) clauses = clauses + queries if len(clauses) != 1: return and_(*clauses), queries, headlines, order_by_ranks else: return (and_(*clauses), ), queries, headlines, order_by_ranks
def _get_vector(self, col): """Return the query vector.""" try: return _entity_descriptor(Annotation, col) except InvalidRequestError: return _entity_descriptor(Annotation, '_data')[col]