From 0d70bdc05dc15446b6e6480fb9bd9b79ddc48fce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2?= Date: Wed, 3 May 2023 17:01:16 +0200 Subject: [PATCH] allows overriding email urls using settings three new settings have been added to customize the context passed to the email templates, allowing to set urls domain, protocol and site name with ease. --- djoser/conf.py | 3 +++ djoser/email.py | 26 +++++++++++++++----- testproject/testapp/tests/test_email.py | 32 +++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 testproject/testapp/tests/test_email.py diff --git a/djoser/conf.py b/djoser/conf.py index dc85a039..c6251ea1 100644 --- a/djoser/conf.py +++ b/djoser/conf.py @@ -74,6 +74,9 @@ def __getattribute__(self, item): "username_reset": "djoser.email.UsernameResetEmail", } ), + "EMAIL_FRONTEND_DOMAIN": None, + "EMAIL_FRONTEND_PROTOCOL": None, + "EMAIL_FRONTEND_SITE_NAME": None, "CONSTANTS": ObjDict({"messages": "djoser.constants.Messages"}), "LOGOUT_ON_PASSWORD_CHANGE": False, "CREATE_SESSION_ON_LOGIN": False, diff --git a/djoser/email.py b/djoser/email.py index 8c01925e..b41783d5 100644 --- a/djoser/email.py +++ b/djoser/email.py @@ -5,7 +5,21 @@ from djoser.conf import settings -class ActivationEmail(BaseEmailMessage): +class BaseDjoserEmail(BaseEmailMessage): + def get_context_data(self): + context = super().get_context_data() + overridable = { + "protocol": settings.EMAIL_FRONTEND_PROTOCOL, + "domain": settings.EMAIL_FRONTEND_DOMAIN, + "site_name": settings.EMAIL_FRONTEND_SITE_NAME, + } + for context_key, context_value in overridable.items(): + if context_value: + context.update({context_key: context_value}) + return context + + +class ActivationEmail(BaseDjoserEmail): template_name = "email/activation.html" def get_context_data(self): @@ -19,11 +33,11 @@ def get_context_data(self): return context -class ConfirmationEmail(BaseEmailMessage): +class ConfirmationEmail(BaseDjoserEmail): template_name = "email/confirmation.html" -class PasswordResetEmail(BaseEmailMessage): +class PasswordResetEmail(BaseDjoserEmail): template_name = "email/password_reset.html" def get_context_data(self): @@ -37,15 +51,15 @@ def get_context_data(self): return context -class PasswordChangedConfirmationEmail(BaseEmailMessage): +class PasswordChangedConfirmationEmail(BaseDjoserEmail): template_name = "email/password_changed_confirmation.html" -class UsernameChangedConfirmationEmail(BaseEmailMessage): +class UsernameChangedConfirmationEmail(BaseDjoserEmail): template_name = "email/username_changed_confirmation.html" -class UsernameResetEmail(BaseEmailMessage): +class UsernameResetEmail(BaseDjoserEmail): template_name = "email/username_reset.html" def get_context_data(self): diff --git a/testproject/testapp/tests/test_email.py b/testproject/testapp/tests/test_email.py new file mode 100644 index 00000000..384a492b --- /dev/null +++ b/testproject/testapp/tests/test_email.py @@ -0,0 +1,32 @@ +from django.conf import settings +from django.test.testcases import SimpleTestCase +from django.test.utils import override_settings +from djoser.email import BaseDjoserEmail + + +class BaseDjoserEmailTestCase(SimpleTestCase): + @override_settings(DJOSER=dict()) + def test_base_djoser_email_get_context_data_with_no_settings_uses_defaults(self): + base_djoser_email = BaseDjoserEmail() + context_produced_without_settings = base_djoser_email.get_context_data() + default_context = super(BaseDjoserEmail, base_djoser_email).get_context_data() + self.assertEqual(context_produced_without_settings, default_context) + + @override_settings( + DJOSER=dict( + settings.DJOSER, + **{ + "EMAIL_FRONTEND_DOMAIN": "my_domain", + "EMAIL_FRONTEND_SITE_NAME": "my_site_name", + "EMAIL_FRONTEND_PROTOCOL": "https", + }, + ) + ) + def test_base_djoser_email_get_context_data_overrides_defaults_correctly(self): + base_djoser_email = BaseDjoserEmail() + context_produced_using_settings = base_djoser_email.get_context_data() + self.assertEqual(context_produced_using_settings.get("domain"), "my_domain") + self.assertEqual( + context_produced_using_settings.get("site_name"), "my_site_name" + ) + self.assertEqual(context_produced_using_settings.get("protocol"), "https")