From ded8b2308413eee716f5ef7fcefca564a87a4575 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Wed, 5 Mar 2008 15:10:39 +0100 Subject: [PATCH] Handle session and timeout --- tryton/common/common.py | 3 +- tryton/gui/main.py | 12 ++--- tryton/gui/window/tree.py | 13 +++--- tryton/gui/window/view_form/model/record.py | 5 ++- tryton/gui/window/view_tree/view_tree.py | 11 ++--- tryton/gui/window/view_tree/view_tree_sc.py | 11 ++--- tryton/rpc.py | 50 ++++++++++++++------- 7 files changed, 64 insertions(+), 41 deletions(-) diff --git a/tryton/common/common.py b/tryton/common/common.py index cb1a86887f..f94fa9d14e 100644 --- a/tryton/common/common.py +++ b/tryton/common/common.py @@ -347,9 +347,10 @@ def ask(question, parent, visibility=True): response = win.run() parent.present() + res = entry.get_text() win.destroy() if response == gtk.RESPONSE_OK: - return entry.get_text() + return res else: return None diff --git a/tryton/gui/main.py b/tryton/gui/main.py index 28fec0003b..8a0b85bb09 100644 --- a/tryton/gui/main.py +++ b/tryton/gui/main.py @@ -534,9 +534,9 @@ class Main(object): self.notebook.set_current_page(page - 1) def sig_user_preferences(self, widget): - win = Preference(rpc.session.user, self.window) + win = Preference(rpc._USER, self.window) if win.run(): - rpc.session.context_reload() + rpc.context_reload() self.window.present() return True @@ -545,28 +545,28 @@ class Main(object): def sig_request_new(self, widget): return Window.create(None, 'res.request', False, - [('act_from', '=', rpc.session.user)], 'form', + [('act_from', '=', rpc._USER)], 'form', mode=['form', 'tree'], window=self.window, context={'active_test': False}) def sig_request_open(self, widget): ids = self.request_set()[0] return Window.create(False, 'res.request', ids, - [('act_to', '=', rpc.session.user), ('active', '=', True)], + [('act_to', '=', rpc._USER), ('active', '=', True)], 'form', mode=['tree', 'form'], window=self.window, context={'active_test': False}) def sig_request_wait(self, widget): ids = self.request_set()[0] return Window.create(False, 'res.request', ids, - [('act_from', '=', rpc.session.user), + [('act_from', '=', rpc._USER), ('state', '=', 'waiting'), ('active', '=', True)], 'form', mode=['tree', 'form'], window=self.window, context={'active_test': False}) def request_set(self): try: - ids, ids2 = rpc.session.rpc_exec_auth_try('/object', 'execute', + ids, ids2 = rpc.execute('object', 'execute', 'res.request', 'request_get') if len(ids): message = _('%s request(s)') % len(ids) diff --git a/tryton/gui/window/tree.py b/tryton/gui/window/tree.py index 0c1b91a1cc..2f93f11a1d 100644 --- a/tryton/gui/window/tree.py +++ b/tryton/gui/window/tree.py @@ -160,14 +160,15 @@ class Tree(SignalEvent): def menu_main_clicked(self, widget): if widget.get_active(): obj_id = widget.get_data('id') - + args = ('object', 'execute', self.model, + 'read', obj_id, [self.view['field_childs']]) try: - ids = rpc.execute('object', 'execute', self.model, - 'read', obj_id, [self.view['field_childs']])\ - [self.view['field_childs']] + ids = rpc.execute(*args)[self.view['field_childs']] except Exception, exception: - rpc.process_exception(exception, self.window) - return False + ids = rpc.process_exception(exception, self.window, *args)\ + [self.view['field_childs']] + if not ids: + return False self.tree_res.ids = ids self.tree_res.reload() diff --git a/tryton/gui/window/view_form/model/record.py b/tryton/gui/window/view_form/model/record.py index 0023cbcd6a..9e38c81031 100644 --- a/tryton/gui/window/view_form/model/record.py +++ b/tryton/gui/window/view_form/model/record.py @@ -92,10 +92,11 @@ class ModelRecord(SignalEvent): try: self.id = self.rpc.create(value, self.context_get()) except Exception, exception: - rpc.process_exception(exception, self.window, 'object', + self.id = rpc.process_exception(exception, self.window, 'object', 'execute', self.resource, 'create', value, self.context_get()) - return False + if not self.id: + return False else: if not self.is_modified(): return self.id diff --git a/tryton/gui/window/view_tree/view_tree.py b/tryton/gui/window/view_tree/view_tree.py index c278b3dabe..a094207efb 100644 --- a/tryton/gui/window/view_tree/view_tree.py +++ b/tryton/gui/window/view_tree/view_tree.py @@ -53,9 +53,10 @@ class ViewTreeModel(gtk.GenericTreeModel, gtk.TreeSortable): ctx = {} ctx.update(rpc.CONTEXT) ctx.update(self.context) + args = ('object', 'execute', self.view['model'], 'read', ids, fields, + ctx) try: - res_ids = rpc.execute('object', 'execute', - self.view['model'], 'read', ids, fields, ctx) + res_ids = rpc.execute(*args) except: res_ids = [] for obj_id in ids: @@ -321,9 +322,9 @@ class ViewTree(object): def reload(self): del self.model - self.model = ViewTreeModel(self.ids, self.view_info, - self.fields_order, self.fields, context=self.context, - pixbufs=self.pixbufs, treeview=self.view) + self.model = ViewTreeModel(self.ids, self.view_info, self.fields_order, + self.fields, context=self.context, pixbufs=self.pixbufs, + treeview=self.view) self.view.set_model(self.model) def widget_get(self): diff --git a/tryton/gui/window/view_tree/view_tree_sc.py b/tryton/gui/window/view_tree/view_tree_sc.py index 4e49d271f6..0f706a17b3 100644 --- a/tryton/gui/window/view_tree/view_tree_sc.py +++ b/tryton/gui/window/view_tree/view_tree_sc.py @@ -27,13 +27,14 @@ class ViewTreeSC(object): store = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING) user = rpc._USER + args = ('object', 'execute', 'ir.ui.view_sc', 'get_sc', user, + self.model, rpc.CONTEXT) try: - view_sc = rpc.execute('object', 'execute', - 'ir.ui.view_sc', 'get_sc', user, self.model, - rpc.CONTEXT) + view_sc = rpc.execute(*args) except Exception, exception: - rpc.process_exception(exception, self.window) - return + view_sc = rpc.process_exception(exception, self.window, *args) + if not view_sc: + return for shortcut in view_sc: num = store.append() store.set(num, 0, shortcut['res_id'], 1, shortcut['name'], diff --git a/tryton/rpc.py b/tryton/rpc.py index 11ec0ec803..57ed1d9025 100644 --- a/tryton/rpc.py +++ b/tryton/rpc.py @@ -9,7 +9,7 @@ import socket _SOCK = None _USER = 0 _USERNAME = '' -_PASSWORD = '' +_SESSION = '' _DATABASE = '' CONTEXT = {} _VIEW_CACHE = {} @@ -27,8 +27,7 @@ def db_list(host, port): _SOCK.send(('db', 'list')) res = _SOCK.receive() return res - except Exception, e: - print e + except: return None def db_exec(host, port, method, *args): @@ -43,14 +42,10 @@ def db_exec(host, port, method, *args): return res def login(username, password, host, port, database): - global _SOCK, _USER, _USERNAME, _PASSWORD, _DATABASE, _VIEW_CACHE, SECURE + global _SOCK, _USER, _USERNAME, _SESSION, _DATABASE, _VIEW_CACHE, SECURE if _SOCK: _SOCK.disconnect() _SOCK = None - _USER = 0 - _USERNAME = '' - _PASSWORD = '' - _DATABASE = '' _VIEW_CACHE = {} SECURE = False try: @@ -59,25 +54,33 @@ def login(username, password, host, port, database): _SOCK.send(('common', 'login', database, username, password)) res = _SOCK.receive() except socket.error: + _USER = 0 + _USERNAME = '' + _SESSION = '' + _DATABASE = '' return -1 if not res: + _USER = 0 + _USERNAME = '' + _SESSION = '' + _DATABASE = '' return -2 - _USER = res + _USER = res[0] _USERNAME = username - _PASSWORD = password + _SESSION = res[1] _DATABASE = database SECURE = _SOCK.ssl context_reload() return 1 def logout(): - global _SOCK, _USER, _USERNAME, _PASSWORD, _DATABASE, _VIEW_CACHE + global _SOCK, _USER, _USERNAME, _SESSION, _DATABASE, _VIEW_CACHE if _SOCK: _SOCK.disconnect() _SOCK = None _USER = 0 _USERNAME = '' - _PASSWORD = '' + _SESSION = '' _DATABASE = '' _VIEW_CACHE = {} @@ -107,7 +110,7 @@ def context_reload(): TIMEZONE = execute('common', 'timezone_get') def execute(obj, method, *args): - global _SOCK + global _SOCK, _DATABASE, _USER, _SESSION if not _SOCK: raise Exception('Not logged!') logging.getLogger('rpc.request').info(str((obj, method, args))) @@ -118,11 +121,11 @@ def execute(obj, method, *args): args = args[:] args = args + (_VIEW_CACHE[key][0],) try: - _SOCK.send((obj, method, _DATABASE, _USER, _PASSWORD) + args) + _SOCK.send((obj, method, _DATABASE, _USER, _SESSION) + args) result = _SOCK.receive() except socket.error: _SOCK.reconnect() - _SOCK.send((obj, method, _DATABASE, _USER, _PASSWORD) + args) + _SOCK.send((obj, method, _DATABASE, _USER, _SESSION) + args) result = _SOCK.receive() if key: if result is True and key in _VIEW_CACHE: @@ -132,7 +135,22 @@ def execute(obj, method, *args): return result def process_exception(exception, parent, obj='', method='', *args): - global _USERNAME, _DATABASE + global _USERNAME, _DATABASE, _SOCK + if str(exception.args[0]) == 'NotLogged': + while True: + password = common.ask(_('Password:'), parent, visibility=False) + if password is None: + break + res = login(_USERNAME, password, _SOCK.host, _SOCK.port, _DATABASE) + if res < 0: + continue + if obj and method: + try: + return execute(obj, method, *args) + except Exception, exception: + return process_exception(exception, parent, obj, + method, *args) + return type = 'error' data = str(exception.args[0]) description = data