Skip to content

Instantly share code, notes, and snippets.

@develmaycare
Created April 11, 2017 17:51
Show Gist options
  • Save develmaycare/95273a5ef534365e0526c6ab4f98e8ef to your computer and use it in GitHub Desktop.
Save develmaycare/95273a5ef534365e0526c6ab4f98e8ef to your computer and use it in GitHub Desktop.
Override Django's PasswordResetForm to process only one user email at a time and return the token and uid to the view.
from django.contrib.auth.forms import PasswordResetForm as BasePasswordResetForm
# noinspection PyClassHasNoInit
class PasswordResetForm(BasePasswordResetForm):
"""A self-reset form for users."""
def save(self, domain_override=None, subject_template_name='accounts/password_reset_subject.txt',
email_template_name='accounts/password_reset_email.txt', use_https=False,
token_generator=default_token_generator, from_email=None, request=None, html_email_template_name=None,
extra_email_context=None):
"""Override the default in order to return the UID and token for use in the view. This also differs from the
default behavior by working with one user at a time.
:rtype: list[str]
"""
# Get the email. That's why we're here.
email = self.cleaned_data["email"]
# Find the user.
try:
user = user_model.objects.get(email=email)
except user_model.DoesNotExist:
return None, None
# Generate token and UID.
token = token_generator.make_token()
uid = urlsafe_base64_encode(force_bytes(user.pk))
# Get site info.
if domain_override:
site_title = domain = domain_override
else:
current_site = request.site
site_title = current_site.name
domain = current_site.domain
# Create the template context.
context = {
'domain': domain,
'email': email,
'protocol': 'https' if use_https else 'http',
'site_title': site_title,
'token': token,
'uid': uid,
'user': user,
}
if extra_email_context is not None:
context.update(extra_email_context)
# Send the email.
# noinspection PyUnusedLocal
try:
self.send_mail(
subject_template_name,
email_template_name,
context,
from_email,
email
)
except (SMTPAuthenticationError, SocketError) as e:
# TODO: Need to deal with email errors.
pass
# Return the token and uid for use in the view.
return token, uid
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment