use intermediate table for user matching, fix clear_smart_list_cache

This commit is contained in:
j 2016-01-06 09:54:23 +05:30
parent d1b9e965f5
commit 8fc084b852
4 changed files with 16 additions and 11 deletions

View file

@ -19,7 +19,7 @@ def parse(data):
if key in data: if key in data:
query[key] = data[key] query[key] = data[key]
#print data #print data
query['qs'] = Parser(models.Item).find(data) query['qs'] = Parser(models.Item, models.user_items).find(data)
if not 'group' in query: if not 'group' in query:
query['qs'] = order(query['qs'], query['sort']) query['qs'] = order(query['qs'], query['sort'])
return query return query

View file

@ -37,11 +37,12 @@ def get_operator(op, type='str'):
class Parser(object): class Parser(object):
def __init__(self, model): def __init__(self, model, user_items):
self._model = model self._model = model
self._find = model.find.mapper.class_ self._find = model.find.mapper.class_
self._sort = model.sort.mapper.class_ self._sort = model.sort.mapper.class_
self._user = model.users.mapper.class_ self._user = model.users.mapper.class_
self._user_items = user_items
self._list = model.lists.mapper.class_ self._list = model.lists.mapper.class_
self.item_keys = model.item_keys self.item_keys = model.item_keys
self.filter_keys = model.filter_keys self.filter_keys = model.filter_keys
@ -115,19 +116,19 @@ class Parser(object):
elif k == 'list': elif k == 'list':
nickname, name = v.split(':', 1) nickname, name = v.split(':', 1)
if nickname: if nickname:
u = self._user.query.filter_by(nickname=nickname).first() u = self._user.query.filter_by(nickname=nickname).one()
else: 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: 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: else:
l = None l = None
if not l: if not l:
if not u: if not u:
q = (self._user.id == 0) q = (self._user_items.columns['user_id'] == 0)
else: else:
q = (self._user.id == u.id) q = (self._user_items.columns['user_id'] == u.id)
self._joins.append(self._model.users) self._joins.append(self._user_items)
elif l.type == 'smart': elif l.type == 'smart':
data = l._query data = l._query
q = self.parse_conditions(data.get('conditions', []), q = self.parse_conditions(data.get('conditions', []),
@ -235,4 +236,5 @@ class Parser(object):
# FIXME: group_by needed here to avoid # FIXME: group_by needed here to avoid
# duplicates due to joins. # duplicates due to joins.
qs = qs.group_by(self._model.id) qs = qs.group_by(self._model.id)
#print(qs)
return qs return qs

View file

@ -243,6 +243,8 @@ def upgrade_db(old, new=None):
if 'id' in m.data: if 'id' in m.data:
del m.data['id'] del m.data['id']
m.save() 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): def create_default_lists(user_id=None):
with db.session(): with db.session():

View file

@ -95,7 +95,7 @@ class User(db.Model):
del settings.list_cache[key] del settings.list_cache[key]
def clear_smart_list_cache(self): 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): for key in list(settings.list_cache):
if key in smart_lists: if key in smart_lists:
del settings.list_cache[key] del settings.list_cache[key]
@ -258,6 +258,7 @@ class List(db.Model):
state.db.session.commit() state.db.session.commit()
if self.user_id == settings.USER_ID: if self.user_id == settings.USER_ID:
Changelog.record(self.user, 'addlistitems', self.name, items) Changelog.record(self.user, 'addlistitems', self.name, items)
self.clear_smart_list_cache()
def remove_items(self, items): def remove_items(self, items):
from item.models import Item from item.models import Item
@ -305,10 +306,10 @@ class List(db.Model):
if key in settings.list_cache: if key in settings.list_cache:
value = settings.list_cache[key] value = settings.list_cache[key]
else: else:
from item.models import Item from item.models import Item, user_items
if self._query: if self._query:
data = self._query data = self._query
value = Parser(Item).find({'query': data}).count() value = Parser(Item, user_items).find({'query': data}).count()
else: else:
value = len(self.items) value = len(self.items)
settings.list_cache[key] = value settings.list_cache[key] = value