Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ Changelog
------------------

- Move method of adding attachments out of the template
and require attachments to be passed as an argument to ``send_email()``. [Alex Bliskovsky]

and require attachments to be passed as an argument to ``send_email()``.
- Allow attachments to be specified as file-like objects.

0.9.3 (2015-08-27)
------------------
Expand Down
33 changes: 18 additions & 15 deletions ogmios/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,31 +112,34 @@ def validate_attachments(self):
# Allow specifying an attachment with just
# a path to the filename
continue
elif set(attachment.keys()) == set(['path', 'name']):
# Allow the user to specify the path and the filename.
# Let Django infer the mimetype.
continue
elif set(attachment.keys()) == set(['path', 'name', 'type']):
# Allow specifying all properties of an attachment.
continue
else:
raise OgmiosError("Invalid attachment specification. "
"Please see the documentation for details.")
key_set = set(attachment.keys())
if len(key_set & set(['path', 'data'])) != 1:
# Path or data are mutually exclusive.
raise OgmiosError("You must specify either a path to an attachment "
"or give a file-like object with the data.")
if not 'name' in key_set:
raise OgmiosError("You must specify the filename to send the attachment as.")


def handle_attachments(self, email):
for attachment in self.attachments:
if isinstance(attachment, six.string_types):
email.attach_file(attachment)
else:
# name and path are guaranteed to be in the dictionary
# name is required
name = attachment['name']
fpath = attachment['path']
# type may not be
# type may not be explicitly specified
mimetype = attachment.get('type')

full_path = os.path.join(fpath)
with open(full_path, 'r') as file_:
data = file_.read()
if 'data' in attachment:
data = attachment['data'].read()
else:
fpath = attachment['path']
full_path = os.path.join(fpath)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure to understand what you're trying to do here? os.path.join(x) is a no-op as far as I know (I could be wrong). Did you mean os.path.abspath(x)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's actually from you. I thought so as well but figured that you knew something I didn't. See 2ea20c8.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. That's a mistake from my side. Can you do another commit "fix antoine's shit" where you change this to os.path.abspath()? Thanks.

with open(full_path, 'r') as file_:
data = file_.read()

if mimetype:
email.attach(name, data, mimetype)
else:
Expand Down
17 changes: 17 additions & 0 deletions ogmios_tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from django.core import mail
from django.test import TestCase, override_settings
from six import StringIO

from ogmios import send_email, OgmiosError

Expand Down Expand Up @@ -91,6 +92,22 @@ def test_rename_attachment(self):
assert mail.outbox[0].attachments[0][1] == content
assert mail.outbox[0].attachments[0][0] == 'file.txt'

def test_filelike_object_attachment(self):
content = "This is some data"
content_file = StringIO(content)

send_email('attachment.md',
context={},
attachments=[{
'data': content_file,
'name': 'data.txt'
}])

assert len(mail.outbox) == 1
assert len(mail.outbox[0].attachments) == 1
assert mail.outbox[0].attachments[0][1] == content
assert mail.outbox[0].attachments[0][0] == 'data.txt'

def test_attachment_validation(self):
content = 'Some content'

Expand Down