diff --git a/addons/email_template/email_template.py b/addons/email_template/email_template.py index 3f0fad1c8a0..402e8b1422f 100644 --- a/addons/email_template/email_template.py +++ b/addons/email_template/email_template.py @@ -231,6 +231,11 @@ class email_template(osv.osv): '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."), + 'use_default_to': fields.boolean( + 'Default recipients', + help="Default recipients of the record:\n" + "- partner (using id on a partner or the partner_id field) OR\n" + "- email (using email_from or email field)"), 'email_to': fields.char('To (Emails)', help="Comma-separated recipient addresses (placeholders may be used here)"), 'partner_to': fields.char('To (Partners)', help="Comma-separated ids of recipient partners (placeholders may be used here)", @@ -386,6 +391,42 @@ class email_template(osv.osv): }) return {'value': result} + def generate_recipients_batch(self, cr, uid, results, template_id, res_ids, context=None): + """Generates the recipients of the template. Default values can ben generated + instead of the template values if requested by template or context. + Emails (email_to, email_cc) can be transformed into partners if requested + in the context. """ + if context is None: + context = {} + template = self.browse(cr, uid, template_id, context=context) + + if template.use_default_to or context.get('tpl_force_default_to'): + if template.model and hasattr(self.pool[template.model], 'message_get_default_recipients'): + default_recipients = self.pool[template.model].message_get_default_recipients(cr, uid, res_ids, context=context) + elif template.model: + ctx = dict(context, thread_model=template.model) + default_recipients = self.pool['mail.thread'].message_get_default_recipients(cr, uid, res_ids, context=ctx) + else: + default_recipients = {} + for res_id, recipients in default_recipients: + results[res_id].pop('partner_to') + results[res_id].update(default_recipients) + + for res_id, values in results.iteritems(): + partner_ids = values.get('partner_ids', list()) + if context and context.get('tpl_partners_only'): + mails = tools.email_split(values.pop('email_to', '')) + tools.email_split(values.pop('email_cc', '')) + for mail in mails: + partner_id = self.pool.get('res.partner').find_or_create(cr, uid, mail, context=context) + partner_ids.append(partner_id) + 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] + partner_ids += self.pool['res.partner'].exists(cr, SUPERUSER_ID, tpl_partner_ids, context=context) + results[res_id]['partner_ids'] = partner_ids + return results + 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. @@ -420,14 +461,18 @@ class email_template(osv.osv): context=context) for res_id, field_value in generated_field_values.iteritems(): results.setdefault(res_id, dict())[field] = field_value + # compute recipients + results = self.generate_recipients_batch(cr, uid, results, template.id, template_res_ids, context=context) # update values for all res_ids for res_id in template_res_ids: values = results[res_id] + # body: add user signature, sanitize if 'body_html' in fields and 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.get('body_html'): values['body'] = tools.html_sanitize(values['body_html']) + # technical settings values.update( mail_server_id=template.mail_server_id.id or False, auto_delete=template.auto_delete, @@ -484,17 +529,8 @@ class email_template(osv.osv): # create a mail_mail based on values, without attachments values = self.generate_email(cr, uid, template_id, res_id, context=context) if not values.get('email_from'): - raise osv.except_osv(_('Warning!'),_("Sender email is missing or empty after template rendering. Specify one to deliver your message")) - # process partner_to field that is a comma separated list of partner_ids -> recipient_ids - # 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 - 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)] - + raise osv.except_osv(_('Warning!'), _("Sender email is missing or empty after template rendering. Specify one to deliver your message")) + values['recipient_ids'] = [(4, pid) for pid in values.get('partner_ids', list())] attachment_ids = values.pop('attachment_ids', []) attachments = values.pop('attachments', []) msg_id = mail_mail.create(cr, uid, values, context=context) diff --git a/addons/email_template/email_template_view.xml b/addons/email_template/email_template_view.xml index 4e5b7d0b21a..41652504279 100644 --- a/addons/email_template/email_template_view.xml +++ b/addons/email_template/email_template_view.xml @@ -9,8 +9,10 @@
@@ -25,43 +27,28 @@ context="{'template_id':active_id}"/>
- - - - - - - - - - - - -

Dynamic placeholder generator

- - - - - -
-
-

Body

- + + + + + + + + + + + + + @@ -72,6 +59,21 @@ attrs="{'invisible':[('report_template','=',False)]}"/> + + + + + + + + +
@@ -85,17 +87,20 @@
-
-
-
-
+ + + + + + +
+
+
+
diff --git a/addons/email_template/wizard/email_template_preview_view.xml b/addons/email_template/wizard/email_template_preview_view.xml index 0a74f7d813e..6d61658dcdc 100644 --- a/addons/email_template/wizard/email_template_preview_view.xml +++ b/addons/email_template/wizard/email_template_preview_view.xml @@ -30,10 +30,11 @@ Template Preview email_template.preview - email_template.preview + email.template ir.actions.act_window form form + new {'template_id':active_id} diff --git a/addons/email_template/wizard/mail_compose_message.py b/addons/email_template/wizard/mail_compose_message.py index 10e8b9c5fc6..61a7a7045a4 100644 --- a/addons/email_template/wizard/mail_compose_message.py +++ b/addons/email_template/wizard/mail_compose_message.py @@ -157,47 +157,29 @@ class mail_compose_message(osv.TransientModel): # Wizard validation and send #------------------------------------------------------ - def _get_or_create_partners_from_values(self, cr, uid, rendered_values, context=None): - """ Check for email_to, email_cc, partner_to """ - partner_ids = [] - mails = tools.email_split(rendered_values.pop('email_to', '')) + tools.email_split(rendered_values.pop('email_cc', '')) - for mail in mails: - partner_id = self.pool.get('res.partner').find_or_create(cr, uid, mail, context=context) - partner_ids.append(partner_id) - partner_to = rendered_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] - partner_ids += self.pool['res.partner'].exists(cr, SUPERUSER_ID, tpl_partner_ids, context=context) - return partner_ids - def generate_email_for_composer_batch(self, cr, uid, template_id, res_ids, context=None, fields=None): """ Call email_template.generate_email(), get fields relevant for mail.compose.message, transform email_cc and email_to into partner_ids """ - # filter template values + if context is None: + context = {} if fields is None: fields = ['subject', 'body_html', 'email_from', 'email_to', 'partner_to', 'email_cc', 'reply_to', 'attachment_ids', 'mail_server_id'] - returned_fields = fields + ['attachments'] + returned_fields = fields + ['partner_ids', 'attachments'] values = dict.fromkeys(res_ids, False) - template_values = self.pool.get('email.template').generate_email_batch(cr, uid, template_id, res_ids, fields=fields, context=context) + ctx = dict(context, tpl_partners_only=True) + template_values = self.pool.get('email.template').generate_email_batch(cr, uid, template_id, res_ids, fields=fields, context=ctx) for res_id in res_ids: res_id_values = dict((field, template_values[res_id][field]) for field in returned_fields if template_values[res_id].get(field)) res_id_values['body'] = res_id_values.pop('body_html', '') - - # transform email_to, email_cc into partner_ids - 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, res_id_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: - res_id_values['partner_ids'] = list(partner_ids) - values[res_id] = res_id_values return values def render_message_batch(self, cr, uid, wizard, res_ids, context=None): """ Override to handle templates. """ + # generate composer values + composer_values = super(mail_compose_message, self).render_message_batch(cr, uid, wizard, res_ids, context) + # generate template-based values if wizard.template_id: template_values = self.generate_email_for_composer_batch( @@ -206,10 +188,13 @@ class mail_compose_message(osv.TransientModel): context=context) else: template_values = dict.fromkeys(res_ids, dict()) - # generate composer values - composer_values = super(mail_compose_message, self).render_message_batch(cr, uid, wizard, res_ids, context) for res_id in res_ids: + if wizard.template_id: + # recipients are managed by the template + composer_values[res_id].pop('partner_ids') + composer_values[res_id].pop('email_to') + composer_values[res_id].pop('email_cc') # remove attachments from template values as they should not be rendered template_values[res_id].pop('attachment_ids', None) # update template values by composer values