Skip to content

Commit

Permalink
add additional context to email plaintext
Browse files Browse the repository at this point in the history
  • Loading branch information
firstTimeCaller committed Feb 14, 2020
1 parent 88fa89b commit 8f5fd09
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
3 changes: 3 additions & 0 deletions drfpasswordless/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@
# A plaintext verification email message overridden by the html message. Takes one string.
'PASSWORDLESS_EMAIL_VERIFICATION_PLAINTEXT_MESSAGE': "Enter this verification code: %s",

# Insert multiple variables into PASSWORDLESS_EMAIL_VERIFICATION_PLAINTEXT_MESSAGE, eg ["kwargs['request'].get_host()", 'user_email', 'email_token.key']
'PASSWORDLESS_EMAIL_PLAINTEXT_MESSAGE_ORDERED_CONTEXT' : False,

# The verification email template name.
'PASSWORDLESS_EMAIL_VERIFICATION_TOKEN_HTML_TEMPLATE_NAME': "passwordless_default_verification_token_email.html",

Expand Down
12 changes: 10 additions & 2 deletions drfpasswordless/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,26 @@ def send_email_with_callback_token(user, email_token, **kwargs):
# Get email subject and message
email_subject = kwargs.get('email_subject',
api_settings.PASSWORDLESS_EMAIL_SUBJECT)
user_email = getattr(user, api_settings.PASSWORDLESS_USER_EMAIL_FIELD_NAME)

email_plaintext = kwargs.get('email_plaintext',
api_settings.PASSWORDLESS_EMAIL_PLAINTEXT_MESSAGE)
if api_settings.PASSWORDLESS_EMAIL_PLAINTEXT_MESSAGE_ORDERED_CONTEXT:
string_options = tuple()
for x in api_settings.PASSWORDLESS_EMAIL_PLAINTEXT_MESSAGE_ORDERED_CONTEXT:
string_options = string_options + (eval(x),)
else:
email_plaintext = email_plaintext % email_token.key

email_html = kwargs.get('email_html',
api_settings.PASSWORDLESS_EMAIL_TOKEN_HTML_TEMPLATE_NAME)

# Inject context if user specifies.
user_email = getattr(user, api_settings.PASSWORDLESS_USER_EMAIL_FIELD_NAME)
context = inject_template_context({'callback_token': email_token.key, 'user_email':user_email, 'request': kwargs['request']})
html_message = loader.render_to_string(email_html, context,)
send_mail(
email_subject,
email_plaintext % email_token.key,
email_plaintext,
api_settings.PASSWORDLESS_EMAIL_NOREPLY_ADDRESS,
[user_email],
fail_silently=False,
Expand Down
10 changes: 10 additions & 0 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.urls import reverse
from drfpasswordless.settings import api_settings, DEFAULTS
from drfpasswordless.utils import CallbackToken
from drfpasswordless.views import ObtainEmailCallbackToken

User = get_user_model()

Expand Down Expand Up @@ -75,6 +76,15 @@ def test_mobile_only_auth_enabled(self):
mobile_response = self.client.post(self.mobile_url, self.mobile_data)
self.assertEqual(mobile_response.status_code, status.HTTP_200_OK)

def test_email_plaintext_ordered_context(self):
api_settings.PASSWORDLESS_AUTH_TYPES = ['EMAIL']
api_settings.PASSWORDLESS_EMAIL_NOREPLY_ADDRESS = '[email protected]'
api_settings.PASSWORDLESS_EMAIL_PLAINTEXT_MESSAGE_ORDERED_CONTEXT = ["kwargs['request'].get_host()", 'user_email', 'email_token.key']
api_settings.PASSWORDLESS_EMAIL_PLAINTEXT_MESSAGE = 'click on link to activate https://%s/login/%s/%s'

email_response = self.client.post(self.email_url, self.email_data)
self.assertEqual(email_response.data['detail'], ObtainEmailCallbackToken.success_response)

def tearDown(self):
api_settings.PASSWORDLESS_AUTH_TYPES = DEFAULTS['PASSWORDLESS_AUTH_TYPES']
api_settings.PASSWORDLESS_EMAIL_NOREPLY_ADDRESS = DEFAULTS['PASSWORDLESS_EMAIL_NOREPLY_ADDRESS']
Expand Down

0 comments on commit 8f5fd09

Please sign in to comment.