from django.db.models import Q
from hier.search import SearchResult
from .models import app_name, Apart, Meter, Bill, Service, Price

def search(user, query):
    result = SearchResult(query)
    
    lookups = Q(name__icontains=query) | Q(addr__icontains=query)
    items = Apart.objects.filter(user = user.id).filter(lookups)
    for item in items:
        result.add(app_name, 'apart', item.id, None, item.name, item.addr, False)

    lookups = Q(info__icontains=query)
    items = Meter.objects.filter(apart__user = user.id).filter(lookups)
    for item in items:
        result.add(app_name, 'meter', item.id, item.reading.date(), item.name(), item.info, False, item.apart.name, item.period.strftime('%m.%Y'))

    lookups = Q(info__icontains=query) | Q(url__icontains=query)
    items = Bill.objects.filter(apart__user = user.id).filter(lookups)
    for item in items:
        result.add(app_name, 'bill', item.id, item.payment.date(), item.name(), item.info, False, item.apart.name, item.period.strftime('%m.%Y'))

    lookups = Q(name__icontains=query) | Q(abbr__icontains=query)
    items = Service.objects.filter(apart__user = user.id).filter(lookups)
    for item in items:
        result.add(app_name, 'service', item.id, None, item.name, item.abbr, False, item.apart.name)

    lookups = Q(info__icontains=query)
    items = Price.objects.filter(apart__user = user.id).filter(lookups)
    for item in items:
        result.add(app_name, 'price', item.id, item.start, item.name(), item.info, False, item.apart.name)

    return result.items

[((6, 13, 6, 32), 'hier.search.SearchResult', 'SearchResult', ({(6, 26, 6, 31): 'query'}, {}), '(query)', False, 'from hier.search import SearchResult\n'), ((13, 14, 13, 38), 'django.db.models.Q', 'Q', (), '', False, 'from django.db.models import Q\n'), ((28, 14, 28, 38), 'django.db.models.Q', 'Q', (), '', False, 'from django.db.models import Q\n'), ((8, 14, 8, 38), 'django.db.models.Q', 'Q', (), '', False, 'from django.db.models import Q\n'), ((8, 41, 8, 65), 'django.db.models.Q', 'Q', (), '', False, 'from django.db.models import Q\n'), ((18, 14, 18, 38), 'django.db.models.Q', 'Q', (), '', False, 'from django.db.models import Q\n'), ((18, 41, 18, 64), 'django.db.models.Q', 'Q', (), '', False, 'from django.db.models import Q\n'), ((23, 14, 23, 38), 'django.db.models.Q', 'Q', (), '', False, 'from django.db.models import Q\n'), ((23, 41, 23, 65), 'django.db.models.Q', 'Q', (), '', False, 'from django.db.models import Q\n')]