[FIX] email_template: support of email_recipients in send_mail.
email_recipients is a char fields holding a comma separated list of partner_ids that are the email recipients. This field is correctly handled in the composer but was not taken into account in the send_mail method email_template. However due to model limitations, this field can only be taken into account when sending the email directly using force_send. When the email is queued, no field is available to hold the value of email_recipients. This is already fixed in trunk/v8 as a field has been added to handle this value. [FIX] mail: when sending email to some recipients_ids, filter to have only the existing partner_ids. Trusting email template to generate valid partner_ids is a bit risky. [TESTS] email_template: added tests for send_mail bzr revid: tde@openerp.com-20131220144652-h18yam60vpedbh7x
This commit is contained in:
parent
73dfa69e34
commit
b92ad39005
|
@ -377,7 +377,17 @@ 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)
|
||||
assert values.get('email_from'), 'email_from is missing or empty after template rendering, send_mail() cannot proceed'
|
||||
del values['email_recipients'] # TODO Properly use them.
|
||||
|
||||
# process email_recipients 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
|
||||
recipient_ids = []
|
||||
email_recipients = values.pop('email_recipients', '')
|
||||
if email_recipients:
|
||||
for partner_id in email_recipients.split(','):
|
||||
if partner_id: # placeholders could generate '', 3, 2 due to some empty field values
|
||||
recipient_ids.append(int(partner_id))
|
||||
|
||||
attachment_ids = values.pop('attachment_ids', [])
|
||||
attachments = values.pop('attachments', [])
|
||||
msg_id = mail_mail.create(cr, uid, values, context=context)
|
||||
|
@ -386,11 +396,11 @@ class email_template(osv.osv):
|
|||
# manage attachments
|
||||
for attachment in attachments:
|
||||
attachment_data = {
|
||||
'name': attachment[0],
|
||||
'datas_fname': attachment[0],
|
||||
'datas': attachment[1],
|
||||
'res_model': 'mail.message',
|
||||
'res_id': mail.mail_message_id.id,
|
||||
'name': attachment[0],
|
||||
'datas_fname': attachment[0],
|
||||
'datas': attachment[1],
|
||||
'res_model': 'mail.message',
|
||||
'res_id': mail.mail_message_id.id,
|
||||
}
|
||||
context.pop('default_type', None)
|
||||
attachment_ids.append(ir_attachment.create(cr, uid, attachment_data, context=context))
|
||||
|
@ -399,7 +409,7 @@ class email_template(osv.osv):
|
|||
mail_mail.write(cr, uid, msg_id, {'attachment_ids': [(6, 0, attachment_ids)]}, context=context)
|
||||
|
||||
if force_send:
|
||||
mail_mail.send(cr, uid, [msg_id], context=context)
|
||||
mail_mail.send(cr, uid, [msg_id], recipient_ids=recipient_ids, context=context)
|
||||
return msg_id
|
||||
|
||||
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|
||||
|
|
|
@ -21,6 +21,7 @@
|
|||
|
||||
import base64
|
||||
from openerp.addons.mail.tests.test_mail_base import TestMailBase
|
||||
from openerp.tools import mute_logger
|
||||
|
||||
|
||||
class test_message_compose(TestMailBase):
|
||||
|
@ -199,3 +200,39 @@ class test_message_compose(TestMailBase):
|
|||
# Generate messsage with default email and partner on template
|
||||
mail_value = mail_compose.generate_email_for_composer(cr, uid, email_template_id, uid)
|
||||
self.assertEqual(set(mail_value['partner_ids']), set(send_to), 'mail.message partner_ids list created by template is incorrect')
|
||||
|
||||
@mute_logger('openerp.osv.orm', 'openerp.osv.orm')
|
||||
def test_10_email_templating(self):
|
||||
""" Tests designed for the mail.compose.message wizard updated by email_template. """
|
||||
cr, uid, context = self.cr, self.uid, {}
|
||||
|
||||
# create the email.template on mail.group model
|
||||
group_model_id = self.registry('ir.model').search(cr, uid, [('model', '=', 'mail.group')])[0]
|
||||
email_template = self.registry('email.template')
|
||||
email_template_id = email_template.create(cr, uid, {
|
||||
'model_id': group_model_id,
|
||||
'name': 'Pigs Template',
|
||||
'email_from': 'Raoul Grosbedon <raoul@example.com>',
|
||||
'subject': '${object.name}',
|
||||
'body_html': '${object.description}',
|
||||
'user_signature': True,
|
||||
'email_to': 'b@b.b c@c.c',
|
||||
'email_cc': 'd@d.d',
|
||||
'email_recipients': '${user.partner_id.id},%s,%s,-1' % (self.user_raoul.partner_id.id, self.user_bert.partner_id.id)
|
||||
})
|
||||
|
||||
# not force send: email_recipients is not taken into account
|
||||
msg_id = email_template.send_mail(cr, uid, email_template_id, self.group_pigs_id, context=context)
|
||||
mail = self.mail_mail.browse(cr, uid, msg_id, context=context)
|
||||
self.assertEqual(mail.subject, 'Pigs', 'email_template: send_mail: wrong subject')
|
||||
self.assertEqual(mail.email_to, 'b@b.b c@c.c', 'email_template: send_mail: wrong email_to')
|
||||
self.assertEqual(mail.email_cc, 'd@d.d', 'email_template: send_mail: wrong email_cc')
|
||||
|
||||
# force send: take email_recipients into account
|
||||
email_template.send_mail(cr, uid, email_template_id, self.group_pigs_id, force_send=True, context=context)
|
||||
sent_emails = self._build_email_kwargs_list
|
||||
email_to_lst = ['"Followers of Pigs" <admin@example.com>', '"Followers of Pigs" <raoul@raoul.fr>', '"Followers of Pigs" <bert@bert.fr>']
|
||||
self.assertEqual(len(sent_emails), 3, 'email_template: send_mail: 3 valid email recipients -> should send 3 emails')
|
||||
for email in sent_emails:
|
||||
self.assertEqual(len(email['email_to']), 1, 'email_template: send_mail: email_recipient should send email to one recipient at a time')
|
||||
self.assertIn(email['email_to'][0], email_to_lst, 'email_template: send_mail: wrong email_recipients')
|
||||
|
|
|
@ -282,7 +282,9 @@ class mail_mail(osv.Model):
|
|||
# specific behavior to customize the send email for notified partners
|
||||
email_list = []
|
||||
if recipient_ids:
|
||||
for partner in self.pool.get('res.partner').browse(cr, SUPERUSER_ID, recipient_ids, context=context):
|
||||
partner_obj = self.pool.get('res.partner')
|
||||
existing_recipient_ids = partner_obj.exists(cr, SUPERUSER_ID, recipient_ids, context=context)
|
||||
for partner in partner_obj.browse(cr, SUPERUSER_ID, existing_recipient_ids, context=context):
|
||||
email_list.append(self.send_get_email_dict(cr, uid, mail, partner=partner, context=context))
|
||||
else:
|
||||
email_list.append(self.send_get_email_dict(cr, uid, mail, context=context))
|
||||
|
|
Loading…
Reference in New Issue