if not password:
return False
user_id = False
- cr = self.pool.db.cursor()
+ cr = self.pool.get_cursor()
try:
# autocommit: our single update request will be performed atomically.
# (In this way, there is no opportunity to have two transactions
# Successfully logged in as admin!
# Attempt to guess the web base url...
if user_agent_env and user_agent_env.get('base_location'):
- cr = self.pool.db.cursor()
+ cr = self.pool.get_cursor()
try:
base = user_agent_env['base_location']
ICP = self.pool['ir.config_parameter']
raise openerp.exceptions.AccessDenied()
if self._uid_cache.get(db, {}).get(uid) == passwd:
return
- cr = self.pool.db.cursor()
+ cr = self.pool.get_cursor()
try:
self.check_credentials(cr, uid, passwd)
if self._uid_cache.has_key(db):
return openerp.modules.registry.RegistryManager.get(DB)[model]
def cursor():
- return openerp.modules.registry.RegistryManager.get(DB).db.cursor()
+ return openerp.modules.registry.RegistryManager.get(DB).get_cursor()
def drop_sequence(code):
return openerp.modules.registry.RegistryManager.get(DB)[model]
def cursor():
- return openerp.modules.registry.RegistryManager.get(DB).db.cursor()
+ return openerp.modules.registry.RegistryManager.get(DB).get_cursor()
def get_module(module_name):
registry = openerp.modules.registry.RegistryManager.get(DB)
fileformat = os.path.splitext(config["translate_out"])[-1][1:].lower()
buf = file(config["translate_out"], "w")
registry = openerp.modules.registry.RegistryManager.new(dbname)
- cr = registry.db.cursor()
+ cr = registry.get_cursor()
openerp.tools.trans_export(config["language"],
config["translate_modules"] or ["all"], buf, fileformat, cr)
cr.close()
dbname = config['db_name']
registry = openerp.modules.registry.RegistryManager.new(dbname)
- cr = registry.db.cursor()
+ cr = registry.get_cursor()
openerp.tools.trans_load( cr, config["translate_in"], config["language"],
context=context)
cr.commit()
# Test cursors
self._cr = openerp.tests.common.acquire_test_cursor(self.session_id)
if not self._cr:
- self._cr = self.registry.db.cursor()
+ self._cr = self.registry.get_cursor()
return self._cr
def __enter__(self):
# Useful only in a multi-process context.
self._any_cache_cleared = False
- cr = self.db.cursor()
+ cr = self.get_cursor()
has_unaccent = openerp.modules.db.has_unaccent(cr)
if openerp.tools.config['unaccent'] and not has_unaccent:
_logger.warning("The option --unaccent was given but no unaccent() function was found in database.")
r, c)
return r, c
+ def get_cursor(self):
+ """ Return a new cursor for the database. """
+ return self.db.cursor()
+
@contextmanager
def cursor(self, auto_commit=True):
- cr = self.db.cursor()
+ """ Manage a new cursor; commit, rollback and closing are automatic. """
+ cr = self.get_cursor()
try:
yield cr
if auto_commit:
# Yeah, crazy.
registry = cls.registries[db_name]
- cr = registry.db.cursor()
+ cr = registry.get_cursor()
try:
registry.do_parent_store(cr)
cr.commit()
changed = False
if openerp.multi_process and db_name in cls.registries:
registry = cls.get(db_name)
- cr = registry.db.cursor()
+ cr = registry.get_cursor()
try:
cr.execute("""
SELECT base_registry_signaling.last_value,
registry = cls.get(db_name)
if registry.any_cache_cleared():
_logger.info("At least one model cache has been cleared, signaling through the database.")
- cr = registry.db.cursor()
+ cr = registry.get_cursor()
r = 1
try:
cr.execute("select nextval('base_cache_signaling')")
if openerp.multi_process and db_name in cls.registries:
_logger.info("Registry changed, signaling through the database")
registry = cls.get(db_name)
- cr = registry.db.cursor()
+ cr = registry.get_cursor()
r = 1
try:
cr.execute("select nextval('base_registry_signaling')")
def execute_kw(db, uid, obj, method, args, kw=None):
return execute(db, uid, obj, method, *args, **kw or {})
-@contextmanager
-def closing_cr_and_commit(cr):
- try:
- yield cr
- cr.commit()
- except Exception:
- cr.rollback()
- raise
- finally:
- cr.close()
-
@check
def execute(db, uid, obj, method, *args, **kw):
threading.currentThread().dbname = db
- with closing_cr_and_commit(openerp.registry(db).db.cursor()) as cr:
+ with openerp.registry(db).cursor() as cr:
if method.startswith('_'):
raise except_orm('Access Denied', 'Private methods (such as %s) cannot be called remotely.' % (method,))
res = execute_cr(cr, uid, obj, method, *args, **kw)
@check
def exec_workflow(db, uid, obj, signal, *args):
- with closing_cr_and_commit(openerp.registry(db).db.cursor()) as cr:
+ with openerp.registry(db).cursor() as cr:
return exec_workflow_cr(cr, uid, obj, signal, *args)
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
self_reports[id] = {'uid': uid, 'result': False, 'state': False, 'exception': None}
- cr = openerp.registry(db).db.cursor()
+ cr = openerp.registry(db).get_cursor()
try:
result, format = openerp.report.render_report(cr, uid, ids, object, datas, context)
if not result:
self_reports[id] = {'uid': uid, 'result': False, 'state': False, 'exception': None}
def go(id, uid, ids, datas, context):
- cr = openerp.registry(db).db.cursor()
+ cr = openerp.registry(db).get_cursor()
try:
result, format = openerp.report.render_report(cr, uid, ids, object, datas, context)
if not result:
"""
def cursor(self):
- return self.registry.db.cursor()
+ return self.registry.get_cursor()
def ref(self, xid):
""" Returns database ID corresponding to a given identifier.
@classmethod
def setUpClass(cls):
cls.registry = openerp.modules.registry.RegistryManager.get(DB)
- cls.cr = cls.registry.db.cursor()
+ cls.cr = cls.registry.get_cursor()
cls.uid = openerp.SUPERUSER_ID
@classmethod
if not cr:
db_name = getattr(threading.currentThread(), 'dbname', None)
if db_name:
- local_cr = cr = openerp.registry(db_name).db.cursor()
+ local_cr = cr = openerp.registry(db_name).get_cursor()
else:
raise Exception("No database cursor found, please pass one explicitly")
xs = []
ir_module_module = registry.get('ir.module.module')
- cr = registry.db.cursor() # TODO context manager
- try:
+ with registry.cursor() as cr:
ids = ir_module_module.search(cr, openerp.SUPERUSER_ID, [], {})
xs = ir_module_module.read(cr, openerp.SUPERUSER_ID, ids, [], {})
- finally:
- cr.close()
if xs:
print "Modules (database `%s`):" % (args.database,)
registry = openerp.modules.registry.RegistryManager.get(
args.database, update_module=False)
model = registry[args.model]
- cr = registry.db.cursor() # TODO context manager
field_names = [args.field] if args.field else []
if args.short:
# ignore --field
field_names = ['name']
- try:
+ with registry.cursor() as cr:
xs = model.read(cr, 1, args.id, field_names, {})
- finally:
- cr.close()
if xs:
print "Records (model `%s`, database `%s`):" % (args.model, args.database)
args.database, update_module=False)
ir_module_module = registry.get('ir.module.module')
- cr = registry.db.cursor() # TODO context manager
- try:
+ with registry.cursor() as cr:
ids = ir_module_module.search(cr, openerp.SUPERUSER_ID, [('name', 'in', args.module), ('state', '=', 'installed')], {})
if len(ids) == len(args.module):
ir_module_module.button_immediate_uninstall(cr, openerp.SUPERUSER_ID, ids, {})
else:
print "At least one module not found (database `%s`)." % (args.database,)
- finally:
- cr.close()
def add_parser(subparsers):
parser = subparsers.add_parser('uninstall',