Example #1
0
def apply_filter(model: DefaultMeta, query: BaseQuery) -> BaseQuery:
    for param, value in request.args.items():
        if param not in {'fields', 'sort', 'page', 'limit'}:
            operator = '=='
            match = COMPARISON_OPERATOR_RE.match(param)
            if match is not None:
                param, operator = match.groups()
            column_attr = getattr(model, param, None)
            if column_attr is not None:
                value = model.additional_validation(param, value)
                if value is None:
                    continue
                filter_argument = _get_filter_argument(column_attr, value, operator)
                query = query.filter(filter_argument)
    return query
Example #2
0
def apply_filter(model: DefaultMeta, query: BaseQuery) -> BaseQuery:
    """
    Functionality of filtering resources, returns filter arguments to query
    (example: id[gte]=3)
    """
    params = request.args.items()
    if params:
        for param, value in params:
            if param not in ["fields", "sort", "page", "limit"]:
                operator = "=="
                match = COMPARISON_OPERATORS_RE.match(param)
                if match is not None:
                    param, operator = match.groups()
                column_attr = getattr(model, param, None)
                if column_attr is not None:
                    value = model.additional_validation(param, value)
                    if value is None:
                        continue
                    filter_argument = _get_filter_argument(
                        column_attr, value, operator)
                    query = query.filter(filter_argument)
    return query