diff --git a/addons/email_template/__init__.py b/addons/email_template/__init__.py index d1cf5b28431..3f091fd8645 100644 --- a/addons/email_template/__init__.py +++ b/addons/email_template/__init__.py @@ -22,5 +22,6 @@ import email_template import wizard import res_partner +import ir_actions # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/addons/email_template/__openerp__.py b/addons/email_template/__openerp__.py index 1dc3baaf63c..b7977660134 100644 --- a/addons/email_template/__openerp__.py +++ b/addons/email_template/__openerp__.py @@ -59,6 +59,7 @@ campaigns on any OpenERP document. 'wizard/email_template_preview_view.xml', 'email_template_view.xml', 'res_partner_view.xml', + 'ir_actions_view.xml', 'wizard/mail_compose_message_view.xml', 'security/ir.model.access.csv' ], diff --git a/addons/email_template/ir_actions.py b/addons/email_template/ir_actions.py new file mode 100644 index 00000000000..ae2ce4f2f82 --- /dev/null +++ b/addons/email_template/ir_actions.py @@ -0,0 +1,73 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Business Applications +# Copyright (c) 2014 OpenERP S.A. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## + +from openerp.osv import fields, osv + + +class actions_server(osv.Model): + """ Add email option in server actions. """ + _name = 'ir.actions.server' + _inherit = ['ir.actions.server'] + + def _get_states(self, cr, uid, context=None): + res = super(actions_server, self)._get_states(cr, uid, context=context) + res.insert(0, ('email', 'Send Email')) + return res + + _columns = { + 'email_from': fields.char('From', readonly=True, + help="Sender address; define the template to see its value. If not set, the default " + "value will be the author's email alias if configured, or email address."), + 'email_to': fields.char('To (Emails)', readonly=True, + help="Comma-separated recipient addresses; define the template to see its value"), + 'partner_to': fields.char('To (Partners)', readonly=True, + help="Comma-separated ids of recipient partners; define the template to see its value"), + 'subject': fields.char('Subject', readonly=True, + help="Email subject; define the template to see its value"), + 'body_html': fields.text('Body', readonly=True, + help="Rich-text/HTML version of the message; define the template to see its value"), + 'template_id': fields.many2one('email.template', 'Email Template', ondelete='set null', + help="Define the email template to use for the email to send.") + } + + def on_change_template_id(self, cr, uid, ids, template_id, context=None): + """ - mass_mailing: we cannot render, so return the template values + - normal mode: return rendered values """ + if template_id: + fields = ['subject', 'body_html', 'email_from', 'email_to', 'partner_to', 'email_cc', 'reply_to', 'attachment_ids'] + template_values = self.pool.get('email.template').read(cr, uid, template_id, fields, context) + values = dict((field, template_values[field]) for field in fields if template_values.get(field)) + if not values.get('email_from'): + return {'warning': {'title': 'Incomplete template', 'message': 'Your template should define email_from'}, 'value': values} + else: + values = self.default_get(cr, uid, ['subject', 'body_html', 'email_from', 'email_to', 'partner_to'], context=context) + + return {'value': values} + + def run_action_email(self, cr, uid, action, eval_context=None, context=None): + if not action.template_id or not context.get('active_id'): + return False + self.pool['email.template'].send_mail(cr, uid, action.template_id.id, context.get('active_id'), + force_send=False, raise_exception=False, context=context) + return False + + +# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/addons/email_template/ir_actions_view.xml b/addons/email_template/ir_actions_view.xml new file mode 100644 index 00000000000..f673fda687d --- /dev/null +++ b/addons/email_template/ir_actions_view.xml @@ -0,0 +1,49 @@ + + + + + + ir.actions.server.form + ir.actions.server + + + + +

+ Please set the Base Model before setting the action details. +

+ + +

+ Choose a template to display its values. +

+

+ The values displayed hereunder are informative. When sending the email, the values + will be taken from the email template. +

+
+ + +
+
+
+
+ +
+
diff --git a/addons/email_template/tests/__init__.py b/addons/email_template/tests/__init__.py index d63c5634cc3..9418985b06c 100644 --- a/addons/email_template/tests/__init__.py +++ b/addons/email_template/tests/__init__.py @@ -18,10 +18,11 @@ # along with this program. If not, see . # ############################################################################## -from . import test_mail +from . import test_mail, test_ir_actions checks = [ test_mail, + test_ir_actions, ] # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: \ No newline at end of file diff --git a/addons/email_template/tests/test_ir_actions.py b/addons/email_template/tests/test_ir_actions.py new file mode 100644 index 00000000000..d323ee36c1f --- /dev/null +++ b/addons/email_template/tests/test_ir_actions.py @@ -0,0 +1,55 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Business Applications +# Copyright (c) 2013-TODAY OpenERP S.A. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## + +from openerp.addons.base.tests.test_ir_actions import TestServerActionsBase + + +class TestServerActionsEmail(TestServerActionsBase): + + def test_00_state_email(self): + """ Test ir.actions.server email type """ + cr, uid = self.cr, self.uid + + # create email_template + template_id = self.registry('email.template').create(cr, uid, { + 'name': 'TestTemplate', + 'email_from': 'myself@example.com', + 'email_to': 'brigitte@example.com', + 'partner_to': '[%s]' % self.test_partner_id, + 'model_id': self.res_partner_model_id, + 'subject': 'About ${object.name}', + 'body_html': '

Dear ${object.name}, your parent is ${object.parent_id and object.parent_id.name or "False"}

', + }) + + self.ir_actions_server.write(cr, uid, self.act_id, { + 'state': 'email', + 'template_id': template_id, + }) + run_res = self.ir_actions_server.run(cr, uid, [self.act_id], context=self.context) + self.assertFalse(run_res, 'ir_actions_server: email server action correctly finished should return False') + + # check an email is waiting for sending + mail_ids = self.registry('mail.mail').search(cr, uid, [('subject', '=', 'About TestingPartner')]) + self.assertEqual(len(mail_ids), 1, 'ir_actions_server: TODO') + # check email content + mail = self.registry('mail.mail').browse(cr, uid, mail_ids[0]) + self.assertEqual(mail.body, '

Dear TestingPartner, your parent is False

', + 'ir_actions_server: TODO')