From 947adbb912c104927d79b67c4b3468c2b83354a0 Mon Sep 17 00:00:00 2001 From: dufresnedavid Date: Wed, 8 Jun 2016 14:54:35 -0400 Subject: [PATCH 1/5] [8.0] Convert email server actions to email.template --- .../migrations/8.0.1.1/post-migration.py | 69 +++++++++++++++++++ .../migrations/8.0.1.1/pre-migration.py | 3 + 2 files changed, 72 insertions(+) create mode 100644 addons/email_template/migrations/8.0.1.1/post-migration.py diff --git a/addons/email_template/migrations/8.0.1.1/post-migration.py b/addons/email_template/migrations/8.0.1.1/post-migration.py new file mode 100644 index 000000000000..fa57ea680963 --- /dev/null +++ b/addons/email_template/migrations/8.0.1.1/post-migration.py @@ -0,0 +1,69 @@ +# -*- coding: utf-8 -*- +# © 2016 Savoir-faire Linux () +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +import logging + +from openerp.openupgrade import openupgrade + +from openerp import SUPERUSER_ID +from openerp.api import Environment + + +_logger = logging.getLogger(__name__) + + +@openupgrade.migrate() +def migrate(cr, version): + if not version: + return + env = Environment(cr, SUPERUSER_ID, {}) + convert_action_mail_server_email(env) + + +def convert_action_mail_server_email(env): + def convert_body(body): + return body.replace("[[", "${").replace("]]", "}") + + def convert_subject(subject): + subject = subject.strip() + if subject.startswith('object.'): + subject = "${%s}" % subject + return subject + + actions = env['ir.actions.server'].search([ + ('state', '=', 'email'), + ('template_id', '=', False), + ]) + + _logger.info( + 'Transfering existing ir.actions.server emails to ' + 'email templates.' + ) + + for action in actions: + + env.cr.execute( + """ + SELECT email, old_subject, message + FROM ir_act_server + WHERE id = %s + """, (action.id, )) + + (email, subject, message) = env.cr.fetchone() + + email_template = env['email.template'].create({ + 'name': action.name, + 'model_id': action.model_id.id, + 'email_from': convert_subject(email), + 'subject': convert_body(subject), + 'body_html': convert_body(message), + }) + + action.template_id = email_template.id + + openupgrade.drop_columns(env.cr, [ + ('ir_act_server', 'email'), + ('ir_act_server', 'old_subject'), + ('ir_act_server', 'message'), + ]) diff --git a/addons/email_template/migrations/8.0.1.1/pre-migration.py b/addons/email_template/migrations/8.0.1.1/pre-migration.py index 83214b4f687d..a323b6041922 100644 --- a/addons/email_template/migrations/8.0.1.1/pre-migration.py +++ b/addons/email_template/migrations/8.0.1.1/pre-migration.py @@ -24,6 +24,9 @@ column_renames = { 'email_template': [ ('email_recipients', 'partner_to'), + ], + 'ir_act_server': [ + ('subject', 'old_subject'), ] } From 3f0c145c6b15be28a17759199b2ea7594c8e4aee Mon Sep 17 00:00:00 2001 From: dufresnedavid Date: Thu, 9 Jun 2016 09:43:25 -0400 Subject: [PATCH 2/5] Remove column drops --- addons/email_template/migrations/8.0.1.1/post-migration.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/addons/email_template/migrations/8.0.1.1/post-migration.py b/addons/email_template/migrations/8.0.1.1/post-migration.py index fa57ea680963..a51ee5f3d529 100644 --- a/addons/email_template/migrations/8.0.1.1/post-migration.py +++ b/addons/email_template/migrations/8.0.1.1/post-migration.py @@ -61,9 +61,3 @@ def convert_subject(subject): }) action.template_id = email_template.id - - openupgrade.drop_columns(env.cr, [ - ('ir_act_server', 'email'), - ('ir_act_server', 'old_subject'), - ('ir_act_server', 'message'), - ]) From f58faa61c25b96e4f75a2849cc9faf65c7d7792b Mon Sep 17 00:00:00 2001 From: dufresnedavid Date: Thu, 9 Jun 2016 10:08:10 -0400 Subject: [PATCH 3/5] Rename columns with prefix openupgrade_legacy --- addons/email_template/migrations/8.0.1.1/post-migration.py | 4 +++- addons/email_template/migrations/8.0.1.1/pre-migration.py | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/addons/email_template/migrations/8.0.1.1/post-migration.py b/addons/email_template/migrations/8.0.1.1/post-migration.py index a51ee5f3d529..56304537c4a6 100644 --- a/addons/email_template/migrations/8.0.1.1/post-migration.py +++ b/addons/email_template/migrations/8.0.1.1/post-migration.py @@ -45,7 +45,9 @@ def convert_subject(subject): env.cr.execute( """ - SELECT email, old_subject, message + SELECT openupgrade_legacy_email + , openupgrade_legacy_subject + , openupgrade_legacy_message FROM ir_act_server WHERE id = %s """, (action.id, )) diff --git a/addons/email_template/migrations/8.0.1.1/pre-migration.py b/addons/email_template/migrations/8.0.1.1/pre-migration.py index a323b6041922..5e45c6d0216d 100644 --- a/addons/email_template/migrations/8.0.1.1/pre-migration.py +++ b/addons/email_template/migrations/8.0.1.1/pre-migration.py @@ -26,7 +26,7 @@ ('email_recipients', 'partner_to'), ], 'ir_act_server': [ - ('subject', 'old_subject'), + ('subject', 'openupgrade_legacy_subject'), ] } From 458ce021b73d24d0754ea74dd6bbe7bf94719c26 Mon Sep 17 00:00:00 2001 From: dufresnedavid Date: Mon, 13 Jun 2016 11:43:15 -0400 Subject: [PATCH 4/5] Use openupgrade.get_legacy_name --- addons/email_template/migrations/8.0.1.1/post-migration.py | 7 ++++++- addons/email_template/migrations/8.0.1.1/pre-migration.py | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/addons/email_template/migrations/8.0.1.1/post-migration.py b/addons/email_template/migrations/8.0.1.1/post-migration.py index 56304537c4a6..aa288ca99414 100644 --- a/addons/email_template/migrations/8.0.1.1/post-migration.py +++ b/addons/email_template/migrations/8.0.1.1/post-migration.py @@ -50,7 +50,12 @@ def convert_subject(subject): , openupgrade_legacy_message FROM ir_act_server WHERE id = %s - """, (action.id, )) + """, ( + openupgrade.get_legacy_name('email'), + openupgrade.get_legacy_name('subject'), + openupgrade.get_legacy_name('message'), + action.id, + )) (email, subject, message) = env.cr.fetchone() diff --git a/addons/email_template/migrations/8.0.1.1/pre-migration.py b/addons/email_template/migrations/8.0.1.1/pre-migration.py index 5e45c6d0216d..00c8f78d6f93 100644 --- a/addons/email_template/migrations/8.0.1.1/pre-migration.py +++ b/addons/email_template/migrations/8.0.1.1/pre-migration.py @@ -26,7 +26,7 @@ ('email_recipients', 'partner_to'), ], 'ir_act_server': [ - ('subject', 'openupgrade_legacy_subject'), + ('subject', None), ] } From 821899bdd542cd07be7571f13db8099b8518e94a Mon Sep 17 00:00:00 2001 From: dufresnedavid Date: Mon, 13 Jun 2016 12:29:43 -0400 Subject: [PATCH 5/5] Fix migration script --- .../migrations/8.0.1.1/post-migration.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/addons/email_template/migrations/8.0.1.1/post-migration.py b/addons/email_template/migrations/8.0.1.1/post-migration.py index aa288ca99414..d72223b8ab8d 100644 --- a/addons/email_template/migrations/8.0.1.1/post-migration.py +++ b/addons/email_template/migrations/8.0.1.1/post-migration.py @@ -4,6 +4,7 @@ import logging +from psycopg2.extensions import AsIs from openerp.openupgrade import openupgrade from openerp import SUPERUSER_ID @@ -43,19 +44,14 @@ def convert_subject(subject): for action in actions: + subject_column = openupgrade.get_legacy_name('subject') + env.cr.execute( """ - SELECT openupgrade_legacy_email - , openupgrade_legacy_subject - , openupgrade_legacy_message + SELECT email, %s, message FROM ir_act_server WHERE id = %s - """, ( - openupgrade.get_legacy_name('email'), - openupgrade.get_legacy_name('subject'), - openupgrade.get_legacy_name('message'), - action.id, - )) + """, (AsIs(subject_column), action.id, )) (email, subject, message) = env.cr.fetchone()