From 83bc616db700406c8254ae036ef5f41796e80a79 Mon Sep 17 00:00:00 2001 From: Filipp Lepalaan Date: Sun, 6 Jun 2021 14:34:24 +0300 Subject: Python 3 fixes --- servo/models/note.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'servo/models/note.py') diff --git a/servo/models/note.py b/servo/models/note.py index e7ab2dd..5650c4c 100644 --- a/servo/models/note.py +++ b/servo/models/note.py @@ -291,6 +291,11 @@ class Note(MPTTModel): Creates a new Note from an email message """ sender = decode_header(msg['From']) + + if type(sender[0][0]) == str: + enc = sender[0][1] or 'utf-8' + sender = [[sender[0][0].encode(enc), enc]] + detected = chardet.detect(sender[0][0]).get('encoding') sender = [i[0].decode(i[1] or detected) for i in sender] sender = ' '.join(sender) @@ -300,20 +305,28 @@ class Note(MPTTModel): note.is_read = False note.is_reported = False note.recipient = msg['To'] + note.type = cls.T_CUSTOMER_NOTE subject = decode_header(msg['Subject'])[0] + + if type(subject[0][0]) == str: + enc = subject[1] or 'utf-8' + subject = [subject[0].encode(enc), enc] + detected = chardet.detect(subject[0]).get('encoding') - note.subject = subject[0].decode(subject[1] or detected) + encoding = subject[1] or detected + note.subject = subject[0].decode(encoding) note.find_parent(note.subject) for part in msg.walk(): + t, s = part.get_content_type().split('/', 1) charset = part.get_content_charset() or "latin1" if t == "text": payload = part.get_payload(decode=True) - note.body = unicode(payload, str(charset), "ignore") + note.body = payload.decode(charset) if s == "html": h = html2text.HTML2Text() h.ignore_images = True @@ -321,7 +334,7 @@ class Note(MPTTModel): else: note.save() if part.get_filename(): - filename = unicode(part.get_filename()) + filename = part.get_filename() payload = part.get_payload() content = base64.b64decode(payload) content = ContentFile(content, filename) -- cgit v1.2.3