[FIX] mail: convert attachments to binary as it is the expected format of message_post opw 604205

The double convertion (render_message and send_mail) is done to keep the API but should be changed in next version.

bzr revid: mat@openerp.com-20140415161441-q6pfueetvv0namgw
This commit is contained in:
Martin Trigaux 2014-04-15 18:14:41 +02:00
parent 998b290409
commit 1ab962d338
2 changed files with 7 additions and 2 deletions

View File

@ -305,7 +305,7 @@ class email_template(osv.osv):
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`.
format [(report_name, data)] where data is base64 encoded.
"""
if context is None:
context = {}
@ -340,6 +340,7 @@ class email_template(osv.osv):
ctx['lang'] = self.render_template(cr, uid, template.lang, template.model, res_id, context)
service = netsvc.LocalService(report_service)
(result, format) = service.create(cr, uid, [res_id], {'model': template.model}, ctx)
# TODO in trunk, change return format to binary to match message_post expected format
result = base64.b64encode(result)
if not report_name:
report_name = report_service

View File

@ -19,6 +19,7 @@
#
##############################################################################
import base64
import re
from openerp import tools
from openerp import SUPERUSER_ID
@ -237,12 +238,15 @@ class mail_compose_message(osv.TransientModel):
'parent_id': wizard.parent_id and wizard.parent_id.id,
'partner_ids': [partner.id for partner in wizard.partner_ids],
'attachment_ids': [attach.id for attach in wizard.attachment_ids],
'attachments': [],
}
# mass mailing: render and override default values
if mass_mail_mode and wizard.model:
email_dict = self.render_message(cr, uid, wizard, res_id, context=context)
post_values['partner_ids'] += email_dict.pop('partner_ids', [])
post_values['attachments'] = email_dict.pop('attachments', [])
for filename, attachment_data in email_dict.pop('attachments', []):
# decode as render message return in base64 while message_post expect binary
post_values['attachments'].append((filename, base64.b64decode(attachment_data)))
attachment_ids = []
for attach_id in post_values.pop('attachment_ids'):
new_attach_id = ir_attachment_obj.copy(cr, uid, attach_id, {'res_model': self._name, 'res_id': wizard.id}, context=context)