From 1ab962d33829fd07d0cef84e3dbe4d913333aadb Mon Sep 17 00:00:00 2001 From: Martin Trigaux Date: Tue, 15 Apr 2014 18:14:41 +0200 Subject: [PATCH] [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 --- addons/email_template/email_template.py | 3 ++- addons/mail/wizard/mail_compose_message.py | 6 +++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/addons/email_template/email_template.py b/addons/email_template/email_template.py index 8890226f28b..30417acb70a 100644 --- a/addons/email_template/email_template.py +++ b/addons/email_template/email_template.py @@ -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 diff --git a/addons/mail/wizard/mail_compose_message.py b/addons/mail/wizard/mail_compose_message.py index 301dff515b3..7e269ab7379 100644 --- a/addons/mail/wizard/mail_compose_message.py +++ b/addons/mail/wizard/mail_compose_message.py @@ -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)