forked from OCA/OpenUpgrade
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request OCA#505 from savoirfairelinux/8.0_email_template
[8.0] Convert email server actions to email.template
- Loading branch information
Showing
2 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
addons/email_template/migrations/8.0.1.1/post-migration.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# -*- coding: utf-8 -*- | ||
# © 2016 Savoir-faire Linux (<https://www.savoirfairelinux.com>) | ||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). | ||
|
||
import logging | ||
|
||
from psycopg2.extensions import AsIs | ||
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: | ||
|
||
subject_column = openupgrade.get_legacy_name('subject') | ||
|
||
env.cr.execute( | ||
""" | ||
SELECT email, %s, message | ||
FROM ir_act_server | ||
WHERE id = %s | ||
""", (AsIs(subject_column), 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters