Commit ae240084 authored by Jérome Perrin's avatar Jérome Perrin

CRM py3

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