from .models import Item, supported_types
import pygal
from .search import load_index, search


def get_readable_item_query(request, item_query):
    uids = []
    for i in item_query:
        if i.may_read(request.user):
            if i.type in supported_types():
                uids.append(i.id)
    uids = set(uids)
    return Item.objects.filter(id__in=uids)


def search_result_query(request):
    search_txt = pygal.get_search_query(request)
    ix = load_index()
    #
    return get_readable_item_query(request, search(ix, search_txt))
