Beispiel #1
0
def parse_filter(s):
    _or = OrOp()
    for t in s.split(OR_OPERATOR):
        _and = AndOp()
        for term in t.split(AND_OPERATOR):
            found = False
            for operator in OPERATORS:
                if operator in term:
                    f = term.split(operator, 1)
                    lhs = f[0]
                    rhs = f[1] if len(f) > 1 else None
                    if operator == ':':
                        rhs = rhs.split(',')
                    _and.append(QueryFilter(lhs, operator, rhs))
                    found = True
                    break
            if not found:
                _and.append(QueryFilter(term))
        _or.append(_and)
    return _or
def replace_filter(f, table, entity, comment, search_fields):
    if isinstance(f, BitOp):
        f.children = map(
            lambda child: replace_filter(
                child, table, entity, comment, search_fields),
            f.children)
    elif f.lhs == '' and search_fields:
        logger.debug('Search fields: %s', search_fields)
        rhs = f.rhs
        if rhs == '' and f.operator == '*':
            rhs = '%'
        ors = OrOp(map(
            lambda s: QueryFilter(s, f.operator, rhs),
            filter(
                lambda s: s in entity.columns,
                search_fields)))
        if 'id' in comment.columns:
            col = column_or_raise(table, 'id')
            ors.append(QueryFilter(col.name, f.operator, rhs))
        logger.debug('Searches: %s', ors)
        return ors
    return f