2010-07-01 13:16:22 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
##############################################################################
|
|
|
|
#
|
|
|
|
# OpenERP, Open Source Management Solution
|
2010-08-18 15:22:27 +00:00
|
|
|
# Copyright (C) 2009 Sharoon Thomas
|
2011-02-14 19:49:36 +00:00
|
|
|
# Copyright (C) 2010-Today OpenERP SA (<http://www.openerp.com>)
|
2010-07-01 13:16:22 +00:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
2010-08-18 15:22:27 +00:00
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
2010-07-01 13:16:22 +00:00
|
|
|
#
|
|
|
|
# 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
|
2010-08-18 15:22:27 +00:00
|
|
|
# GNU General Public License for more details.
|
2010-07-01 13:16:22 +00:00
|
|
|
#
|
2010-08-18 15:22:27 +00:00
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>
|
2010-07-01 13:16:22 +00:00
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
2010-06-02 14:36:27 +00:00
|
|
|
import base64
|
2013-08-26 12:44:05 +00:00
|
|
|
import datetime
|
|
|
|
import dateutil.relativedelta as relativedelta
|
2011-09-08 17:37:52 +00:00
|
|
|
import logging
|
2011-08-25 12:27:25 +00:00
|
|
|
|
2013-02-22 12:48:23 +00:00
|
|
|
import openerp
|
2013-06-06 12:34:15 +00:00
|
|
|
from openerp import SUPERUSER_ID
|
2012-12-17 14:43:06 +00:00
|
|
|
from openerp.osv import osv, fields
|
2012-12-06 14:56:32 +00:00
|
|
|
from openerp import tools
|
|
|
|
from openerp.tools.translate import _
|
2012-12-17 14:43:06 +00:00
|
|
|
from urllib import urlencode, quote as quote
|
2013-12-19 15:28:01 +00:00
|
|
|
|
2012-12-13 09:56:43 +00:00
|
|
|
|
2012-06-22 06:48:54 +00:00
|
|
|
_logger = logging.getLogger(__name__)
|
2010-06-02 14:36:27 +00:00
|
|
|
|
2011-08-25 12:27:25 +00:00
|
|
|
try:
|
2012-12-13 09:56:43 +00:00
|
|
|
# We use a jinja2 sandboxed environment to render mako templates.
|
|
|
|
# Note that the rendering does not cover all the mako syntax, in particular
|
|
|
|
# arbitrary Python statements are not accepted, and not all expressions are
|
|
|
|
# allowed: only "public" attributes (not starting with '_') of objects may
|
|
|
|
# be accessed.
|
|
|
|
# This is done on purpose: it prevents incidental or malicious execution of
|
|
|
|
# Python code that may break the security of the server.
|
|
|
|
from jinja2.sandbox import SandboxedEnvironment
|
|
|
|
mako_template_env = SandboxedEnvironment(
|
|
|
|
block_start_string="<%",
|
|
|
|
block_end_string="%>",
|
|
|
|
variable_start_string="${",
|
|
|
|
variable_end_string="}",
|
|
|
|
comment_start_string="<%doc>",
|
|
|
|
comment_end_string="</%doc>",
|
|
|
|
line_statement_prefix="%",
|
|
|
|
line_comment_prefix="##",
|
|
|
|
trim_blocks=True, # do not output newline after blocks
|
|
|
|
autoescape=True, # XML/HTML automatic escaping
|
|
|
|
)
|
2012-12-13 14:01:35 +00:00
|
|
|
mako_template_env.globals.update({
|
|
|
|
'str': str,
|
|
|
|
'quote': quote,
|
2012-12-14 09:03:40 +00:00
|
|
|
'urlencode': urlencode,
|
2013-08-26 12:44:05 +00:00
|
|
|
'datetime': datetime,
|
|
|
|
|
|
|
|
# dateutil.relativedelta is an old-style class and cannot be directly
|
|
|
|
# instanciated wihtin a jinja2 expression, so a lambda "proxy" is
|
|
|
|
# is needed, apparently.
|
|
|
|
'relativedelta': lambda *a, **kw : relativedelta.relativedelta(*a, **kw),
|
2012-12-13 14:01:35 +00:00
|
|
|
})
|
2011-08-25 12:27:25 +00:00
|
|
|
except ImportError:
|
2012-12-13 09:56:43 +00:00
|
|
|
_logger.warning("jinja2 not available, templating features will not work!")
|
2011-08-25 12:27:25 +00:00
|
|
|
|
2010-06-02 14:36:27 +00:00
|
|
|
class email_template(osv.osv):
|
2011-08-25 12:27:25 +00:00
|
|
|
"Templates for sending email"
|
2010-06-02 14:36:27 +00:00
|
|
|
_name = "email.template"
|
2011-08-25 12:27:25 +00:00
|
|
|
_description = 'Email Templates'
|
2013-03-07 16:50:14 +00:00
|
|
|
_order = 'name'
|
2011-08-25 12:27:25 +00:00
|
|
|
|
2013-08-28 14:09:29 +00:00
|
|
|
def render_template_batch(self, cr, uid, template, model, res_ids, context=None):
|
2011-08-25 12:27:25 +00:00
|
|
|
"""Render the given template text, replace mako expressions ``${expr}``
|
|
|
|
with the result of evaluating these expressions with
|
|
|
|
an evaluation context containing:
|
|
|
|
|
|
|
|
* ``user``: browse_record of the current user
|
|
|
|
* ``object``: browse_record of the document record this mail is
|
|
|
|
related to
|
|
|
|
* ``context``: the context passed to the mail composition wizard
|
2010-06-02 14:36:27 +00:00
|
|
|
|
2011-08-25 12:27:25 +00:00
|
|
|
:param str template: the template text to render
|
|
|
|
:param str model: model name of the document record this mail is related to.
|
2013-08-28 14:09:29 +00:00
|
|
|
:param int res_ids: list of ids of document records those mails are related to.
|
2011-08-25 12:27:25 +00:00
|
|
|
"""
|
2012-03-22 14:11:53 +00:00
|
|
|
if context is None:
|
|
|
|
context = {}
|
2013-08-28 14:09:29 +00:00
|
|
|
results = dict.fromkeys(res_ids, u"")
|
|
|
|
|
|
|
|
# try to load the template
|
2011-08-25 12:27:25 +00:00
|
|
|
try:
|
2013-08-28 14:09:29 +00:00
|
|
|
template = mako_template_env.from_string(tools.ustr(template))
|
2011-08-25 12:27:25 +00:00
|
|
|
except Exception:
|
2013-08-28 14:09:29 +00:00
|
|
|
_logger.exception("Failed to load template %r", template)
|
|
|
|
return results
|
|
|
|
|
|
|
|
# prepare template variables
|
|
|
|
user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
|
|
|
|
records = self.pool[model].browse(cr, uid, res_ids, context=context) or [None]
|
|
|
|
variables = {
|
|
|
|
'user': user,
|
|
|
|
'ctx': context, # context kw would clash with mako internals
|
|
|
|
}
|
|
|
|
for record in records:
|
|
|
|
res_id = record.id if record else None
|
|
|
|
variables['object'] = record
|
|
|
|
try:
|
|
|
|
render_result = template.render(variables)
|
|
|
|
except Exception:
|
|
|
|
_logger.exception("Failed to render template %r using values %r" % (template, variables))
|
|
|
|
render_result = u""
|
|
|
|
if render_result == u"False":
|
|
|
|
render_result = u""
|
|
|
|
results[res_id] = render_result
|
|
|
|
return results
|
|
|
|
|
|
|
|
def get_email_template_batch(self, cr, uid, template_id=False, res_ids=None, context=None):
|
2011-02-14 19:49:36 +00:00
|
|
|
if context is None:
|
|
|
|
context = {}
|
2013-08-28 14:09:29 +00:00
|
|
|
if res_ids is None:
|
|
|
|
res_ids = [None]
|
|
|
|
results = dict.fromkeys(res_ids, False)
|
|
|
|
|
2011-02-14 19:49:36 +00:00
|
|
|
if not template_id:
|
2013-08-28 14:09:29 +00:00
|
|
|
return results
|
2011-09-08 17:37:52 +00:00
|
|
|
template = self.browse(cr, uid, template_id, context)
|
2013-08-28 14:09:29 +00:00
|
|
|
langs = self.render_template_batch(cr, uid, template.lang, template.model, res_ids, context)
|
|
|
|
for res_id, lang in langs.iteritems():
|
|
|
|
if lang:
|
|
|
|
# Use translated template if necessary
|
|
|
|
ctx = context.copy()
|
|
|
|
ctx['lang'] = lang
|
|
|
|
template = self.browse(cr, uid, template.id, ctx)
|
|
|
|
else:
|
|
|
|
template = self.browse(cr, uid, int(template_id), context)
|
|
|
|
results[res_id] = template
|
|
|
|
return results
|
2011-02-14 19:49:36 +00:00
|
|
|
|
|
|
|
def onchange_model_id(self, cr, uid, ids, model_id, context=None):
|
2011-02-08 05:05:35 +00:00
|
|
|
mod_name = False
|
2011-02-14 19:49:36 +00:00
|
|
|
if model_id:
|
|
|
|
mod_name = self.pool.get('ir.model').browse(cr, uid, model_id, context).model
|
2012-09-05 15:56:26 +00:00
|
|
|
return {'value': {'model': mod_name}}
|
2011-04-13 10:41:45 +00:00
|
|
|
|
2010-06-02 14:36:27 +00:00
|
|
|
_columns = {
|
2012-09-01 01:09:07 +00:00
|
|
|
'name': fields.char('Name'),
|
|
|
|
'model_id': fields.many2one('ir.model', 'Applies to', help="The kind of document with with this template can be used"),
|
2012-08-31 15:58:05 +00:00
|
|
|
'model': fields.related('model_id', 'model', type='char', string='Related Document Model',
|
|
|
|
size=128, select=True, store=True, readonly=True),
|
2012-09-01 01:09:07 +00:00
|
|
|
'lang': fields.char('Language',
|
2011-09-08 17:55:41 +00:00
|
|
|
help="Optional translation language (ISO code) to select when sending out an email. "
|
2011-08-25 12:27:25 +00:00
|
|
|
"If not set, the english version will be used. "
|
|
|
|
"This should usually be a placeholder expression "
|
|
|
|
"that provides the appropriate language code, e.g. "
|
2012-09-01 01:09:07 +00:00
|
|
|
"${object.partner_id.lang.code}.",
|
|
|
|
placeholder="${object.partner_id.lang.code}"),
|
2011-09-08 17:37:52 +00:00
|
|
|
'user_signature': fields.boolean('Add Signature',
|
2011-08-25 12:27:25 +00:00
|
|
|
help="If checked, the user's signature will be appended to the text version "
|
|
|
|
"of the message"),
|
2012-09-01 01:09:07 +00:00
|
|
|
'subject': fields.char('Subject', translate=True, help="Subject (placeholders may be used here)",),
|
2013-03-13 12:16:59 +00:00
|
|
|
'email_from': fields.char('From',
|
|
|
|
help="Sender address (placeholders may be used here). If not set, the default "
|
|
|
|
"value will be the author's email alias if configured, or email address."),
|
2012-11-07 11:39:25 +00:00
|
|
|
'email_to': fields.char('To (Emails)', help="Comma-separated recipient addresses (placeholders may be used here)"),
|
2013-02-25 10:19:18 +00:00
|
|
|
'partner_to': fields.char('To (Partners)',
|
|
|
|
help="Comma-separated ids of recipient partners (placeholders may be used here)",
|
|
|
|
oldname='email_recipients'),
|
2012-09-01 01:09:07 +00:00
|
|
|
'email_cc': fields.char('Cc', help="Carbon copy recipients (placeholders may be used here)"),
|
|
|
|
'reply_to': fields.char('Reply-To', help="Preferred response address (placeholders may be used here)"),
|
|
|
|
'mail_server_id': fields.many2one('ir.mail_server', 'Outgoing Mail Server', readonly=False,
|
|
|
|
help="Optional preferred server for outgoing mails. If not set, the highest "
|
|
|
|
"priority one will be used."),
|
|
|
|
'body_html': fields.text('Body', translate=True, help="Rich-text/HTML version of the message (placeholders may be used here)"),
|
|
|
|
'report_name': fields.char('Report Filename', translate=True,
|
2011-08-25 12:27:25 +00:00
|
|
|
help="Name to use for the generated report file (may contain placeholders)\n"
|
|
|
|
"The extension can be omitted and will then come from the report type."),
|
2012-09-01 01:09:07 +00:00
|
|
|
'report_template': fields.many2one('ir.actions.report.xml', 'Optional report to print and attach'),
|
|
|
|
'ref_ir_act_window': fields.many2one('ir.actions.act_window', 'Sidebar action', readonly=True,
|
2011-08-25 12:27:25 +00:00
|
|
|
help="Sidebar action to make this template available on records "
|
|
|
|
"of the related document model"),
|
2012-09-01 01:09:07 +00:00
|
|
|
'ref_ir_value': fields.many2one('ir.values', 'Sidebar Button', readonly=True,
|
2011-08-25 12:27:25 +00:00
|
|
|
help="Sidebar button to open the sidebar action"),
|
2012-08-31 15:58:05 +00:00
|
|
|
'attachment_ids': fields.many2many('ir.attachment', 'email_template_attachment_rel', 'email_template_id',
|
2012-09-01 01:09:07 +00:00
|
|
|
'attachment_id', 'Attachments',
|
2012-08-31 15:58:05 +00:00
|
|
|
help="You may attach files to this template, to be added to all "
|
|
|
|
"emails created from this template"),
|
|
|
|
'auto_delete': fields.boolean('Auto Delete', help="Permanently delete this email after sending it, to save space"),
|
2011-08-25 12:27:25 +00:00
|
|
|
|
|
|
|
# Fake fields used to implement the placeholder assistant
|
|
|
|
'model_object_field': fields.many2one('ir.model.fields', string="Field",
|
|
|
|
help="Select target field from the related document model.\n"
|
|
|
|
"If it is a relationship field you will be able to select "
|
|
|
|
"a target field at the destination of the relationship."),
|
|
|
|
'sub_object': fields.many2one('ir.model', 'Sub-model', readonly=True,
|
|
|
|
help="When a relationship field is selected as first field, "
|
|
|
|
"this field shows the document model the relationship goes to."),
|
|
|
|
'sub_model_object_field': fields.many2one('ir.model.fields', 'Sub-field',
|
|
|
|
help="When a relationship field is selected as first field, "
|
|
|
|
"this field lets you select the target field within the "
|
|
|
|
"destination document model (sub-model)."),
|
2012-09-01 01:09:07 +00:00
|
|
|
'null_value': fields.char('Default Value', help="Optional value to use if the target field is empty"),
|
|
|
|
'copyvalue': fields.char('Placeholder Expression', help="Final placeholder expression, to be copy-pasted in the desired template field."),
|
2011-09-08 17:37:52 +00:00
|
|
|
}
|
|
|
|
|
2012-09-05 15:19:50 +00:00
|
|
|
_defaults = {
|
|
|
|
'auto_delete': True,
|
|
|
|
}
|
|
|
|
|
2010-11-19 13:48:01 +00:00
|
|
|
def create_action(self, cr, uid, ids, context=None):
|
2011-02-04 13:04:09 +00:00
|
|
|
action_obj = self.pool.get('ir.actions.act_window')
|
2011-02-15 12:51:14 +00:00
|
|
|
data_obj = self.pool.get('ir.model.data')
|
2011-02-04 13:04:09 +00:00
|
|
|
for template in self.browse(cr, uid, ids, context=context):
|
2011-02-14 19:49:36 +00:00
|
|
|
src_obj = template.model_id.model
|
2011-04-06 11:24:54 +00:00
|
|
|
model_data_id = data_obj._get_id(cr, uid, 'mail', 'email_compose_message_wizard_form')
|
2011-02-15 12:51:14 +00:00
|
|
|
res_id = data_obj.browse(cr, uid, model_data_id, context=context).res_id
|
2012-01-25 17:42:49 +00:00
|
|
|
button_name = _('Send Mail (%s)') % template.name
|
2013-06-13 14:39:31 +00:00
|
|
|
act_id = action_obj.create(cr, SUPERUSER_ID, {
|
2012-01-25 17:42:49 +00:00
|
|
|
'name': button_name,
|
2011-02-04 13:04:09 +00:00
|
|
|
'type': 'ir.actions.act_window',
|
2011-08-25 12:27:25 +00:00
|
|
|
'res_model': 'mail.compose.message',
|
2011-02-04 13:04:09 +00:00
|
|
|
'src_model': src_obj,
|
|
|
|
'view_type': 'form',
|
2012-10-16 11:58:39 +00:00
|
|
|
'context': "{'default_composition_mode': 'mass_mail', 'default_template_id' : %d, 'default_use_template': True}" % (template.id),
|
2011-02-04 13:04:09 +00:00
|
|
|
'view_mode':'form,tree',
|
2011-02-15 12:51:14 +00:00
|
|
|
'view_id': res_id,
|
2011-02-04 13:04:09 +00:00
|
|
|
'target': 'new',
|
|
|
|
'auto_refresh':1
|
|
|
|
}, context)
|
2013-06-13 14:39:31 +00:00
|
|
|
ir_values_id = self.pool.get('ir.values').create(cr, SUPERUSER_ID, {
|
2012-01-25 17:42:49 +00:00
|
|
|
'name': button_name,
|
2011-02-04 13:04:09 +00:00
|
|
|
'model': src_obj,
|
|
|
|
'key2': 'client_action_multi',
|
2013-06-13 14:39:31 +00:00
|
|
|
'value': "ir.actions.act_window,%s" % act_id,
|
2011-02-04 13:04:09 +00:00
|
|
|
'object': True,
|
|
|
|
}, context)
|
2013-06-13 14:39:31 +00:00
|
|
|
|
|
|
|
template.write({
|
|
|
|
'ref_ir_act_window': act_id,
|
|
|
|
'ref_ir_value': ir_values_id,
|
|
|
|
})
|
|
|
|
|
2010-06-25 09:52:31 +00:00
|
|
|
return True
|
2010-06-02 14:36:27 +00:00
|
|
|
|
2010-11-19 13:48:01 +00:00
|
|
|
def unlink_action(self, cr, uid, ids, context=None):
|
|
|
|
for template in self.browse(cr, uid, ids, context=context):
|
2010-06-02 14:36:27 +00:00
|
|
|
try:
|
|
|
|
if template.ref_ir_act_window:
|
2013-06-13 14:39:31 +00:00
|
|
|
self.pool.get('ir.actions.act_window').unlink(cr, SUPERUSER_ID, template.ref_ir_act_window.id, context)
|
2010-06-02 14:36:27 +00:00
|
|
|
if template.ref_ir_value:
|
2011-05-16 15:33:31 +00:00
|
|
|
ir_values_obj = self.pool.get('ir.values')
|
2013-06-13 14:39:31 +00:00
|
|
|
ir_values_obj.unlink(cr, SUPERUSER_ID, template.ref_ir_value.id, context)
|
2012-09-01 01:09:07 +00:00
|
|
|
except Exception:
|
2011-12-31 07:57:20 +00:00
|
|
|
raise osv.except_osv(_("Warning"), _("Deletion of the action record failed."))
|
2011-09-08 17:55:41 +00:00
|
|
|
return True
|
2010-06-25 09:52:31 +00:00
|
|
|
|
|
|
|
def unlink(self, cr, uid, ids, context=None):
|
2010-11-19 13:48:01 +00:00
|
|
|
self.unlink_action(cr, uid, ids, context=context)
|
|
|
|
return super(email_template, self).unlink(cr, uid, ids, context=context)
|
2010-10-26 08:47:22 +00:00
|
|
|
|
2010-06-02 14:36:27 +00:00
|
|
|
def copy(self, cr, uid, id, default=None, context=None):
|
2011-04-20 13:40:33 +00:00
|
|
|
template = self.browse(cr, uid, id, context=context)
|
2010-06-02 14:36:27 +00:00
|
|
|
if default is None:
|
|
|
|
default = {}
|
|
|
|
default = default.copy()
|
2012-09-24 17:17:36 +00:00
|
|
|
default.update(
|
|
|
|
name=_("%s (copy)") % (template.name),
|
|
|
|
ref_ir_act_window=False,
|
|
|
|
ref_ir_value=False)
|
2010-06-02 14:36:27 +00:00
|
|
|
return super(email_template, self).copy(cr, uid, id, default, context)
|
2010-10-26 08:47:22 +00:00
|
|
|
|
2011-02-03 10:14:28 +00:00
|
|
|
def build_expression(self, field_name, sub_field_name, null_value):
|
2011-08-25 12:27:25 +00:00
|
|
|
"""Returns a placeholder expression for use in a template field,
|
|
|
|
based on the values provided in the placeholder assistant.
|
|
|
|
|
|
|
|
:param field_name: main field name
|
|
|
|
:param sub_field_name: sub field name (M2O)
|
|
|
|
:param null_value: default value if the target value is empty
|
|
|
|
:return: final placeholder expression
|
2010-06-02 14:36:27 +00:00
|
|
|
"""
|
2010-09-27 14:17:58 +00:00
|
|
|
expression = ''
|
2011-02-03 10:14:28 +00:00
|
|
|
if field_name:
|
|
|
|
expression = "${object." + field_name
|
|
|
|
if sub_field_name:
|
|
|
|
expression += "." + sub_field_name
|
|
|
|
if null_value:
|
|
|
|
expression += " or '''%s'''" % null_value
|
|
|
|
expression += "}"
|
2010-10-26 08:47:22 +00:00
|
|
|
return expression
|
|
|
|
|
2011-02-04 11:28:51 +00:00
|
|
|
def onchange_sub_model_object_value_field(self, cr, uid, ids, model_object_field, sub_model_object_field=False, null_value=None, context=None):
|
|
|
|
result = {
|
|
|
|
'sub_object': False,
|
|
|
|
'copyvalue': False,
|
|
|
|
'sub_model_object_field': False,
|
|
|
|
'null_value': False
|
|
|
|
}
|
|
|
|
if model_object_field:
|
2011-02-04 13:04:09 +00:00
|
|
|
fields_obj = self.pool.get('ir.model.fields')
|
|
|
|
field_value = fields_obj.browse(cr, uid, model_object_field, context)
|
|
|
|
if field_value.ttype in ['many2one', 'one2many', 'many2many']:
|
|
|
|
res_ids = self.pool.get('ir.model').search(cr, uid, [('model', '=', field_value.relation)], context=context)
|
2011-02-04 11:28:51 +00:00
|
|
|
sub_field_value = False
|
|
|
|
if sub_model_object_field:
|
2011-02-04 13:04:09 +00:00
|
|
|
sub_field_value = fields_obj.browse(cr, uid, sub_model_object_field, context)
|
2011-02-04 11:28:51 +00:00
|
|
|
if res_ids:
|
|
|
|
result.update({
|
|
|
|
'sub_object': res_ids[0],
|
2011-02-04 13:04:09 +00:00
|
|
|
'copyvalue': self.build_expression(field_value.name, sub_field_value and sub_field_value.name or False, null_value or False),
|
2011-02-04 11:28:51 +00:00
|
|
|
'sub_model_object_field': sub_model_object_field or False,
|
|
|
|
'null_value': null_value or False
|
|
|
|
})
|
|
|
|
else:
|
|
|
|
result.update({
|
2011-02-04 13:04:09 +00:00
|
|
|
'copyvalue': self.build_expression(field_value.name, False, null_value or False),
|
2011-02-04 11:28:51 +00:00
|
|
|
'null_value': null_value or False
|
|
|
|
})
|
2013-02-28 13:26:25 +00:00
|
|
|
return {'value': result}
|
2011-02-15 09:58:33 +00:00
|
|
|
|
2013-08-28 14:09:29 +00:00
|
|
|
def generate_email_batch(self, cr, uid, template_id, res_ids, context=None, fields=None):
|
|
|
|
"""Generates an email from the template for given the given model based on
|
|
|
|
records given by res_ids.
|
|
|
|
|
|
|
|
:param template_id: id of the template to render.
|
|
|
|
:param res_id: id of the record to use for rendering the template (model
|
|
|
|
is taken from template definition)
|
|
|
|
:returns: a dict containing all relevant fields for creating a new
|
|
|
|
mail.mail entry, with one extra key ``attachments``, in the
|
|
|
|
format expected by :py:meth:`mail_thread.message_post`.
|
2010-06-02 14:36:27 +00:00
|
|
|
"""
|
|
|
|
if context is None:
|
|
|
|
context = {}
|
2013-08-28 14:09:29 +00:00
|
|
|
if fields is None:
|
|
|
|
fields = ['subject', 'body_html', 'email_from', 'email_to', 'partner_to', 'email_cc', 'reply_to']
|
|
|
|
|
2011-02-14 19:49:36 +00:00
|
|
|
report_xml_pool = self.pool.get('ir.actions.report.xml')
|
2013-08-28 14:09:29 +00:00
|
|
|
res_ids_to_templates = self.get_email_template_batch(cr, uid, template_id, res_ids, context)
|
|
|
|
|
|
|
|
# templates: res_id -> template; template -> res_ids
|
|
|
|
templates_to_res_ids = {}
|
|
|
|
for res_id, template in res_ids_to_templates.iteritems():
|
|
|
|
templates_to_res_ids.setdefault(template, []).append(res_id)
|
|
|
|
|
|
|
|
results = dict()
|
|
|
|
for template, template_res_ids in templates_to_res_ids.iteritems():
|
|
|
|
# generate fields value for all res_ids linked to the current template
|
|
|
|
for field in ['subject', 'body_html', 'email_from', 'email_to', 'partner_to', 'email_cc', 'reply_to']:
|
|
|
|
generated_field_values = self.render_template_batch(cr, uid, getattr(template, field), template.model, template_res_ids, context=context)
|
|
|
|
for res_id, field_value in generated_field_values.iteritems():
|
|
|
|
results.setdefault(res_id, dict())[field] = field_value
|
|
|
|
# update values for all res_ids
|
|
|
|
for res_id in template_res_ids:
|
|
|
|
values = results[res_id]
|
|
|
|
if template.user_signature:
|
|
|
|
signature = self.pool.get('res.users').browse(cr, uid, uid, context).signature
|
|
|
|
values['body_html'] = tools.append_content_to_html(values['body_html'], signature)
|
|
|
|
if values['body_html']:
|
|
|
|
values['body'] = tools.html_sanitize(values['body_html'])
|
|
|
|
values.update(
|
|
|
|
mail_server_id=template.mail_server_id.id or False,
|
|
|
|
auto_delete=template.auto_delete,
|
|
|
|
model=template.model,
|
|
|
|
res_id=res_id or False,
|
|
|
|
attachment_ids=[attach.id for attach in template.attachment_ids],
|
|
|
|
)
|
|
|
|
|
2013-11-26 11:07:42 +00:00
|
|
|
# Add report in attachments: generate once for all template_res_ids
|
2013-08-28 14:09:29 +00:00
|
|
|
if template.report_template:
|
|
|
|
for res_id in template_res_ids:
|
|
|
|
attachments = []
|
|
|
|
report_name = self.render_template(cr, uid, template.report_name, template.model, res_id, context=context)
|
|
|
|
report_service = report_xml_pool.browse(cr, uid, template.report_template.id, context).report_name
|
|
|
|
# Ensure report is rendered using template's language
|
|
|
|
ctx = context.copy()
|
|
|
|
if template.lang:
|
2013-11-18 16:31:14 +00:00
|
|
|
ctx['lang'] = self.render_template_batch(cr, uid, template.lang, template.model, [res_id], context)[res_id] # take 0 ?
|
2013-08-28 14:09:29 +00:00
|
|
|
result, format = openerp.report.render_report(cr, uid, [res_id], report_service, {'model': template.model}, ctx)
|
|
|
|
result = base64.b64encode(result)
|
|
|
|
if not report_name:
|
|
|
|
report_name = 'report.' + report_service
|
|
|
|
ext = "." + format
|
|
|
|
if not report_name.endswith(ext):
|
|
|
|
report_name += ext
|
|
|
|
attachments.append((report_name, result))
|
2013-11-26 12:08:43 +00:00
|
|
|
results[res_id]['attachments'] = attachments
|
2013-08-28 14:09:29 +00:00
|
|
|
|
|
|
|
return results
|
2011-02-14 19:49:36 +00:00
|
|
|
|
2013-05-24 11:48:57 +00:00
|
|
|
def send_mail(self, cr, uid, template_id, res_id, force_send=False, raise_exception=False, context=None):
|
2011-08-25 12:27:25 +00:00
|
|
|
"""Generates a new mail message for the given template and record,
|
2011-11-03 02:08:03 +00:00
|
|
|
and schedules it for delivery through the ``mail`` module's scheduler.
|
2010-06-02 14:36:27 +00:00
|
|
|
|
2011-08-25 12:27:25 +00:00
|
|
|
:param int template_id: id of the template to render
|
2011-09-12 18:00:25 +00:00
|
|
|
:param int res_id: id of the record to render the template with
|
|
|
|
(model is taken from the template)
|
2011-11-03 02:08:03 +00:00
|
|
|
:param bool force_send: if True, the generated mail.message is
|
|
|
|
immediately sent after being created, as if the scheduler
|
|
|
|
was executed for this message only.
|
2012-09-24 16:26:45 +00:00
|
|
|
:returns: id of the mail.message that was created
|
2011-08-25 12:27:25 +00:00
|
|
|
"""
|
2013-02-28 13:26:25 +00:00
|
|
|
if context is None:
|
|
|
|
context = {}
|
2012-08-21 21:24:19 +00:00
|
|
|
mail_mail = self.pool.get('mail.mail')
|
2011-08-25 12:27:25 +00:00
|
|
|
ir_attachment = self.pool.get('ir.attachment')
|
2013-02-28 13:26:25 +00:00
|
|
|
|
|
|
|
# create a mail_mail based on values, without attachments
|
2011-08-25 12:27:25 +00:00
|
|
|
values = self.generate_email(cr, uid, template_id, res_id, context=context)
|
2013-02-28 13:26:25 +00:00
|
|
|
assert values.get('email_from'), 'email_from is missing or empty after template rendering, send_mail() cannot proceed'
|
2013-12-20 14:46:52 +00:00
|
|
|
|
2013-12-24 11:24:07 +00:00
|
|
|
# process partner_to field that is a comma separated list of partner_ids -> recipient_ids
|
2013-12-20 14:46:52 +00:00
|
|
|
# NOTE: only usable if force_send is True, because otherwise the value is
|
|
|
|
# not stored on the mail_mail, and therefore lost -> fixed in v8
|
2013-12-24 11:24:07 +00:00
|
|
|
values['recipient_ids'] = []
|
|
|
|
partner_to = values.pop('partner_to', '')
|
|
|
|
if partner_to:
|
|
|
|
# placeholders could generate '', 3, 2 due to some empty field values
|
|
|
|
tpl_partner_ids = [pid for pid in partner_to.split(',') if pid]
|
|
|
|
values['recipient_ids'] += [(4, pid) for pid in self.pool['res.partner'].exists(cr, SUPERUSER_ID, tpl_partner_ids, context=context)]
|
2013-12-20 14:46:52 +00:00
|
|
|
|
2013-02-28 13:26:25 +00:00
|
|
|
attachment_ids = values.pop('attachment_ids', [])
|
|
|
|
attachments = values.pop('attachments', [])
|
2012-08-21 21:24:19 +00:00
|
|
|
msg_id = mail_mail.create(cr, uid, values, context=context)
|
2013-04-22 10:21:14 +00:00
|
|
|
mail = mail_mail.browse(cr, uid, msg_id, context=context)
|
2013-03-06 13:44:10 +00:00
|
|
|
|
|
|
|
# manage attachments
|
2013-02-28 13:49:01 +00:00
|
|
|
for attachment in attachments:
|
2011-08-25 12:27:25 +00:00
|
|
|
attachment_data = {
|
2013-12-20 14:46:52 +00:00
|
|
|
'name': attachment[0],
|
|
|
|
'datas_fname': attachment[0],
|
|
|
|
'datas': attachment[1],
|
|
|
|
'res_model': 'mail.message',
|
|
|
|
'res_id': mail.mail_message_id.id,
|
2011-08-25 12:27:25 +00:00
|
|
|
}
|
2012-03-09 01:11:56 +00:00
|
|
|
context.pop('default_type', None)
|
2011-11-03 02:08:03 +00:00
|
|
|
attachment_ids.append(ir_attachment.create(cr, uid, attachment_data, context=context))
|
2012-03-15 15:55:12 +00:00
|
|
|
if attachment_ids:
|
2013-02-28 13:26:25 +00:00
|
|
|
values['attachment_ids'] = [(6, 0, attachment_ids)]
|
2012-08-21 21:24:19 +00:00
|
|
|
mail_mail.write(cr, uid, msg_id, {'attachment_ids': [(6, 0, attachment_ids)]}, context=context)
|
2013-02-28 13:26:25 +00:00
|
|
|
|
2011-11-03 02:08:03 +00:00
|
|
|
if force_send:
|
2013-05-24 11:48:57 +00:00
|
|
|
mail_mail.send(cr, uid, [msg_id], raise_exception=raise_exception, context=context)
|
2011-11-03 02:08:03 +00:00
|
|
|
return msg_id
|
2011-02-03 13:13:57 +00:00
|
|
|
|
2013-08-28 14:09:29 +00:00
|
|
|
# Compatibility method
|
|
|
|
def render_template(self, cr, uid, template, model, res_id, context=None):
|
|
|
|
return self.render_template_batch(cr, uid, template, model, [res_id], context)[res_id]
|
|
|
|
|
|
|
|
def get_email_template(self, cr, uid, template_id=False, record_id=None, context=None):
|
|
|
|
return self.get_email_template_batch(cr, uid, template_id, [record_id], context)[record_id]
|
|
|
|
|
|
|
|
def generate_email(self, cr, uid, template_id, res_id, context=None):
|
|
|
|
return self.generate_email_batch(cr, uid, template_id, [res_id], context)[res_id]
|
|
|
|
|
2010-06-02 14:36:27 +00:00
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|