Skip to content

Commit

Permalink
feat: ✨ adds setting for the password_changed email template (hackt…
Browse files Browse the repository at this point in the history
…oolkit#447)

- Modifies the signature of `reset_password` to take in email template
fields to handle the success email for resetting the user's password.
- Removes unused `random` import from `apps/accounts/emails.py`.
  • Loading branch information
shreyastelkar committed Aug 8, 2024
1 parent d9ee17f commit 0f0b952
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
12 changes: 8 additions & 4 deletions apps/accounts/emails.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Python Standard Library Imports
import datetime
import random

# Django Imports
from django.urls import reverse
Expand Down Expand Up @@ -110,7 +109,9 @@ def password_reset_email(user, token_generator, use_https=False, domain=None, te
'protocol': use_https and 'https' or 'http',
'domain': domain,
'site_name': htk_setting('HTK_SITE_NAME'),
'reset_path': reverse(htk_setting('HTK_ACCOUNTS_RESET_PASSWORD_URL_NAME')),
'reset_path': reverse(
htk_setting('HTK_ACCOUNT_RESET_PASSWORD_URL_NAME')
),
'uid': int_to_base36(user.id),
'token': token_generator.make_token(user),
}
Expand All @@ -128,7 +129,10 @@ def password_reset_email(user, token_generator, use_https=False, domain=None, te
)


def password_changed_email(user):
def password_changed_email(
user,
template=None,
):
context = {
'user': user,
'email': user.profile.confirmed_email or user.email,
Expand All @@ -137,7 +141,7 @@ def password_changed_email(user):
}
subject = htk_setting('HTK_ACCOUNT_EMAIL_SUBJECT_PASSWORD_CHANGED') % context
send_email(
template='accounts/password_changed',
template=template or 'accounts/password_changed',
subject=subject,
to=[user.profile.confirmed_email or user.email],
context=context
Expand Down
10 changes: 8 additions & 2 deletions apps/accounts/forms/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,23 @@ class UpdatePasswordForm(SetPasswordForm):
"""A subclass of Django's SetPasswordForm
Sends out a password_changed_email
"""

def __init__(self, *args, **kwargs):
super(UpdatePasswordForm, self).__init__(*args, **kwargs)
self.label_suffix = ''
set_input_placeholder_labels(self)
set_input_attrs(self)

def save(self, commit=True):
def save(
self,
email_template=None,
commit=True,
):
user = super(UpdatePasswordForm, self).save(commit=commit)
from htk.apps.accounts.emails import password_changed_email

try:
password_changed_email(user)
password_changed_email(user, template=email_template)
except Exception:
request = get_current_request()
rollbar.report_exc_info(request=request)
Expand Down
12 changes: 9 additions & 3 deletions apps/accounts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,8 @@ def reset_password(
data=None,
redirect_url_name='account_password_reset_success',
template='account/reset_password.html',
renderer=_r
email_template=None,
renderer=_r,
):
"""
View that checks the hash in a password reset link and presents a
Expand Down Expand Up @@ -508,9 +509,14 @@ def reset_password(
if request.method == 'POST':
form = UpdatePasswordForm(user, request.POST)
if form.is_valid():
user = form.save()
if htk_setting('HTK_ACCOUNTS_CHANGE_PASSWORD_UPDATE_SESSION_AUTH_HASH'):
user = form.save(
email_template=email_template,
)
if htk_setting(
'HTK_ACCOUNTS_CHANGE_PASSWORD_UPDATE_SESSION_AUTH_HASH'
):
from django.contrib.auth import update_session_auth_hash

update_session_auth_hash(request, user)
success = True
else:
Expand Down

0 comments on commit 0f0b952

Please sign in to comment.