Commit 72c76928 authored by Jérome Perrin's avatar Jérome Perrin

CRM py3

parent 91f2ac01
...@@ -49,9 +49,10 @@ except ImportError: ...@@ -49,9 +49,10 @@ except ImportError:
""" """
if six.PY2: if six.PY2:
from email import message_from_string as message_from_x from email import message_from_string as message_from_bytes
else: else:
from email import message_from_bytes as message_from_x from email import message_from_bytes
from email.utils import parsedate_tz, mktime_tz from email.utils import parsedate_tz, mktime_tz
DEFAULT_TEXT_FORMAT = 'text/html' DEFAULT_TEXT_FORMAT = 'text/html'
...@@ -162,7 +163,7 @@ class EmailDocument(TextDocument, MailMessageMixin): ...@@ -162,7 +163,7 @@ class EmailDocument(TextDocument, MailMessageMixin):
# store it in 'data' property. # store it in 'data' property.
result = getattr(self, '_v_message', None) result = getattr(self, '_v_message', None)
if result is None: if result is None:
data = self.getData() data = bytes(self.getData() or b'')
if not data: if not data:
# Generated a mail message temporarily to provide backward compatibility. # Generated a mail message temporarily to provide backward compatibility.
document_type_list = list(self.getPortalEmbeddedDocumentTypeList()) + list(self.getPortalDocumentTypeList()) document_type_list = list(self.getPortalEmbeddedDocumentTypeList()) + list(self.getPortalDocumentTypeList())
...@@ -174,7 +175,9 @@ class EmailDocument(TextDocument, MailMessageMixin): ...@@ -174,7 +175,9 @@ class EmailDocument(TextDocument, MailMessageMixin):
content_type=self.getContentType(), content_type=self.getContentType(),
embedded_file_list=self.getAggregateValueList(portal_type=document_type_list), embedded_file_list=self.getAggregateValueList(portal_type=document_type_list),
) )
result = message_from_x(data) if six.PY3:
data = data.encode()
result = message_from_bytes(data)
self._v_message = result self._v_message = result
return result return result
......
...@@ -205,9 +205,9 @@ class File(Document, OFS_File): ...@@ -205,9 +205,9 @@ class File(Document, OFS_File):
security.declareProtected(Permissions.AccessContentsInformation, 'getMimeTypeAndContent') security.declareProtected(Permissions.AccessContentsInformation, 'getMimeTypeAndContent')
def getMimeTypeAndContent(self): def getMimeTypeAndContent(self):
# type: () -> tuple[str, bytes]
"""This method returns a tuple which contains mimetype and content.""" """This method returns a tuple which contains mimetype and content."""
from erp5.component.document.EmailDocument import MimeTypeException from erp5.component.document.EmailDocument import MimeTypeException
# return a tuple (mime_type, data)
content = None content = None
mime_type = self.getContentType() mime_type = self.getContentType()
...@@ -229,6 +229,8 @@ class File(Document, OFS_File): ...@@ -229,6 +229,8 @@ class File(Document, OFS_File):
elif getattr(self, 'getBaseData', None) is not None: elif getattr(self, 'getBaseData', None) is not None:
content = self.getBaseData() content = self.getBaseData()
if isinstance(content, six.text_type):
content = content.encode('utf-8')
if content and not isinstance(content, bytes): if content and not isinstance(content, bytes):
content = bytes(content) content = bytes(content)
......
...@@ -40,22 +40,24 @@ import six ...@@ -40,22 +40,24 @@ import six
filename_regexp = 'name="([^"]*)"' filename_regexp = 'name="([^"]*)"'
def testCharsetAndConvert(text_content, content_type, encoding): def testCharsetAndConvert(text_content, content_type, encoding):
try: if not isinstance(text_content, six.text_type):
if encoding is not None: try:
text_content = text_content.decode(encoding) if encoding is not None:
else:
if six.PY2:
text_content = text_content.decode().encode('utf-8')
except (UnicodeDecodeError, LookupError):
encoding = guessEncodingFromText(text_content, content_type)
if encoding is not None:
try:
text_content = text_content.decode(encoding) text_content = text_content.decode(encoding)
except (UnicodeDecodeError, LookupError): else:
# TODO: errors= repr ? text_content = text_content.decode()
if six.PY2:
text_content = text_content.encode('utf-8')
except (UnicodeDecodeError, LookupError):
encoding = guessEncodingFromText(text_content, content_type)
if encoding is not None:
try:
text_content = text_content.decode(encoding)
except (UnicodeDecodeError, LookupError):
# TODO: errors= repr ?
text_content = repr(text_content)[1:-1]
else:
text_content = repr(text_content)[1:-1] text_content = repr(text_content)[1:-1]
else:
text_content = repr(text_content)[1:-1]
return text_content, encoding return text_content, encoding
...@@ -118,18 +120,21 @@ class MailMessageMixin: ...@@ -118,18 +120,21 @@ class MailMessageMixin:
result = {} result = {}
for (name, value) in self._getMessage().items(): for (name, value) in self._getMessage().items():
try: try:
decoded_header = decode_header(value) decoded_header_parts = decode_header(value)
except HeaderParseError as error_message: except HeaderParseError as error_message:
decoded_header = () decoded_header_parts = ()
LOG('MailMessageMixin.getContentInformation', INFO, LOG('MailMessageMixin.getContentInformation', INFO,
'Failed to decode %s header of %s with error: %s' % 'Failed to decode %s header of %s with error: %s' %
(name, self.getPath(), error_message)) (name, self.getPath(), error_message))
for text, encoding in decoded_header: header_parts = []
text, encoding = testCharsetAndConvert(text, 'text/plain', encoding) for text, encoding in decoded_header_parts:
if name in result: text, _ = testCharsetAndConvert(text, 'text/plain', encoding)
result[name] = '%s %s' % (result[name], text) header_parts.append(text)
else: if six.PY3:
result[name] = text result[name] = ''.join(header_parts)
else:
# https://bugs.python.org/issue1079
result[name] = ' '.join(header_parts)
return result return result
security.declareProtected(Permissions.AccessContentsInformation, 'getAttachmentInformationList') security.declareProtected(Permissions.AccessContentsInformation, 'getAttachmentInformationList')
...@@ -215,6 +220,8 @@ class MailMessageMixin: ...@@ -215,6 +220,8 @@ class MailMessageMixin:
encoding=part_encoding, encoding=part_encoding,
index=index) # add index to generate index=index) # add index to generate
# a unique cache key per attachment # a unique cache key per attachment
if six.PY3:
content = content.encode()
else: else:
content = part.get_payload(decode=1) content = part.get_payload(decode=1)
return content return content
......
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