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:
u = self._user.query.filter_by(id=settings.USER_ID).one()
l = self._list.query.filter_by(user_id=u.id, name=name).one()
if exclude:
ids = l.user.items.filter(self._list.id==l.id).options(load_only('id'))
q = operators.notin_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)
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)
return q
elif key_type == 'date':
def parse_date(d):

View file

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