lookupy icon indicating copy to clipboard operation
lookupy copied to clipboard

Does not implemet Django QuerySet interface

Open w495 opened this issue 9 years ago • 1 comments

There is no some methods, that QuerySets have. And it cannot interact with django.db.models.query_utils.Q. May it be a feature request?

w495 avatar Nov 24 '15 16:11 w495

The main idea of this is simple

"""
In lookupy.lookupy
...
"""

def filter_items(items, *args, **kwargs):
    q1 = list(args) if args is not None else []
    q2 = [Q(**kwargs)] if kwargs is not None else []
    lookup_groups = q1 + q2
    is_true = lambda item: query_is_true(lookup_groups, item)
    return (item for item in items if is_true(item))


def convert_query(query):
    if isinstance(query, django.db.models.query_utils.Q):
        new_children = convert_query_it(query.children)
        if 'OR' == query.connector:
            query_it = (Q(**dict([nch])) for nch in new_children)
            query = reduce(operator.or_, query_it)
        else:
            query = Q(**dict(new_children))
        return query
    return query


def convert_query_it(query_iterable):
    for query in query_iterable:
        yield convert_query(query)


def query_is_true(query_iterable, item):
    query_iterable = convert_query_it(query_iterable)
    return all(query.evaluate(item) for query in query_iterable)

w495 avatar Nov 25 '15 07:11 w495