Skip to content

Commit

Permalink
Define two class attributes to customise notifications
Browse files Browse the repository at this point in the history
* `password_reset_notification`
* `validation_notification`
  • Loading branch information
Kevin Etienne committed Mar 13, 2015
1 parent 4a461ea commit e2387e6
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 4 deletions.
12 changes: 10 additions & 2 deletions user_management/models/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ def create_superuser(self, email, password, **extra_fields):


class EmailVerifyUserMethodsMixin:
"""Define how validation and password reset emails are sent.
`password_reset_notification` and `validation_notification` can be overriden to
provide custom settings to send emails.
"""
password_reset_notification = PasswordResetNotification
validation_notification = ValidationNotification

This comment has been minimized.

Copy link
@LilyFoote

LilyFoote Mar 13, 2015

Contributor

This is a nice solution. Since we always define a custom User, we don't need to add an AppConfig or any settings. 👍


def email_context(self, site):
return {
'protocol': 'https',
Expand All @@ -148,7 +156,7 @@ def send_validation_email(self):
site = Site.objects.get_current()
email_subject = _('{domain} account validate'.format(domain=site.domain))

notification = ValidationNotification(
notification = self.validation_notification(
user=self,
email_subject=email_subject,
context=self.email_context(site),
Expand All @@ -160,7 +168,7 @@ def send_password_reset(self):
site = Site.objects.get_current()
email_subject = _('{domain} password reset'.format(domain=site.domain))

notification = PasswordResetNotification(
notification = self.password_reset_notification(
user=self,
email_subject=email_subject,
context=self.email_context(site),
Expand Down
6 changes: 6 additions & 0 deletions user_management/models/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.db import models


from .notifications import CustomPasswordResetNotification
from ..mixins import (
AvatarMixin,
BasicUserFieldsMixin,
Expand All @@ -26,6 +27,11 @@ class VerifyEmailUser(VerifyEmailMixin, AbstractBaseUser):
pass


class CustomVerifyEmailUser(VerifyEmailMixin, AbstractBaseUser):
"""Customise the notification class to send a password reset."""
password_reset_notification = CustomPasswordResetNotification


class CustomBasicUserFieldsMixin(
NameUserMethodsMixin, EmailUserMixin, DateJoinedUserMixin,
IsStaffUserMixin):
Expand Down
7 changes: 7 additions & 0 deletions user_management/models/tests/notifications.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from user_management.utils.notifications import PasswordResetNotification


class CustomPasswordResetNotification(PasswordResetNotification):
"""Test setting a custom notification to alter how we send the password reset."""
text_email_template = 'my_cystom_email.txt'
html_email_template = None
25 changes: 25 additions & 0 deletions user_management/models/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,28 @@ def test_name(self):
def test_manager_check_invalid(self):
errors = self.model.check()
self.assertEqual(errors, [])


class TestCustomPasswordResetNotification(TestCase):
"""Assert we can customise the notification to send a password reset."""
model = models.CustomVerifyEmailUser

def test_send_password_reset_email(self):
"""Assert `text_email_template` and `html_template_name` can be customised."""
context = {}
site = Site.objects.get_current()
user = self.model(email='email@email.email')

with patch.object(user, 'email_context') as get_context:
get_context.return_value = context
with patch(SEND_METHOD) as send:
user.send_password_reset()

expected = {
'to': user.email,
'template_name': 'my_cystom_email.txt',
'html_template_name': None,
'subject': '{} password reset'.format(site.domain),
'context': context
}
send.assert_called_once_with(**expected)
4 changes: 2 additions & 2 deletions user_management/utils/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ class NotificationBase(Notification):


class PasswordResetNotification(NotificationBase):
"""`PasswordResetNotification` defining text and html email templates."""
"""`PasswordResetNotification` defines text and html email templates."""
text_email_template = 'user_management/password_reset_email.txt'
html_email_template = 'user_management/password_reset_email.html'


class ValidationNotification(NotificationBase):
"""`ValidationNotification` defining text and html email templates."""
"""`ValidationNotification` defines text and html email templates."""
text_email_template = 'user_management/account_validation_email.txt'
html_email_template = 'user_management/account_validation_email.html'

0 comments on commit e2387e6

Please sign in to comment.