1
0
Fork 0

Adding Python3 support.

This commit is contained in:
Adam Coddington 2013-06-22 15:42:50 -07:00
parent 4567312402
commit 364a3061c2
3 changed files with 35 additions and 22 deletions

View file

@ -1,10 +1,14 @@
import email
from email.message import Message as EmailMessage
from email.utils import formatdate
import rfc822
from email.utils import formatdate, parseaddr
import urllib
import urlparse
import os
import sys
try:
import urllib.parse as urlparse
except ImportError:
import urlparse
from django.conf import settings
from django.core.mail.message import make_msgid
@ -337,14 +341,14 @@ class Message(models.Model):
@property
def from_address(self):
return rfc822.parseaddr(self.from_header)[1]
return parseaddr(self.from_header)[1]
@property
def to_addresses(self):
addresses = []
for address in self.to_header.split(','):
addresses.append(
rfc822.parseaddr(
parseaddr(
address
)[1]
)
@ -408,7 +412,9 @@ class Message(models.Model):
encoded bytes is probably the safest answer.
"""
return email.message_from_string(self.body.encode('utf-8'))
if sys.version_info < (2, 7):
return email.message_from_string(self.body.encode('utf-8'))
return email.message_from_string(self.body)
def delete(self, *args, **kwargs):
for attachment in self.attachments.all():

View file

@ -2,17 +2,27 @@ import email
import os.path
from django.test import TestCase
import mimic
import six
import django_mailbox
from django_mailbox.models import Mailbox, Message
class EmailMessageTestCase(TestCase):
def setUp(self):
super(EmailMessageTestCase, self).setUp()
self.mimic = mimic.Mimic()
class TestMailbox(TestCase):
def test_protocol_info(self):
mailbox = Mailbox()
mailbox.uri = 'alpha://test.com'
expected_protocol = 'alpha'
actual_protocol = mailbox._protocol_info.scheme
self.assertEquals(
expected_protocol,
actual_protocol,
)
class EmailMessageTestCase(TestCase):
def _get_email_object(self, name):
with open(os.path.join(os.path.dirname(__file__), name), 'r') as f:
return email.message_from_string(
@ -23,8 +33,6 @@ class EmailMessageTestCase(TestCase):
for message in Message.objects.all():
message.delete()
self.mimic.verify_all()
class TestProcessEmail(EmailMessageTestCase):
def test_message_without_attachments(self):
@ -140,15 +148,12 @@ class TestGetMessage(EmailMessageTestCase):
'message_with_long_text_lines.eml'
)
self.mimic.stub_out_with_mock(message, 'get_email_object')
message.get_email_object().and_return(email_object)
self.mimic.replay_all()
message.get_email_object = lambda: email_object
actual_text = message.get_text_body()
expected_text = (
u'The one of us with a bike pump is far ahead, '
u'but a man stopped to help us and gave us his pump.'
'The one of us with a bike pump is far ahead, '
'but a man stopped to help us and gave us his pump.'
)
self.assertEquals(
@ -165,7 +170,7 @@ class TestMessageGetEmailObject(TestCase):
'generic_message.eml'
)
) as f:
unicode_body = unicode(f.read())
unicode_body = six.u(f.read())
message = Message()
message.body = unicode_body