Skip to content

Commit

Permalink
Merge pull request #25 from deployed/add_default_reply_to_support
Browse files Browse the repository at this point in the history
Support for default reply_to email
  • Loading branch information
wuuuduu authored Aug 9, 2019
2 parents 4f122d6 + 5da7e79 commit ac7d7d0
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 14 deletions.
3 changes: 3 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ Assumptions

Changelog
=========
1.1.3
-----
* Adding support for DEFAULT_REPLY_TO_EMAIL in django settings.

1.1.2
-----
Expand Down
7 changes: 6 additions & 1 deletion emailtemplates/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from .models import now, EmailTemplate
from .registry import email_templates


logger = logging.getLogger(__name__)


Expand All @@ -27,6 +26,7 @@ class EmailFromTemplate(object):
Site Admins should know given template context.
Site Admins should be familiar with Django Template System.
"""

def __init__(self, name="", from_email=settings.DEFAULT_FROM_EMAIL,
language=settings.LANGUAGE_CODE, subject="", template_class=EmailTemplate,
registry_validation=True, template_object=None):
Expand Down Expand Up @@ -119,6 +119,11 @@ def render_message(self):
self.message = message

def get_message_object(self, send_to, attachment_paths, *args, **kwargs):
if kwargs.get('reply_to') is None:
defaut_reply_to_email = getattr(settings, 'DEFAULT_REPLY_TO_EMAIL', None)
if defaut_reply_to_email:
kwargs['reply_to'] = [defaut_reply_to_email]

msg = EmailMessage(self.subject, self.message, self.from_email, send_to, *args, **kwargs)
if attachment_paths:
for path in attachment_paths:
Expand Down
5 changes: 0 additions & 5 deletions emailtemplates/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
# encoding: utf-8
from test_helpers import *
from test_email import *
from test_template_registry import *
from test_models import *

23 changes: 19 additions & 4 deletions emailtemplates/tests/test_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from django.conf import settings
from django.core import mail
from django.test import TestCase
from django.test import TestCase, override_settings
from django.utils.html import escape
from mock import Mock

Expand All @@ -16,7 +16,10 @@


class CheckEmail(TestCase):
def check_email_was_sent(self, eft, to):
def check_email_was_sent(self, eft, to, reply_to=None):
if reply_to is None:
reply_to = []

self.assertTrue(len(mail.outbox) > 0)
msg = mail.outbox[0]
self.assertTrue(settings.DEFAULT_FROM_EMAIL in msg.from_email)
Expand All @@ -25,6 +28,7 @@ def check_email_was_sent(self, eft, to):
self.assertEqual(msg.body, eft.message)
self.assertTrue('Message-Id' in msg.message())
self.assertEqual(msg.to, to)
self.assertEqual(msg.reply_to, reply_to)


class EmailFromTemplateTest(CheckEmail):
Expand All @@ -34,7 +38,17 @@ def setUp(self):
email_logger.warning = Mock()
self.attachment_filepath = os.path.join(os.path.dirname(__file__), 'data', 'example_file.txt')

@override_settings(DEFAULT_REPLY_TO_EMAIL='[email protected]')
def test_empty_object(self):
eft = EmailFromTemplate(registry_validation=False)
self.assertTrue(isinstance(eft, object))
eft.render_message()
to = ['[email protected]']
eft.send_email(to)
self.check_email_was_sent(eft, to, reply_to=['[email protected]'])

@override_settings(DEFAULT_REPLY_TO_EMAIL=None)
def test_empty_object_with_empty_reply_to(self):
eft = EmailFromTemplate(registry_validation=False)
self.assertTrue(isinstance(eft, object))
eft.render_message()
Expand Down Expand Up @@ -84,9 +98,10 @@ def setUp(self):
email_logger.debug = Mock()

def test_support_database_template(self):
eft = EmailFromTemplate(name='support_respond.html', language=self.language, registry_validation=False)
template_name = 'support_respond.html'
eft = EmailFromTemplate(name=template_name, language=self.language, registry_validation=False)
eft.get_object()
email_logger.debug.assert_called_with(substr("Got template"))
email_logger.debug.assert_called_with('Got template %s from database', template_name)
self.assertEqual(eft.template_source, 'database')
eft.render_message()
to = ['[email protected]', '[email protected]']
Expand Down
6 changes: 3 additions & 3 deletions emailtemplates/tests/test_template_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def test_context_description(self):
def test_as_form_help_text(self):
item = RegistrationItem('hello_template.html', help_text=u'Hello template',
help_context={'username': u'Name of user in hello expression'})
self.assertEqual(unicode, type(item.as_form_help_text()))
self.assertEqual(str, type(item.as_form_help_text()))
self.assertIn("USAGE", item.as_form_help_text())
self.assertIn("CONTEXT", item.as_form_help_text())

Expand All @@ -52,7 +52,7 @@ def test_as_form_choice(self):

def test_safe_defaults(self):
item = RegistrationItem('hello_template.html')
self.assertEqual(unicode, type(item.help_text))
self.assertEqual(str, type(item.help_text))
self.assertEqual(dict, type(item.help_context))
self.assertEqual(tuple, type(item.as_form_choice()))

Expand Down Expand Up @@ -112,7 +112,7 @@ def test_registration_items(self):
template_registry = EmailTemplateRegistry()
template_registry.register('hello_template.html', help_text=u'Hello template',
help_context={'username': u'Name of user in hello expression'})
items = template_registry.registration_items()
items = list(template_registry.registration_items())
self.assertEqual(1, len(items))
self.assertEqual('hello_template.html', items[0].path)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

setup(
name='django-emailtemplates',
version='1.1.2',
version='1.1.3',
packages=find_packages(),
include_package_data=True,
license='MIT License',
Expand Down

0 comments on commit ac7d7d0

Please sign in to comment.