From d7528f634feccaa28b20265dd3e72cded80bfcbb Mon Sep 17 00:00:00 2001 From: j Date: Thu, 25 Feb 2016 13:09:59 +0530 Subject: [PATCH] avoid extra lookup --- oml/nodes.py | 11 +++++------ oml/user/models.py | 22 +++++++++++----------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/oml/nodes.py b/oml/nodes.py index 88d1aaf..d2a0519 100644 --- a/oml/nodes.py +++ b/oml/nodes.py @@ -445,15 +445,10 @@ class Nodes(Thread): else: self._call(*args) - def cleanup(self): if not state.shutdown and self._local: self._local.cleanup() - def pull(self): - if not self._pulling: - self.queue('pull') - def queue(self, *args): self._q.put(list(args)) @@ -493,6 +488,10 @@ class Nodes(Thread): if send_response: self._nodes[user_id].send_response() + def pull(self): + if not self._pulling: + self.queue('pull') + def _pull(self): if state.activity and state.activity.get('activity') == 'import': return @@ -503,7 +502,7 @@ class Nodes(Thread): with db.session(): from user.models import User for u in User.query.filter(User.id!=settings.USER_ID).filter_by(peered=True).all(): - users.append(u.json()) + users.append(u.json(['id', 'index', 'name'])) users.sort(key=user_sort_key) for u in users: if state.shutdown: diff --git a/oml/user/models.py b/oml/user/models.py index d0b4b24..df82261 100644 --- a/oml/user/models.py +++ b/oml/user/models.py @@ -86,18 +86,18 @@ class User(db.Model): if self.pending: j['pending'] = self.pending j['peered'] = self.peered - j['online'] = self.is_online() - j['nickname'] = self.info.get('nickname') - j['username'] = self.info.get('username') + if not keys or 'online' in keys: + j['online'] = self.is_online() j['name'] = self.name - if self.id == settings.USER_ID: - j['username'] = settings.preferences['username'] - j['contact'] = settings.preferences['contact'] - elif self.id in state.peers: - peer = state.peers[self.id] - for key in ('username', 'contact'): - if key in peer.info: - j[key] = peer.info[key] + if not keys or 'username' in keys or 'contact' in keys: + if self.id == settings.USER_ID: + j['username'] = settings.preferences['username'] + j['contact'] = settings.preferences['contact'] + elif self.id in state.peers: + peer = state.peers[self.id] + for key in ('username', 'contact'): + if key in peer.info: + j[key] = peer.info[key] if keys: for k in set(j) - set(keys): del j[k]