Skip to content

Commit c05f032

Browse files
author
Rich Leland
committed
Test and first crack at code for attachment support
1 parent 0aa08ed commit c05f032

File tree

3 files changed

+34
-15
lines changed

3 files changed

+34
-15
lines changed

sparkpost/django/message.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,14 @@ def __init__(self, message):
4545
)
4646

4747
if message.attachments:
48-
raise UnsupportedContent(
49-
'The SparkPost Django email backend does not '
50-
'currently support attachment.'
51-
)
48+
formatted['attachments'] = []
49+
for attachment in message.attachments:
50+
filename, content, mimetype = attachment
51+
formatted['attachments'].append({
52+
'name': filename,
53+
'data': content,
54+
'type': mimetype
55+
})
56+
print(message.attachments)
5257

5358
return super(SparkPostMessage, self).__init__(formatted)

test/django/test_message.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
try:
2-
from StringIO import StringIO
3-
except ImportError:
4-
from io import StringIO
1+
import os
52

6-
import pytest
73
from django.core.mail import EmailMultiAlternatives
84
from django.core.mail.message import EmailMessage
95

10-
from sparkpost.django.exceptions import UnsupportedContent
116
from sparkpost.django.message import SparkPostMessage
127
from .utils import at_least_version
138

@@ -66,13 +61,31 @@ def test_cc_bcc():
6661

6762

6863
def test_attachment():
69-
attachment = StringIO()
70-
attachment.write('hello file')
7164
email_message = EmailMessage(**base_options)
72-
email_message.attach('file.txt', attachment, 'text/plain')
65+
email_message.attach('file.txt', 'test content', 'text/plain')
7366

74-
with pytest.raises(UnsupportedContent):
75-
SparkPostMessage(email_message)
67+
current_dir = os.path.dirname(os.path.abspath(__file__))
68+
test_file = os.path.join(current_dir, 'testfile.txt')
69+
email_message.attach_file(test_file)
70+
71+
actual = SparkPostMessage(email_message)
72+
expected = dict(
73+
attachments=[
74+
{
75+
'name': 'file.txt',
76+
'data': 'test content',
77+
'type': 'text/plain'
78+
},
79+
{
80+
'name': 'testfile.txt',
81+
'data': 'hello there!\n',
82+
'type': 'text/plain'
83+
}
84+
]
85+
)
86+
expected.update(base_expected)
87+
88+
assert actual == expected
7689

7790
if at_least_version('1.8'):
7891
def test_reply_to():

test/django/testfile.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello there!

0 commit comments

Comments
 (0)