Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allows overriding email urls using settings #729

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
Loading