diff options
| author | Vidhin Mehta <vme@tinyerp.com> | 2013-08-23 10:36:02 +0530 |
|---|---|---|
| committer | Vidhin Mehta <vme@tinyerp.com> | 2013-08-23 10:36:02 +0530 |
| commit | 75ddd5ccbec73c4a671b6a74efaa3d41dc2ea2db (patch) | |
| tree | 50ed3620df15e8533760f5d47e754a1d39c1e856 /addons/email_template | |
| parent | 42f27a15275effd9c43dfe99e32c395ca6b3fd8a (diff) | |
| parent | a7f4b6aaa0359d12046e81183417c24f093cd7c3 (diff) | |
[MERGE]Trunk.
bzr revid: vme@tinyerp.com-20130823050602-33bqv07e7g8dhv7x
Diffstat (limited to 'addons/email_template')
| -rw-r--r-- | addons/email_template/__init__.py | 1 | ||||
| -rw-r--r-- | addons/email_template/__openerp__.py | 1 | ||||
| -rw-r--r-- | addons/email_template/doc/changelog.rst | 12 | ||||
| -rw-r--r-- | addons/email_template/doc/index.rst | 13 | ||||
| -rw-r--r-- | addons/email_template/email_template.py | 23 | ||||
| -rw-r--r-- | addons/email_template/ir_actions.py | 86 | ||||
| -rw-r--r-- | addons/email_template/ir_actions_view.xml | 49 | ||||
| -rw-r--r-- | addons/email_template/tests/__init__.py | 3 | ||||
| -rw-r--r-- | addons/email_template/tests/test_ir_actions.py | 55 | ||||
| -rw-r--r-- | addons/email_template/wizard/mail_compose_message.py | 23 |
10 files changed, 246 insertions, 20 deletions
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/doc/changelog.rst b/addons/email_template/doc/changelog.rst new file mode 100644 index 00000000000..77e342c920a --- /dev/null +++ b/addons/email_template/doc/changelog.rst @@ -0,0 +1,12 @@ +.. _changelog: + +Changelog +========= + +`trunk (saas-2)` +---------------- + + - ``mail.compose.message``: added support of ``mail_server_id`` from template + - Server action update + + - added `email` server action type, now entirely based on email templates.
\ No newline at end of file diff --git a/addons/email_template/doc/index.rst b/addons/email_template/doc/index.rst new file mode 100644 index 00000000000..58cf9413601 --- /dev/null +++ b/addons/email_template/doc/index.rst @@ -0,0 +1,13 @@ +Email Template module documentation +=================================== + +Email Template documentation topics +''''''''''''''''''''''''''''''''''' + +Changelog +''''''''' + +.. toctree:: + :maxdepth: 1 + + changelog.rst diff --git a/addons/email_template/email_template.py b/addons/email_template/email_template.py index 2a7990e301b..2e3405ed079 100644 --- a/addons/email_template/email_template.py +++ b/addons/email_template/email_template.py @@ -24,8 +24,8 @@ import base64 import logging import openerp +from openerp import SUPERUSER_ID from openerp.osv import osv, fields -from openerp.osv import fields from openerp import tools from openerp.tools.translate import _ from urllib import urlencode, quote as quote @@ -191,7 +191,6 @@ class email_template(osv.osv): } def create_action(self, cr, uid, ids, context=None): - vals = {} action_obj = self.pool.get('ir.actions.act_window') data_obj = self.pool.get('ir.model.data') for template in self.browse(cr, uid, ids, context=context): @@ -199,7 +198,7 @@ class email_template(osv.osv): model_data_id = data_obj._get_id(cr, uid, 'mail', 'email_compose_message_wizard_form') res_id = data_obj.browse(cr, uid, model_data_id, context=context).res_id button_name = _('Send Mail (%s)') % template.name - vals['ref_ir_act_window'] = action_obj.create(cr, uid, { + act_id = action_obj.create(cr, SUPERUSER_ID, { 'name': button_name, 'type': 'ir.actions.act_window', 'res_model': 'mail.compose.message', @@ -211,27 +210,29 @@ class email_template(osv.osv): 'target': 'new', 'auto_refresh':1 }, context) - vals['ref_ir_value'] = self.pool.get('ir.values').create(cr, uid, { + ir_values_id = self.pool.get('ir.values').create(cr, SUPERUSER_ID, { 'name': button_name, 'model': src_obj, 'key2': 'client_action_multi', - 'value': "ir.actions.act_window," + str(vals['ref_ir_act_window']), + 'value': "ir.actions.act_window,%s" % act_id, 'object': True, }, context) - self.write(cr, uid, ids, { - 'ref_ir_act_window': vals.get('ref_ir_act_window',False), - 'ref_ir_value': vals.get('ref_ir_value',False), - }, context) + + template.write({ + 'ref_ir_act_window': act_id, + 'ref_ir_value': ir_values_id, + }) + return True def unlink_action(self, cr, uid, ids, context=None): for template in self.browse(cr, uid, ids, context=context): try: if template.ref_ir_act_window: - self.pool.get('ir.actions.act_window').unlink(cr, uid, template.ref_ir_act_window.id, context) + self.pool.get('ir.actions.act_window').unlink(cr, SUPERUSER_ID, template.ref_ir_act_window.id, context) if template.ref_ir_value: ir_values_obj = self.pool.get('ir.values') - ir_values_obj.unlink(cr, uid, template.ref_ir_value.id, context) + ir_values_obj.unlink(cr, SUPERUSER_ID, template.ref_ir_value.id, context) except Exception: raise osv.except_osv(_("Warning"), _("Deletion of the action record failed.")) return True diff --git a/addons/email_template/ir_actions.py b/addons/email_template/ir_actions.py new file mode 100644 index 00000000000..bd860785da4 --- /dev/null +++ b/addons/email_template/ir_actions.py @@ -0,0 +1,86 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Business Applications +# Copyright (c) 2013 OpenERP S.A. <http://www.openerp.com> +# +# 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 <http://www.gnu.org/licenses/>. +# +############################################################################## + +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', + 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)', + help="Comma-separated recipient addresses; define the template to see its value"), + 'partner_to': fields.char('To (Partners)', + help="Comma-separated ids of recipient partners; define the template to see its value"), + 'subject': fields.char('Subject', + help="Email subject; define the template to see its value"), + 'body_html': fields.text('Body', + 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): + """ Render the raw template in the server action fields. """ + 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 create(self, cr, uid, values, context=None): + if values.get('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, values.get('template_id'), fields, context) + values.update(dict((field, template_values[field]) for field in fields if template_values.get(field))) + return super(actions_server, self).create(cr, uid, values, context=context) + + def write(self, cr, uid, ids, values, context=None): + if values.get('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, values.get('template_id'), fields, context) + values.update(dict((field, template_values[field]) for field in fields if template_values.get(field))) + return super(actions_server, self).write(cr, uid, ids, values, context=context) + + 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..fae9c364312 --- /dev/null +++ b/addons/email_template/ir_actions_view.xml @@ -0,0 +1,49 @@ +<?xml version="1.0" encoding="utf-8"?> +<openerp> + <data> + + <record model="ir.ui.view" id="view_server_action_form_template"> + <field name="name">ir.actions.server.form</field> + <field name="model">ir.actions.server</field> + <field name="inherit_id" ref="base.view_server_action_form"/> + <field name="arch" type="xml"> + <xpath expr="//page[@name='code']" position="after"> + <page string="Email" autofocus="autofocus" + attrs="{'invisible': [('state', '!=', 'email')]}"> + <p attrs="{'invisible': [('model_id', '!=', False)]}"> + Please set the Base Model before setting the action details. + </p> + <group attrs="{'invisible': [('model_id', '=', False)]}"> + <field name="template_id" + on_change='on_change_template_id(template_id)' + domain="[('model_id', '=', model_id)]" + attrs="{'required': [('state', '=', 'email')]}"/> + <p colspan="2" attrs="{'invisible': [('template_id', '!=', False)]}"> + Choose a template to display its values. + </p> + <p colspan="2" attrs="{'invisible': [('template_id', '=', False)]}"> + The values displayed hereunder are informative. When sending the email, the values + will be taken from the email template. + </p> + </group> + <group attrs="{'invisible': ['|', ('model_id', '=', False), ('template_id', '=', False)]}"> + <label for="email_from"/> + <div> + <field name="email_from" nolabel="1'" readonly="1" + attrs="{'required': [('state', '=', 'email')]}"/> + <p attrs="{'invisible': [('email_from', '!=', False)]}"> + Your template does not defined any email_from. Please update your template. + </p> + </div> + <field name="email_to" readonly="1"/> + <field name="partner_to" readonly="1"/> + <field name="subject" readonly="1" attrs="{'required': [('state', '=', 'email')]}"/> + <field name="body_html" readonly="1" attrs="{'required': [('state', '=', 'email')]}"/> + </group> + </page> + </xpath> + </field> + </record> + + </data> +</openerp> 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 <http://www.gnu.org/licenses/>. # ############################################################################## -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. <http://www.openerp.com> +# +# 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 <http://www.gnu.org/licenses/>. +# +############################################################################## + +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': '<p>Dear ${object.name}, your parent is ${object.parent_id and object.parent_id.name or "False"}</p>', + }) + + 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, '<p>Dear TestingPartner, your parent is False</p>', + 'ir_actions_server: TODO') diff --git a/addons/email_template/wizard/mail_compose_message.py b/addons/email_template/wizard/mail_compose_message.py index 7f44828ce15..0aab765efbf 100644 --- a/addons/email_template/wizard/mail_compose_message.py +++ b/addons/email_template/wizard/mail_compose_message.py @@ -22,6 +22,7 @@ from openerp import tools from openerp.osv import osv, fields + def _reopen(self, res_id, model): return {'type': 'ir.actions.act_window', 'view_mode': 'form', @@ -34,7 +35,8 @@ def _reopen(self, res_id, model): 'context': { 'default_model': model, }, - } + } + class mail_compose_message(osv.TransientModel): _inherit = 'mail.compose.message' @@ -54,24 +56,28 @@ class mail_compose_message(osv.TransientModel): Indeed, basic mail.compose.message wizard duplicates attachments in mass mailing mode. But in 'single post' mode, attachments of an email template also have to be duplicated to avoid changing their ownership. """ + if context is None: + context = {} + wizard_context = dict(context) for wizard in self.browse(cr, uid, ids, context=context): + if wizard.template_id: + wizard_context['mail_notify_user_signature'] = False # template user_signature is added when generating body_html if not wizard.attachment_ids or wizard.composition_mode == 'mass_mail' or not wizard.template_id: continue - template = self.pool.get('email.template').browse(cr, uid, wizard.template_id.id, context=context) new_attachment_ids = [] for attachment in wizard.attachment_ids: - if attachment in template.attachment_ids: + if attachment in wizard.template_id.attachment_ids: new_attachment_ids.append(self.pool.get('ir.attachment').copy(cr, uid, attachment.id, {'res_model': 'mail.compose.message', 'res_id': wizard.id}, context=context)) else: new_attachment_ids.append(attachment.id) self.write(cr, uid, wizard.id, {'attachment_ids': [(6, 0, new_attachment_ids)]}, context=context) - return super(mail_compose_message, self).send_mail(cr, uid, ids, context=context) + return super(mail_compose_message, self).send_mail(cr, uid, ids, context=wizard_context) def onchange_template_id(self, cr, uid, ids, template_id, composition_mode, model, res_id, context=None): """ - mass_mailing: we cannot render, so return the template values - normal mode: return rendered values """ if template_id and composition_mode == 'mass_mail': - fields = ['subject', 'body_html', 'email_from', 'email_to', 'partner_to', 'email_cc', 'reply_to', 'attachment_ids'] + fields = ['subject', 'body_html', 'email_from', 'email_to', 'partner_to', 'email_cc', 'reply_to', 'attachment_ids', 'mail_server_id'] 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)) elif template_id: @@ -91,7 +97,7 @@ class mail_compose_message(osv.TransientModel): } values['attachment_ids'].append(ir_attach_obj.create(cr, uid, data_attach, context=context)) else: - values = self.default_get(cr, uid, ['subject', 'body', 'email_from', 'email_to', 'email_cc', 'partner_to', 'reply_to', 'attachment_ids'], context=context) + values = self.default_get(cr, uid, ['subject', 'body', 'email_from', 'email_to', 'email_cc', 'partner_to', 'reply_to', 'attachment_ids', 'mail_server_id'], context=context) if values.get('body_html'): values['body'] = values.pop('body_html') @@ -146,12 +152,13 @@ class mail_compose_message(osv.TransientModel): mail.compose.message, transform email_cc and email_to into partner_ids """ template_values = self.pool.get('email.template').generate_email(cr, uid, template_id, res_id, context=context) # filter template values - fields = ['subject', 'body_html', 'email_from', 'email_to', 'partner_to', 'email_cc', 'reply_to', 'attachment_ids', 'attachments'] + fields = ['subject', 'body_html', 'email_from', 'email_to', 'partner_to', 'email_cc', 'reply_to', 'attachment_ids', 'attachments', 'mail_server_id'] values = dict((field, template_values[field]) for field in fields if template_values.get(field)) values['body'] = values.pop('body_html', '') # transform email_to, email_cc into partner_ids - partner_ids = self._get_or_create_partners_from_values(cr, uid, values, context=context) + ctx = dict((k, v) for k, v in (context or {}).items() if not k.startswith('default_')) + partner_ids = self._get_or_create_partners_from_values(cr, uid, values, context=ctx) # legacy template behavior: void values do not erase existing values and the # related key is removed from the values dict if partner_ids: |
