[IMP] core: deprecate method cr.after()

Use the more convenient `cr.postcommit.add()` instead.

closes odoo/odoo#57043

X-original-commit: e9e37091a65d5ae2caa9eb6728abaffb7c9555a8
Related: odoo/enterprise#12928
Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
This commit is contained in:
Raphael Collet
2020-09-03 14:29:39 +00:00
parent e8f7cfd00a
commit 317765c8f3
6 changed files with 16 additions and 5 deletions
+1 -1
View File
@@ -59,10 +59,10 @@ class ImBus(models.Model):
# awakened and will fetch the notification in the bus table. If the
# transaction is not commited yet, there will be nothing to fetch,
# and the longpolling will return no notification.
@self.env.cr.postcommit.add
def notify():
with odoo.sql_db.db_connect('postgres').cursor() as cr:
cr.execute("notify imbus, %s", (json_dump(list(channels)),))
self._cr.after('commit', notify)
@api.model
def sendone(self, channel, message):
+3 -1
View File
@@ -30,6 +30,8 @@ def after_commit(func):
dbname = self.env.cr.dbname
context = self.env.context
uid = self.env.uid
@self.env.cr.postcommit.add
def called_after():
db_registry = registry(dbname)
with api.Environment.manage(), db_registry.cursor() as cr:
@@ -39,7 +41,7 @@ def after_commit(func):
except Exception as e:
_logger.warning("Could not sync record now: %s" % self)
_logger.exception(e)
self.env.cr.after('commit', called_after)
return wrapped
@contextmanager
+2 -1
View File
@@ -2281,12 +2281,13 @@ class MailThread(models.AbstractModel):
email_ids = emails.ids
dbname = self.env.cr.dbname
_context = self._context
@self.env.cr.postcommit.add
def send_notifications():
db_registry = registry(dbname)
with api.Environment.manage(), db_registry.cursor() as cr:
env = api.Environment(cr, SUPERUSER_ID, _context)
env['mail.mail'].browse(email_ids).send()
self._cr.after('commit', send_notifications)
else:
emails.send()
@@ -32,6 +32,8 @@ def after_commit(func):
dbname = self.env.cr.dbname
context = self.env.context
uid = self.env.uid
@self.env.cr.postcommit.add
def called_after():
db_registry = registry(dbname)
with api.Environment.manage(), db_registry.cursor() as cr:
@@ -41,7 +43,7 @@ def after_commit(func):
except Exception as e:
_logger.warning("Could not sync record now: %s" % self)
_logger.exception(e)
self.env.cr.after('commit', called_after)
return wrapped
@contextmanager
+2 -1
View File
@@ -19,7 +19,8 @@ def uninstall_hook(cr, registry):
('name', '=', 'website_id'),
('model', '=', 'res.config.settings'),
]).unlink()
cr.after('commit', partial(rem_website_id_null, cr.dbname))
cr.postcommit.add(partial(rem_website_id_null, cr.dbname))
def post_init_hook(cr, registry):
+5
View File
@@ -14,6 +14,7 @@ import itertools
import logging
import time
import uuid
import warnings
from decorator import decorator
import psycopg2
@@ -420,6 +421,10 @@ class Cursor(BaseCursor):
back or committed independently. You may consider the use of a
dedicated temporary cursor to do some database operation.
"""
warnings.warn(
"Cursor.after() is deprecated, use Cursor.postcommit.add() instead.",
DeprecationWarning,
)
if event == 'commit':
self.postcommit.add(func)
elif event == 'rollback':