Skip to content

Commit

Permalink
allows overriding email urls using settings
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
n-borges authored and tomwojcik committed Mar 31, 2024
1 parent 4452009 commit 610c78c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
3 changes: 3 additions & 0 deletions djoser/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
26 changes: 20 additions & 6 deletions djoser/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand Down
32 changes: 32 additions & 0 deletions testproject/testapp/tests/test_email.py
Original file line number Diff line number Diff line change
@@ -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")

0 comments on commit 610c78c

Please sign in to comment.