forked from mirror/django-mailbox
Adding Python3 support.
This commit is contained in:
parent
4567312402
commit
364a3061c2
3 changed files with 35 additions and 22 deletions
|
|
@ -1,10 +1,14 @@
|
||||||
import email
|
import email
|
||||||
from email.message import Message as EmailMessage
|
from email.message import Message as EmailMessage
|
||||||
from email.utils import formatdate
|
from email.utils import formatdate, parseaddr
|
||||||
import rfc822
|
|
||||||
import urllib
|
import urllib
|
||||||
import urlparse
|
|
||||||
import os
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
try:
|
||||||
|
import urllib.parse as urlparse
|
||||||
|
except ImportError:
|
||||||
|
import urlparse
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.mail.message import make_msgid
|
from django.core.mail.message import make_msgid
|
||||||
|
|
@ -337,14 +341,14 @@ class Message(models.Model):
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def from_address(self):
|
def from_address(self):
|
||||||
return rfc822.parseaddr(self.from_header)[1]
|
return parseaddr(self.from_header)[1]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def to_addresses(self):
|
def to_addresses(self):
|
||||||
addresses = []
|
addresses = []
|
||||||
for address in self.to_header.split(','):
|
for address in self.to_header.split(','):
|
||||||
addresses.append(
|
addresses.append(
|
||||||
rfc822.parseaddr(
|
parseaddr(
|
||||||
address
|
address
|
||||||
)[1]
|
)[1]
|
||||||
)
|
)
|
||||||
|
|
@ -408,7 +412,9 @@ class Message(models.Model):
|
||||||
encoded bytes is probably the safest answer.
|
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):
|
def delete(self, *args, **kwargs):
|
||||||
for attachment in self.attachments.all():
|
for attachment in self.attachments.all():
|
||||||
|
|
|
||||||
|
|
@ -2,17 +2,27 @@ import email
|
||||||
import os.path
|
import os.path
|
||||||
|
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
import mimic
|
import six
|
||||||
|
|
||||||
import django_mailbox
|
import django_mailbox
|
||||||
from django_mailbox.models import Mailbox, Message
|
from django_mailbox.models import Mailbox, Message
|
||||||
|
|
||||||
|
|
||||||
class EmailMessageTestCase(TestCase):
|
class TestMailbox(TestCase):
|
||||||
def setUp(self):
|
def test_protocol_info(self):
|
||||||
super(EmailMessageTestCase, self).setUp()
|
mailbox = Mailbox()
|
||||||
self.mimic = mimic.Mimic()
|
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):
|
def _get_email_object(self, name):
|
||||||
with open(os.path.join(os.path.dirname(__file__), name), 'r') as f:
|
with open(os.path.join(os.path.dirname(__file__), name), 'r') as f:
|
||||||
return email.message_from_string(
|
return email.message_from_string(
|
||||||
|
|
@ -23,8 +33,6 @@ class EmailMessageTestCase(TestCase):
|
||||||
for message in Message.objects.all():
|
for message in Message.objects.all():
|
||||||
message.delete()
|
message.delete()
|
||||||
|
|
||||||
self.mimic.verify_all()
|
|
||||||
|
|
||||||
|
|
||||||
class TestProcessEmail(EmailMessageTestCase):
|
class TestProcessEmail(EmailMessageTestCase):
|
||||||
def test_message_without_attachments(self):
|
def test_message_without_attachments(self):
|
||||||
|
|
@ -140,15 +148,12 @@ class TestGetMessage(EmailMessageTestCase):
|
||||||
'message_with_long_text_lines.eml'
|
'message_with_long_text_lines.eml'
|
||||||
)
|
)
|
||||||
|
|
||||||
self.mimic.stub_out_with_mock(message, 'get_email_object')
|
message.get_email_object = lambda: email_object
|
||||||
message.get_email_object().and_return(email_object)
|
|
||||||
|
|
||||||
self.mimic.replay_all()
|
|
||||||
|
|
||||||
actual_text = message.get_text_body()
|
actual_text = message.get_text_body()
|
||||||
expected_text = (
|
expected_text = (
|
||||||
u'The one of us with a bike pump is far ahead, '
|
'The one of us with a bike pump is far ahead, '
|
||||||
u'but a man stopped to help us and gave us his pump.'
|
'but a man stopped to help us and gave us his pump.'
|
||||||
)
|
)
|
||||||
|
|
||||||
self.assertEquals(
|
self.assertEquals(
|
||||||
|
|
@ -165,7 +170,7 @@ class TestMessageGetEmailObject(TestCase):
|
||||||
'generic_message.eml'
|
'generic_message.eml'
|
||||||
)
|
)
|
||||||
) as f:
|
) as f:
|
||||||
unicode_body = unicode(f.read())
|
unicode_body = six.u(f.read())
|
||||||
|
|
||||||
message = Message()
|
message = Message()
|
||||||
message.body = unicode_body
|
message.body = unicode_body
|
||||||
|
|
|
||||||
6
setup.py
6
setup.py
|
|
@ -2,7 +2,6 @@ from setuptools import setup
|
||||||
|
|
||||||
tests_require=[
|
tests_require=[
|
||||||
'django',
|
'django',
|
||||||
'mimic',
|
|
||||||
]
|
]
|
||||||
|
|
||||||
setup(
|
setup(
|
||||||
|
|
@ -34,5 +33,8 @@ setup(
|
||||||
'django_mailbox.migrations',
|
'django_mailbox.migrations',
|
||||||
'django_mailbox.transports',
|
'django_mailbox.transports',
|
||||||
'django_mailbox.tests',
|
'django_mailbox.tests',
|
||||||
],
|
],
|
||||||
|
install_requires=[
|
||||||
|
'six'
|
||||||
|
]
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue