always return query in get_items. use for count and in/notin operator in query

This commit is contained in:
j 2016-01-07 11:37:50 +05:30
parent 650f30a776
commit 7c438dc6b2
2 changed files with 12 additions and 22 deletions

View file

@ -121,17 +121,9 @@ class Parser(object):
else: else:
u = self._user.query.filter_by(id=settings.USER_ID).one() u = self._user.query.filter_by(id=settings.USER_ID).one()
l = self._list.query.filter_by(user_id=u.id, name=name).one() l = self._list.query.filter_by(user_id=u.id, name=name).one()
if exclude: ids = l.get_items().options(load_only('id'))
ids = l.user.items.filter(self._list.id==l.id).options(load_only('id')) in_op = operators.notin_op if exclude else operators.in_op
q = operators.notin_op(self._model.id, ids) q = in_op(self._model.id, ids)
else:
if l.type == 'smart':
data = l._query
q = self.parse_conditions(data.get('conditions', []),
data.get('operator', '&'))
else:
q = (self._list.id == l.id)
self._joins.append(self._list.items)
return q return q
elif key_type == 'date': elif key_type == 'date':
def parse_date(d): def parse_date(d):

View file

@ -229,7 +229,7 @@ class List(db.Model):
state.db.session.add(l) state.db.session.add(l)
state.db.session.commit() state.db.session.commit()
if user_id == settings.USER_ID: if user_id == settings.USER_ID:
if not l._query: if not l._query and name != '':
Changelog.record(state.user(), 'addlist', l.name) Changelog.record(state.user(), 'addlist', l.name)
return l return l
@ -262,17 +262,17 @@ class List(db.Model):
i.update() i.update()
state.db.session.add(self) state.db.session.add(self)
state.db.session.commit() state.db.session.commit()
if self.user_id == settings.USER_ID: if self.user_id == settings.USER_ID and self.name != '':
Changelog.record(self.user, 'addlistitems', self.name, items) Changelog.record(self.user, 'addlistitems', self.name, items)
self.user.clear_smart_list_cache() self.user.clear_smart_list_cache()
self.user.clear_list_cache() self.user.clear_list_cache()
def get_items(self): def get_items(self):
from item.models import Item, user_items
if self.type == 'smart': if self.type == 'smart':
from item.models import Item, user_items
return Parser(Item, user_items).find({'query': self._query}) return Parser(Item, user_items).find({'query': self._query})
else: else:
return self.items return self.user.items.join(Item.lists, aliased=True).filter(List.id == self.id)
def remove_items(self, items): def remove_items(self, items):
from item.models import Item from item.models import Item
@ -283,7 +283,7 @@ class List(db.Model):
i.update() i.update()
state.db.session.add(self) state.db.session.add(self)
state.db.session.commit() state.db.session.commit()
if self.user_id == settings.USER_ID: if self.user_id == settings.USER_ID and self.name != '':
Changelog.record(self.user, 'removelistitems', self.name, items) Changelog.record(self.user, 'removelistitems', self.name, items)
self.user.clear_smart_list_cache() self.user.clear_smart_list_cache()
self.user.clear_list_cache() self.user.clear_list_cache()
@ -293,7 +293,7 @@ class List(db.Model):
for i in self.items: for i in self.items:
self.items.remove(i) self.items.remove(i)
if not self._query: if not self._query:
if self.user_id == settings.USER_ID: if self.user_id == settings.USER_ID and self.name != '':
Changelog.record(self.user, 'removelist', self.name) Changelog.record(self.user, 'removelist', self.name)
state.db.session.delete(self) state.db.session.delete(self)
state.db.session.commit() state.db.session.commit()
@ -318,14 +318,12 @@ class List(db.Model):
return self.public_id return self.public_id
def items_count(self): def items_count(self):
return self.get_items().count()
key = self.find_id key = self.find_id
if key in settings.list_cache: if key in settings.list_cache:
value = settings.list_cache[key] value = settings.list_cache[key]
else: else:
if self.type == 'smart': value = self.get_items().count()
value = self.get_items().count()
else:
value = len(self.items)
settings.list_cache[key] = value settings.list_cache[key] = value
return value return value
@ -336,7 +334,7 @@ class List(db.Model):
'name': self.name, 'name': self.name,
'index': self.index_, 'index': self.index_,
# to slow for many smart lists # to slow for many smart lists
'items': 0, #self.items_count(), 'items': self.items_count(),
'type': self.type 'type': self.type
} }
if self.name == '': if self.name == '':