Skip to content

Commit 6007dca

Browse files
committed
Added context processor support for templates
1 parent 5da0ee2 commit 6007dca

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

drfpasswordless/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# -*- coding: utf-8 -*-
22

33
__title__ = 'drfpasswordless'
4-
__version__ = '1.0.6'
4+
__version__ = '1.0.7'
55
__author__ = 'Aaron Ng'
66
__license__ = 'MIT'
77
__copyright__ = 'Copyright 2017 Aaron Ng'

drfpasswordless/settings.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,16 @@
4949
'PASSWORDLESS_REGISTER_NEW_USERS': True,
5050

5151
# Suppresses actual SMS for testing
52-
'PASSWORDLESS_TEST_SUPPRESSION': False
52+
'PASSWORDLESS_TEST_SUPPRESSION': False,
53+
54+
# Context Processors for Email Template
55+
'PASSWORDLESS_CONTEXT_PROCESSORS': [],
5356
}
5457

5558
# List of settings that may be in string import notation.
5659
IMPORT_STRINGS = (
5760
'PASSWORDLESS_EMAIL_TOKEN_HTML_TEMPLATE',
61+
'PASSWORDLESS_CONTEXT_PROCESSORS',
5862
)
5963

6064
api_settings = APISettings(USER_SETTINGS, DEFAULTS, IMPORT_STRINGS)

drfpasswordless/utils.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,15 @@ def verify_user_alias(user, token):
9999
return user
100100

101101

102+
def inject_template_context(context):
103+
"""
104+
Injects additional context into email template.
105+
"""
106+
for processor in api_settings.PASSWORDLESS_CONTEXT_PROCESSORS:
107+
context.update(processor())
108+
return context
109+
110+
102111
def send_email_with_callback_token(self, user, email_token):
103112
"""
104113
Sends a SMS to user.mobile.
@@ -110,9 +119,12 @@ def send_email_with_callback_token(self, user, email_token):
110119
if api_settings.PASSWORDLESS_EMAIL_NOREPLY_ADDRESS:
111120
# Make sure we have a sending address before sending.
112121

122+
# Inject context if user specifies.
123+
context = inject_template_context({'callback_token': email_token.key, })
124+
113125
html_message = loader.render_to_string(
114126
api_settings.PASSWORDLESS_EMAIL_TOKEN_HTML_TEMPLATE_NAME,
115-
{'callback_token': email_token.key, }
127+
context,
116128
)
117129
send_mail(
118130
api_settings.PASSWORDLESS_EMAIL_SUBJECT,

0 commit comments

Comments
 (0)