Commit 6ad08d5c authored by Jérome Perrin's avatar Jérome Perrin

*: make attachment_list of notification API correctly use bytes

parent 23544c2a
# coding: utf-8
import unicodedata
from six.moves import cStringIO as StringIO
import six
from io import BytesIO
import zipfile
from Products.ERP5Type.Message import translateString
......@@ -22,7 +23,7 @@ if test_compta_demat_compatibility:
'NFKD', fec_file.replace(u"€", "EUR")
).encode('ascii', 'ignore')
zipbuffer = StringIO()
zipbuffer = BytesIO()
zipfilename = at_date.strftime('FEC-%Y%m%d.zip')
zipfileobj = zipfile.ZipFile(zipbuffer, 'w', compression=zipfile.ZIP_DEFLATED)
filename = 'FEC.xml'
......@@ -42,9 +43,15 @@ attachment_list = (
'content': zipbuffer.getvalue(),
'name': zipfilename, }, )
subject = translateString('French Accounting Transaction File')
if six.PY2:
subject = unicode(subject)
else:
subject = str(subject)
portal.ERP5Site_notifyReportComplete(
user_name=user_name,
subject=unicode(translateString('French Accounting Transaction File')),
subject=subject,
message='',
attachment_list=attachment_list)
......
......@@ -121,9 +121,9 @@ class Url(Coordinate, UrlMixin):
This method was previously named 'SendMail' and is used to send email
* attachment_list is a list of dictionnaries with those keys:
- name : name of the attachment,
- content: data of the attachment
- mime_type: mime-type corresponding to the attachment
- name (str): name of the attachment,
- content (bytes): data of the attachment
- mime_type (str): mime-type corresponding to the attachment
* extra_headers is a dictionnary of custom headers to add to the email.
"X-" prefix is automatically added to those headers.
"""
......
......@@ -6,10 +6,10 @@
* body: body of the message as UTF-8 encoded string
* content_type: mime type of this message, can be text/html for
HTML message or anything else for text/plain message.
* attachment_list: a list of attachement mapping in format:
- mime_type: mime type of thsi attachement
- content: file content of the attachement, as a string
- name: displayed name of this attachements
* attachment_list: a list of attachment mapping in format:
- mime_type (str): mime type of thsi attachment
- content (bytes): file content of the attachment
- name (str): displayed name of this attachment
* embedded_file_list: a list of ERP5 File to use as attachments.
* extra_header_dict: additional email headers
......
......@@ -54,6 +54,7 @@ def decode_email(file_):
for key, value in msg.items():
decoded_value_list = decode_header(value)
unicode_value = make_header(decoded_value_list)
# TODO PY3
new_value = unicode_value.__unicode__().encode('utf-8')
theMail['headers'][key.lower()] = new_value
# Filter mail addresses
......@@ -302,12 +303,12 @@ class TestNotificationTool(ERP5TypeTestCase):
attachment_list=[
{
'name': 'Attachment 1',
'content': 'Text 1',
'content': b'Text 1',
'mime_type': 'text/plain',
},
{
'name': 'Attachment 2',
'content': 'Text 2',
'content': b'Text 2',
'mime_type': 'application/octet-stream',
},
])
......@@ -322,8 +323,11 @@ class TestNotificationTool(ERP5TypeTestCase):
mail_dict = decode_email(messageText)
self.assertEqual(mail_dict['headers']['subject'], 'Subject')
self.assertEqual(mail_dict['body'], 'Message')
# "Attachment 1" is decoded as str because there was a charset in the
# message, this is how this `decode_email` utility function from this
# test works.
self.assertSameSet([('Attachment 1', 'text/plain', 'Text 1'),
('Attachment 2', 'application/octet-stream', 'Text 2')],
('Attachment 2', 'application/octet-stream', b'Text 2')],
mail_dict['attachment_list'])
def test_07_AttachmentMessage(self):
......
......@@ -30,7 +30,7 @@ with portal.Localizer.translationContext(localizer_language):
attachment_name = attachment_name[:-1]
attachment_list = (
{'mime_type': (request.RESPONSE.getHeader('content-type') or 'application/octet-stream;').split(';')[0],
'content': '%s' % report_data,
'content': bytes(report_data),
'name': attachment_name},)
getattr(portal, notify_report_complete_script_id)(
......
......@@ -51,7 +51,7 @@ with portal.Localizer.translationContext(localizer_language):
attachment_list = (
{'mime_type': (request.RESPONSE.getHeader('content-type') or 'application/octet-stream;').split(';')[0],
'content': '%s' % report_data,
'content': bytes(report_data),
'name': attachment_name},)
getattr(portal, notify_report_complete_script_id)(
......
......@@ -355,7 +355,7 @@ class Alarm(XMLObject, PeriodicityMixin):
for x in result_list]
rendered_alarm_result = '\n'.join(rendered_alarm_result_list)
attachment_list.append({'name': 'alarm_result.txt',
'content': rendered_alarm_result,
'content': rendered_alarm_result.encode(),
'mime_type': 'text/plain'})
notification_tool.sendMessage(recipient=candidate_list,
......
......@@ -109,9 +109,9 @@ def buildEmailMessage(from_url, to_url, msg=None,
sent by Zope MailHost.
* attachment_list is a list of dictionaries with those keys:
- name : name of the attachment,
- content: data of the attachment
- mime_type: mime-type corresponding to the attachment
- name (str): name of the attachment
- content (bytes): data of the attachment
- mime_type (str): mime-type corresponding to the attachment
* extra_headers is a dictionary of custom headers to add to the email.
"X-" prefix is automatically added to those headers.
* additional_headers is similar to extra_headers, but no prefix is added.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment