From f0c76997b649aa8b01fd6e805f66df5bbaa71396 Mon Sep 17 00:00:00 2001 From: lennon Date: Tue, 1 Mar 2022 09:39:14 -0500 Subject: [PATCH 1/2] allow the 'from' e-mail to be overridden --- README.md | 10 +++++++--- magiclink/models.py | 3 ++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 464c47e..5d386f5 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Passwordless Authentication for Django with Magic Links. -This package was created with a focus on [ease of setup](#steps-to-impliment), [security](#security) and testing (coverage is currently at 100%). The idea is to use sane defaults to quickly create secure single-use token authentication for Django. +This package was created with a focus on [ease of setup](#configuration), [security](#security) and testing (coverage is currently at 100%). The idea is to use sane defaults to quickly create secure single-use token authentication for Django. ![](example.gif) @@ -185,7 +185,7 @@ When overriding this template please ensure the following content is included:

Already have an account? Log in here

``` -There are several forms made avalible in the context on this page depending on what information you want to collect: +There are several forms made available in the context on this page depending on what information you want to collect: * **SignupFormEmailOnly** - Only includes an `email` field * **SignupForm** - Includes `name` and `email` fields * **SignupFormWithUsername** - Includes `username` and `email` fields @@ -319,8 +319,12 @@ MAGICLINK_ANTISPAM_FIELD_TIME = 1 MAGICLINK_LOGIN_VERIFY_URL = 'magiclink:login_verify' # If an email address has been added to the unsubscribe table but is also -# assocaited with a Django user, should a login email be sent +# associated with a Django user, should a login email be sent MAGICLINK_IGNORE_UNSUBSCRIBE_IF_USER = False + +# By default, the sign in e-mail is sent from the DEFAULT_FROM_EMAIL. set +# this if you would like to override the from e-mail. +MAGICLINK_FROM_EMAIL = 'custom@email.com' ``` ## Magic Link cleanup diff --git a/magiclink/models.py b/magiclink/models.py index e007f64..945e549 100644 --- a/magiclink/models.py +++ b/magiclink/models.py @@ -89,7 +89,8 @@ def send(self, request: HttpRequest) -> None: subject=settings.EMAIL_SUBJECT, message=plain, recipient_list=[user.email], - from_email=djsettings.DEFAULT_FROM_EMAIL, + from_email=getattr(djsettings, 'MAGICLINK_FROM_EMAIL', + djsettings.DEFAULT_FROM_EMAIL), html_message=html, ) From bd961ada9cd6c6d90805aef58d5cae770cb36f94 Mon Sep 17 00:00:00 2001 From: lennon Date: Tue, 1 Mar 2022 09:55:11 -0500 Subject: [PATCH 2/2] changing how email settings are acquired --- magiclink/models.py | 3 +-- magiclink/settings.py | 4 +++- tests/settings.py | 2 ++ tests/test_settings.py | 13 +++++++++++++ 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/magiclink/models.py b/magiclink/models.py index 945e549..f69ccaf 100644 --- a/magiclink/models.py +++ b/magiclink/models.py @@ -89,8 +89,7 @@ def send(self, request: HttpRequest) -> None: subject=settings.EMAIL_SUBJECT, message=plain, recipient_list=[user.email], - from_email=getattr(djsettings, 'MAGICLINK_FROM_EMAIL', - djsettings.DEFAULT_FROM_EMAIL), + from_email=settings.FROM_EMAIL, html_message=html, ) diff --git a/magiclink/settings.py b/magiclink/settings.py index f2746db..906dfa2 100644 --- a/magiclink/settings.py +++ b/magiclink/settings.py @@ -119,4 +119,6 @@ IGNORE_UNSUBSCRIBE_IF_USER = getattr(settings, 'MAGICLINK_IGNORE_UNSUBSCRIBE_IF_USER', False) if not isinstance(IGNORE_UNSUBSCRIBE_IF_USER, bool): - raise ImproperlyConfigured('"MAGICLINK_IGNORE_UNSUBSCRIBE_IF_USER" must be a boolean') \ No newline at end of file + raise ImproperlyConfigured('"MAGICLINK_IGNORE_UNSUBSCRIBE_IF_USER" must be a boolean') + +FROM_EMAIL = getattr(settings, 'MAGICLINK_FROM_SETTINGS', settings.DEFAULT_FROM_EMAIL) diff --git a/tests/settings.py b/tests/settings.py index fe191a1..ae5dc0f 100644 --- a/tests/settings.py +++ b/tests/settings.py @@ -61,3 +61,5 @@ LOGOUT_REDIRECT_URL = 'no_login' MAGICLINK_LOGIN_SENT_REDIRECT = 'magiclink:login_sent' MAGICLINK_SIGNUP_LOGIN_REDIRECT = 'no_login' + +DEFAULT_FROM_EMAIL = 'default@email.com' diff --git a/tests/test_settings.py b/tests/test_settings.py index fabca84..afe14d9 100644 --- a/tests/test_settings.py +++ b/tests/test_settings.py @@ -371,3 +371,16 @@ def test_ignore_unsubscribe_if_user_bad_value(settings): with pytest.raises(ImproperlyConfigured): from magiclink import settings reload(settings) + + +def test_from_settings(settings): + settings.MAGICLINK_FROM_EMAIL = 'test@email.com' + from magiclink import settings as mlsettings + reload(mlsettings) + assert mlsettings.FROM_EMAIL == settings.MAGICLINK_FROM_EMAIL + + +def test_default_email_if_no_magiclink_email(settings): + from magiclink import settings as mlsettings + reload(mlsettings) + assert mlsettings.FROM_EMAIL == 'default@email.com'