diff --git a/oml/item/query.py b/oml/item/query.py index d2a55c5..5dc5300 100644 --- a/oml/item/query.py +++ b/oml/item/query.py @@ -19,7 +19,7 @@ def parse(data): if key in data: query[key] = data[key] #print data - query['qs'] = Parser(models.Item).find(data) + query['qs'] = Parser(models.Item, models.user_items).find(data) if not 'group' in query: query['qs'] = order(query['qs'], query['sort']) return query diff --git a/oml/queryparser.py b/oml/queryparser.py index 4f2f8e6..8720bd0 100644 --- a/oml/queryparser.py +++ b/oml/queryparser.py @@ -37,11 +37,12 @@ def get_operator(op, type='str'): class Parser(object): - def __init__(self, model): + def __init__(self, model, user_items): self._model = model self._find = model.find.mapper.class_ self._sort = model.sort.mapper.class_ self._user = model.users.mapper.class_ + self._user_items = user_items self._list = model.lists.mapper.class_ self.item_keys = model.item_keys self.filter_keys = model.filter_keys @@ -115,19 +116,19 @@ class Parser(object): elif k == 'list': nickname, name = v.split(':', 1) if nickname: - u = self._user.query.filter_by(nickname=nickname).first() + u = self._user.query.filter_by(nickname=nickname).one() else: - u = self._user.query.filter_by(id=settings.USER_ID).first() + u = self._user.query.filter_by(id=settings.USER_ID).one() if name: - l = self._list.query.filter_by(user_id=u.id, name=name).first() + l = self._list.query.filter_by(user_id=u.id, name=name).one() else: l = None if not l: if not u: - q = (self._user.id == 0) + q = (self._user_items.columns['user_id'] == 0) else: - q = (self._user.id == u.id) - self._joins.append(self._model.users) + q = (self._user_items.columns['user_id'] == u.id) + self._joins.append(self._user_items) elif l.type == 'smart': data = l._query q = self.parse_conditions(data.get('conditions', []), @@ -235,4 +236,5 @@ class Parser(object): # FIXME: group_by needed here to avoid # duplicates due to joins. qs = qs.group_by(self._model.id) + #print(qs) return qs diff --git a/oml/setup.py b/oml/setup.py index cc270ed..7b418e6 100644 --- a/oml/setup.py +++ b/oml/setup.py @@ -243,6 +243,8 @@ def upgrade_db(old, new=None): if 'id' in m.data: del m.data['id'] m.save() + if old <= '20160106-495-d1b9e96': + run_sql('CREATE INDEX ix_useritem_user ON useritem ("user_id")') def create_default_lists(user_id=None): with db.session(): diff --git a/oml/user/models.py b/oml/user/models.py index 01e1bf0..ed6001e 100644 --- a/oml/user/models.py +++ b/oml/user/models.py @@ -95,7 +95,7 @@ class User(db.Model): del settings.list_cache[key] def clear_smart_list_cache(self): - smart_lists = [':' + l.name for l in List.query.filter_by(type='smart')] + smart_lists = [':%d' % l.id for l in List.query.filter_by(type='smart')] for key in list(settings.list_cache): if key in smart_lists: del settings.list_cache[key] @@ -258,6 +258,7 @@ class List(db.Model): state.db.session.commit() if self.user_id == settings.USER_ID: Changelog.record(self.user, 'addlistitems', self.name, items) + self.clear_smart_list_cache() def remove_items(self, items): from item.models import Item @@ -305,10 +306,10 @@ class List(db.Model): if key in settings.list_cache: value = settings.list_cache[key] else: - from item.models import Item + from item.models import Item, user_items if self._query: data = self._query - value = Parser(Item).find({'query': data}).count() + value = Parser(Item, user_items).find({'query': data}).count() else: value = len(self.items) settings.list_cache[key] = value