Skip to content

Commit

Permalink
Refactoring imports for Python3 support; refactoring unicode payload
Browse files Browse the repository at this point in the history
handling.  Refs #13.
  • Loading branch information
Adam Coddington committed Apr 24, 2014
1 parent ff68e56 commit b4c335c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
35 changes: 21 additions & 14 deletions django_mailbox/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,12 @@
from email.message import Message as EmailMessage
from email.utils import formatdate, parseaddr
from email.encoders import encode_base64
import urllib
import mimetypes
import os.path
from quopri import encode as encode_quopri
import sys
import uuid

try:
import urllib.parse as urlparse
except ImportError:
import urlparse

from django.conf import settings
from django.core.mail.message import make_msgid
from django.core.files.base import ContentFile
Expand All @@ -25,6 +19,7 @@
MMDFTransport
from django_mailbox.signals import message_received
import six
from six.moves.urllib.parse import unquote, urlparse


STRIP_UNALLOWED_MIMETYPES = getattr(
Expand Down Expand Up @@ -117,7 +112,7 @@ class Mailbox(models.Model):

@property
def _protocol_info(self):
return urlparse.urlparse(self.uri)
return urlparse(self.uri)

@property
def _domain(self):
Expand All @@ -129,11 +124,11 @@ def port(self):

@property
def username(self):
return urllib.unquote(self._protocol_info.username)
return unquote(self._protocol_info.username)

@property
def password(self):
return urllib.unquote(self._protocol_info.password)
return unquote(self._protocol_info.password)

@property
def location(self):
Expand Down Expand Up @@ -249,6 +244,21 @@ def _get_dehydrated_message(self, msg, record):
placeholder[ATTACHMENT_INTERPOLATION_HEADER] = str(attachment.pk)
new = placeholder
else:
content_charset = msg.get_content_charset()
if not content_charset:
content_charset = 'ascii'
try:
# Make sure that the payload can be properly decoded in the
# defined charset, if it can't, let's mash some things
# inside the payload :-\
msg.get_payload(decode=True).decode(content_charset)
except UnicodeDecodeError:
msg.set_payload(
msg.get_payload(decode=True).decode(
content_charset,
'replace'
).encode(content_charset, 'replace')
)
new = msg
return new

Expand Down Expand Up @@ -495,18 +505,15 @@ def get_body(self):
return self.body.encode('utf-8')

def set_body(self, body):
if sys.version_info >= (3, 0):
if six.PY3:
body = body.encode('utf-8')
self.encoded = True
self.body = base64.b64encode(body).decode('ascii')

def get_email_object(self):
""" Returns an `email.message.Message` instance for this message."""
body = self.get_body()
if sys.version_info < (3, 0):
flat = email.message_from_string(body)
else:
flat = email.message_from_bytes(body)
flat = email.message_from_bytes(body)
return self._rehydrate(flat)

def delete(self, *args, **kwargs):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Date: Fri, 30 Jan 2012 11:30:01 PST
Subject: Tjest
Subject: Tjest Wrong
From: "Somebody" <[email protected]>
To: [email protected]
MIME-Version: 1.0
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@
'django_mailbox.tests',
],
install_requires=[
'six'
'six>=1.6.1'
]
)

0 comments on commit b4c335c

Please sign in to comment.