-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Template MultiAttachments EmailView (#53)
- Loading branch information
Showing
3 changed files
with
179 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -152,7 +152,7 @@ Attachments | |
To add an attachment to your mail you just have to remember that `render_to_message` returns a `EmailMessage` instance, | ||
so you can use https://docs.djangoproject.com/en/dev/topics/email/ | ||
|
||
As usually we sent just an attachment, we have created a class thats tries to save your time allowing to sent an | ||
As usually we sent just an attachment, we have created a class that tries to save your time allowing to sent an | ||
attachent just passing the file name or a file object | ||
|
||
.. code:: python | ||
|
@@ -190,6 +190,53 @@ So if we want to send in our newsletter a pdf file we could do | |
As an attachment you must provide the full file path or the data stream. | ||
|
||
|
||
Multiple Attachments | ||
-------------------- | ||
|
||
To add multiple attachments to your mail you just have to remember that `render_to_message` returns a `EmailMessage` | ||
instance, so you can use https://docs.djangoproject.com/en/dev/topics/email/ | ||
|
||
To send multiple attachments, we have created a class that tries to save yor time allowing to set the attachments | ||
just passing a list of dicts with the filename and the file content | ||
|
||
.. code:: python | ||
TemplatedMultipleAttachmentsEmailMessageView | ||
So if we want to send in our newsletter a pdf file we could do | ||
|
||
|
||
.. code:: python | ||
class NewsletterView(TemplatedMultipleAttachmentsEmailMessageView): | ||
subject_template_name = 'emails/newsletter/subject.txt' | ||
body_template_name = 'emails/newsletter/body.txt' | ||
html_body_template_name = 'emails/newsletter/body_html.html' | ||
def render_to_message(self, extra_context=None, **kwargs): | ||
kwargs['to'] = ('[email protected]',) | ||
kwargs['from_email'] = '[email protected]' | ||
return super(NewsletterView, self).render_to_message(extra_context=None, **kwargs) | ||
def get_context_data(self, **kwargs): | ||
""" | ||
here we can get the addtional data we want | ||
""" | ||
context = super(NewsletterView, self).get_context_data(**kwargs) | ||
context['day'] = datetime.date.today() | ||
return context | ||
# Instantiate and send a message. | ||
attachments = [ | ||
{"attachment": os.path.join(OUR_ROOT_FILES_PATH, 'newsletter/attachment.pdf'), "filename": "attachment.pdf"}, | ||
{"attachment": os.path.join(OUR_ROOT_FILES_PATH, 'newsletter/attachment2.pdf'), "filename": "attachment2.pdf"}, | ||
{"attachment": os.path.join(OUR_ROOT_FILES_PATH, 'newsletter/attachment3.pdf'), "filename": "attachment3.pdf"}, | ||
] | ||
NewsletterView().send(attachments=attachments) | ||
As an attachment you must provide the full file path or the data stream. | ||
|
||
Sending mail to a user | ||
---------------------- | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,8 +15,7 @@ | |
from django_yubin.messages import ( | ||
TemplatedEmailMessageView, TemplatedHTMLEmailMessageView, | ||
TemplatedAttachmentEmailMessageView, template_from_string, | ||
) | ||
|
||
TemplatedMultipleAttachmentsEmailMessageView) | ||
|
||
using_test_templates = override_settings( | ||
TEMPLATE_DIRS=( | ||
|
@@ -177,7 +176,7 @@ def add_templates_to_message(self): | |
""" | ||
Adds templates to the fixture message, ensuring it can be rendered. | ||
""" | ||
super(TemplatedHTMLEmailMessageViewTestCase, self)\ | ||
super(TemplatedHTMLEmailMessageViewTestCase, self) \ | ||
.add_templates_to_message() | ||
self.message.html_body_template = self.html_body_template | ||
|
||
|
@@ -233,7 +232,7 @@ def add_templates_to_message(self): | |
""" | ||
Adds templates to the fixture message, ensuring it can be rendered. | ||
""" | ||
super(TemplatedAttachmentEmailMessageViewTestCase, self)\ | ||
super(TemplatedAttachmentEmailMessageViewTestCase, self) \ | ||
.add_templates_to_message() | ||
self.message.html_body_template = self.html_body_template | ||
|
||
|
@@ -261,6 +260,46 @@ def test_send_message(self): | |
self.assertOutboxLengthEquals(1) | ||
|
||
|
||
class TemplatedMultipleAttachmentsEmailMessageViewTestCase(TemplatedAttachmentEmailMessageViewTestCase): | ||
message_class = TemplatedMultipleAttachmentsEmailMessageView | ||
|
||
def test_send_message(self): | ||
"""Test we can send an attachment using the send command""" | ||
self.add_templates_to_message() | ||
attachment = os.path.join(os.path.dirname(__file__), 'files/attachment.pdf') | ||
attachments = [{ | ||
"filename": "attachment.pdf", | ||
"attachment": attachment | ||
}] | ||
self.message.send(self.context, | ||
attachments=attachments, | ||
to=('[email protected]',)) | ||
self.assertOutboxLengthEquals(1) | ||
|
||
def render_to_message(self, attach_number): | ||
self.add_templates_to_message() | ||
attachment = os.path.join(os.path.dirname(__file__), 'files/attachment.pdf'), | ||
attachments = [{ | ||
"filename": "{}-attachment.pdf".format(number), | ||
"attachment": attachment | ||
} for number in range(attach_number)] | ||
message = self.message.render_to_message(extra_context=self.context, | ||
attachments=attachments) | ||
self.assertEqual(len(message.attachments), attach_number) | ||
|
||
def test_render_to_message(self): | ||
"""Test we can send an attachment using the send command""" | ||
self.render_to_message(1) | ||
|
||
def test_render_to_message_no_attach(self): | ||
"""Test we can send no attchaments using the send command""" | ||
self.render_to_message(0) | ||
|
||
def test_render_to_message_multiple_attachs(self): | ||
"""Test we can send multiple attchaments using the send command""" | ||
self.render_to_message(10) | ||
|
||
|
||
class TestEmailOptions(EmailMessageViewTestCase): | ||
message_class = TemplatedEmailMessageView | ||
|
||
|