faster list query
This commit is contained in:
parent
a71dab1c91
commit
ed02b1772d
2 changed files with 20 additions and 8 deletions
|
@ -95,7 +95,8 @@ class Item(db.Model):
|
|||
|
||||
@classmethod
|
||||
def find(cls, data):
|
||||
return Parser(cls, user_items, Find, Sort).find(data)
|
||||
from user.models import list_items
|
||||
return Parser(cls, user_items, list_items, Find, Sort).find(data)
|
||||
|
||||
@classmethod
|
||||
def remove_many(cls, ids):
|
||||
|
|
|
@ -38,12 +38,13 @@ def get_operator(op, type='str'):
|
|||
|
||||
class Parser(object):
|
||||
|
||||
def __init__(self, model, user_items, find, sort):
|
||||
def __init__(self, model, user_items, list_items, find, sort):
|
||||
self._model = model
|
||||
self._find = find
|
||||
self._sort = sort
|
||||
self._user = model.users.mapper.class_
|
||||
self._user_items = user_items
|
||||
self._list_items = list_items
|
||||
self._list = model.lists.mapper.class_
|
||||
self.item_keys = model.item_keys
|
||||
self.filter_keys = model.filter_keys
|
||||
|
@ -129,14 +130,24 @@ class Parser(object):
|
|||
u = self._user.query.filter_by(nickname=nickname).one()
|
||||
else:
|
||||
u = self._user.query.filter_by(id=settings.USER_ID).one()
|
||||
if nickname:
|
||||
if not name:
|
||||
ids = self._user_items.select().where(
|
||||
self._user_items.columns['user_id'].is_(u.id)
|
||||
).with_only_columns(
|
||||
['item_id']
|
||||
)
|
||||
elif nickname:
|
||||
peer = utils.get_peer(u.id)
|
||||
if name:
|
||||
ids = peer.info['lists'].get(name, [])
|
||||
else:
|
||||
ids = list(peer.library.keys())
|
||||
else:
|
||||
l = self._list.query.filter_by(user_id=u.id, name=name).one()
|
||||
if l.type == 'static':
|
||||
ids = self._list_items.select().where(
|
||||
self._list_items.columns['list_id'].is_(l.id)
|
||||
).with_only_columns(
|
||||
['item_id']
|
||||
)
|
||||
else:
|
||||
ids = l.get_items().options(load_only('id'))
|
||||
in_op = operators.notin_op if exclude else operators.in_op
|
||||
q = in_op(self._model.id, ids)
|
||||
|
|
Loading…
Reference in a new issue