Skip to content
This repository has been archived by the owner on May 5, 2020. It is now read-only.

Commit

Permalink
feat: Set default html_template_name in EmailBackend
Browse files Browse the repository at this point in the history
  • Loading branch information
rubengrill authored and relekang committed Sep 14, 2018
1 parent efecdcf commit 0f46de1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
7 changes: 5 additions & 2 deletions nopassword/backends/email.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# -*- coding: utf-8 -*-
from django.core.mail import EmailMultiAlternatives
from django.template.exceptions import TemplateDoesNotExist
from django.template.loader import render_to_string

from nopassword.backends.base import NoPasswordBackend


class EmailBackend(NoPasswordBackend):
template_name = 'registration/login_code_request_email.txt'
html_template_name = None
html_template_name = 'registration/login_code_request_email.html'
subject_template_name = 'registration/login_code_request_subject.txt'
from_email = None

Expand All @@ -20,8 +21,10 @@ def send_login_code(self, code, context, **kwargs):

email_message = EmailMultiAlternatives(subject, body, self.from_email, [to_email])

if self.html_template_name is not None:
try:
html_email = render_to_string(self.html_template_name, context)
email_message.attach_alternative(html_email, 'text/html')
except TemplateDoesNotExist:
pass

email_message.send()
3 changes: 2 additions & 1 deletion tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ def test_email_backend(self):
self.assertEqual(0, len(message.alternatives))

def test_html_template_name(self):
self.backend.html_template_name = 'registration/login_code_request_email.html'
# We don't have an existing html template, so we just use the txt template
self.backend.html_template_name = 'registration/login_code_request_email.txt'
self.backend.send_login_code(self.code, {'url': 'https://example.com'})
self.assertEqual(1, len(mail.outbox))
message = mail.outbox[0]
Expand Down

0 comments on commit 0f46de1

Please sign in to comment.