Skip to content

Commit

Permalink
Merge pull request #26 from deployed/python3_support
Browse files Browse the repository at this point in the history
Python3 support
  • Loading branch information
czubik8805 authored Aug 18, 2020
2 parents 0e95139 + ac7d7d0 commit dc24ac6
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 20 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
5 changes: 3 additions & 2 deletions emailtemplates/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# coding=utf-8
from django.contrib import admin
from django.urls import reverse
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _

from .forms import EmailTemplateAdminForm, MassEmailMessageForm, MassEmailAttachmentForm
Expand All @@ -23,9 +24,9 @@ class EmailTemplateAdmin(admin.ModelAdmin):
def show_links(self, obj):
if not obj.pk:
return ''
return u'<a href="%s" target="_blank">%s</a>' % (
return mark_safe(u'<a href="%s" target="_blank">%s</a>' % (
reverse('email_preview', kwargs={'pk': obj.pk}), _('Show email preview')
)
))

show_links.allow_tags = True
show_links.short_description = _('Actions')
Expand Down
14 changes: 10 additions & 4 deletions emailtemplates/email.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# coding=utf-8
import os
import logging
import six
from smtplib import SMTPException

from django.conf import settings
Expand All @@ -12,7 +13,6 @@
from .models import now, EmailTemplate
from .registry import email_templates


logger = logging.getLogger(__name__)


Expand All @@ -26,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 @@ -96,8 +97,8 @@ def get_object(self):
"Can't convert to unicode EmailTemplate object from database, using default file template.")
break
else:
self.template = unicode(tmp.content)
self.subject = unicode(tmp.subject) or self.subject
self.template = str(tmp.content)
self.subject = str(tmp.subject) or self.subject
self._template_source = 'database'
logger.debug(u"Got template %s from database", self.name)
return
Expand All @@ -118,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 All @@ -140,7 +146,7 @@ def send_email(self, send_to, attachment_paths=None, fail_silently=True, *args,

try:
self.sent = msg.send()
except SMTPException, e:
except SMTPException as e:
if not fail_silently:
raise
logger.error(u'Problem sending email to %s: %s', send_to, e)
Expand Down
5 changes: 4 additions & 1 deletion emailtemplates/helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# coding=utf-8
from importlib import import_module
from string import lower
try:
from string import lower
except ImportError:
lower = str.lower

from django.template.loaders import app_directories
from django.contrib.auth import get_user_model
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 dc24ac6

Please sign in to comment.