From cfaadc0433af25c6a224b40739ecc05678284536 Mon Sep 17 00:00:00 2001 From: Fabien Meghazi Date: Tue, 28 Jan 2014 22:15:50 +0100 Subject: [PATCH] [ADD] http#ensure_db() A helper that allows to force a db in the controller context otherwise redirect to database selector Used for /web, /web/login and auth_* modules bzr revid: fme@openerp.com-20140128211550-vmhy5nu0qbi33qh5 --- openerp/http.py | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/openerp/http.py b/openerp/http.py index 23c677f0620..4ebc9b6ecc2 100644 --- a/openerp/http.py +++ b/openerp/http.py @@ -72,6 +72,28 @@ def redirect_with_hash(url, code=303): return werkzeug.utils.redirect(url, code) return "" % url +def ensure_db(with_registry=False, redirect='/web/database/selector'): + db = request.params.get('db') + # if db not provided, use the session one + if not db: + db = request.session.db + + # if no database provided and no database in session, use monodb + if not db: + db = db_monodb(request.httprequest) + + # if no db can be found til here, send to the database selector + # the database selector will redirect to database manager if needed + if not db: + werkzeug.exceptions.abort(werkzeug.utils.redirect(redirect, 303)) + + # always switch the session to the computed db + if db != request.session.db: + request.session.logout() + request.session.db = db + + if with_registry: + request.disable_db = False class WebRequest(object): """ Parent class for all OpenERP Web request types, mostly deals with @@ -667,9 +689,10 @@ class OpenERPSession(werkzeug.contrib.sessions.Session): raise SessionExpiredException("Session expired") security.check(self.db, self.uid, self.password) - def logout(self): + def logout(self, keep_db=False): for k in self.keys(): - del self[k] + if not (keep_db and k == 'db'): + del self[k] self._default_values() def _default_values(self):