Skip to content

Commit

Permalink
updates to email templates for post_office compatibility (#752)
Browse files Browse the repository at this point in the history
[#188743887]
  • Loading branch information
uraniumanchor authored Jan 4, 2025
1 parent e1eb633 commit 2a93d7d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 59 deletions.
2 changes: 1 addition & 1 deletion tests/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ wheel
django-ical==1.9.2
django-paypal==1.1.2
django-mptt==0.16.0
django-post-office==3.6.0
django-post-office==3.9.1
django-timezone-field==7.0
djangorestframework==3.15.2
pre-commit==4.0.1
Expand Down
20 changes: 15 additions & 5 deletions tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,22 @@

AuthUser = get_user_model()

TEST_AUTH_MAIL_TEMPLATE = post_office.models.EmailTemplate(
content='user:{{user}}\nurl:{{reset_url}}\npassword_reset_url:{{password_reset_url}}'
)


@override_settings(EMAIL_FROM_USER='[email protected]')
class TestRegistrationFlow(TestCase):
def setUp(self):
self.factory = RequestFactory()
self.template = post_office.models.EmailTemplate.objects.create(
content='user:{{user}}\nurl:{{confirmation_url}}\npassword_reset_url:{{password_reset_url}}'
)

def test_registration_flow(self):
request = self.factory.post(reverse('tracker:register'))
new_user = AuthUser.objects.create(
username='dummyuser', email='[email protected]', is_active=False
)
sent_mail = tracker.auth.send_registration_mail(
request, new_user, template=TEST_AUTH_MAIL_TEMPLATE
request, new_user, template=self.template
)
contents = util.parse_test_mail(sent_mail)
self.assertEqual(new_user.username, contents['user'][0])
Expand Down Expand Up @@ -62,6 +61,17 @@ def test_registration_flow(self):
self.assertTrue(new_user.is_active)
self.assertTrue(new_user.check_password('foobar'))

def test_reset_url_deprecation(self):
with self.assertRaises(AssertionError):
request = self.factory.post(reverse('tracker:register'))
new_user = AuthUser.objects.create(
username='dummyuser', email='[email protected]', is_active=False
)
template = post_office.models.EmailTemplate.objects.create(
content='{{ reset_url }}'
)
tracker.auth.send_registration_mail(request, new_user, template=template)

def test_register_inactive_user(self):
AuthUser.objects.create(
username='existinguser', email='[email protected]', is_active=False
Expand Down
77 changes: 25 additions & 52 deletions tracker/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,65 +10,33 @@
from . import mailutil, viewutil


def default_password_reset_template_name():
return getattr(
settings,
'PASSWORD_RESET_EMAIL_TEMPLATE_NAME',
'default_password_reset_template',
)


# TODO: get better control over when the auth links expire, and explicitly state the expiration time
def default_password_reset_template():
return post_office.models.EmailTemplate(
name=default_password_reset_template_name(),
subject='Password Reset',
description="""Email template for user password reset.
The following context variables are defined:
user -- the User object
domain -- the web-domain of the website
reset_url -- the token-encoded url to redirect the user to
""",
html_content="""Hello {{ user }},
<p>
You (or something pretending to be you) has requested a password reset for your account on {{ domain }}. Please follow this <a href="{{ reset_url }}">link</a> to reset your password.
</p>
<p>
This login link will expire after you reset your password.
</p>
- The Staff
""",
)


def default_registration_template_name():
return getattr(
settings, 'REGISTER_EMAIL_TEMPLATE_NAME', 'default_registration_template'
)


def default_registration_template():
return post_office.models.EmailTemplate(
return post_office.models.EmailTemplate.objects.get_or_create(
name=default_registration_template_name(),
subject='Account Registration',
description="""Email template for user account registration.
defaults=dict(
subject='Account Registration',
description="""Email template for user account registration.
The following context variables are defined:
user -- the User object
domain -- the web-domain of the website
reset_url -- the token-encoded url to redirect the user to
confirmation_url -- the full URL (including token) the user should visit to confirm their registration and set their password
""",
html_content="""Hello {{ user }},
html_content="""Hello {{ user }},
<p>
You (or something pretending to be you) has requested an account on {{ domain }}. Please follow this <a href="{{ reset_url }}">link</a> to complete registering your account.
You (or something pretending to be you) has requested an account on {{ domain }}. Please follow this <a href="{{ confirmation_url }}">link</a> to complete registering your account.
</p>
- The GamesDoneQuick Staff
- The Staff
""".strip(),
)
),
)[0]


def default_volunteer_registration_template_name():
Expand All @@ -80,22 +48,22 @@ def default_volunteer_registration_template_name():


def default_volunteer_registration_template():
return post_office.models.EmailTemplate(
return post_office.models.EmailTemplate.objects.get_or_create(
name=default_volunteer_registration_template_name(),
subject='Donation Processing',
description="""Email template for donation volunteers.
defaults=dict(
subject='Donation Processing',
description="""Email template for donation volunteers.
The following context variables are defined:
user -- the User object
event -- the Event that this email is being send for
is_host -- True if the user was imported as a host
is_head -- True if the user was importes as a head donation screener
is_head -- True if the user was imported as a head donation screener
confirmation_url -- the full URL (including token) the user should visit to confirm their registration and set their password
reset_url -- deprecated name for confirmation_url
password_reset_url -- the full URL the user should visit if the above link expires (happens after 3 days if using default Django settings)
admin_url -- the full URL of the admin site (which will redirect them to login if need be)
""".strip(),
content="""
content="""
Hello {{ user }},
{% if is_head %}
Expand Down Expand Up @@ -124,7 +92,7 @@ def default_volunteer_registration_template():
- The Staff
""".strip(),
html_content="""
html_content="""
Hello {{ user }},
<p>
Expand All @@ -133,7 +101,7 @@ def default_volunteer_registration_template():
{% elif is_host %}
You are receiving this e-mail because you have been listed as a host during {{ event.name }}.
{% else %}
You are receiving this e-mail because you have been listed as a donation processer during {{ event.name }}.
You are receiving this e-mail because you have been listed as a donation processor during {{ event.name }}.
{% endif %}
</p>
Expand Down Expand Up @@ -161,7 +129,8 @@ def default_volunteer_registration_template():
- The Staff
""".strip(),
)
),
)[0]


def send_registration_mail(
Expand Down Expand Up @@ -189,6 +158,10 @@ def send_registration_mail(
password_reset_url = request.build_absolute_uri(
reverse('tracker:password_reset'),
)

def reset_url():
raise AssertionError('reset_url is deprecated, use confirmation_url instead')

return post_office.mail.send(
recipients=[user.email],
sender=sender,
Expand All @@ -197,7 +170,7 @@ def send_registration_mail(
**extra_context,
'user': user,
'confirmation_url': confirmation_url,
'reset_url': confirmation_url, # reset_url is deprecated
'reset_url': reset_url,
'password_reset_url': password_reset_url,
},
)
1 change: 0 additions & 1 deletion tracker/management/commands/default_email_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from tracker import auth, commandutil, prizemail

_default_templates = {
auth.default_password_reset_template_name(): auth.default_password_reset_template(),
auth.default_registration_template_name(): auth.default_registration_template(),
auth.default_volunteer_registration_template_name(): auth.default_volunteer_registration_template(),
prizemail.default_prize_winner_template_name(): prizemail.default_prize_winner_template(),
Expand Down

0 comments on commit 2a93d7d

Please sign in to comment.