Source code for superdjango.contrib.accounts.passwords.forms
# Imports
from django import forms
from django.contrib.auth.forms import PasswordResetForm as BasePasswordResetForm
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth import get_user_model
UserModel = get_user_model()
# Exports
__all__ = (
"PasswordResetForm",
)
# Forms
[docs]class PasswordResetForm(BasePasswordResetForm):
"""User password reset form a user name field."""
email = forms.CharField(
label = _("Email or User Name"),
max_length=255, # length of standard USERNAME_FIELD
required=True
)
[docs] def get_users(self, email_or_username):
"""Also check the email argument as a user name argument."""
# Determine if the given value is an email address.
criteria = {
'is_active': True,
'%s__exact' % UserModel.get_email_field_name(): email_or_username,
}
# noinspection PyProtectedMember
if UserModel._default_manager.filter(**criteria).exists():
return super().get_users(email_or_username)
# It's not an email address, so get the email address associated with the given user name.
criteria = {
'is_active': True,
"%s__exact" % UserModel.USERNAME_FIELD: email_or_username,
}
try:
# noinspection PyProtectedMember
user = UserModel._default_manager.get(**criteria)
return super().get_users(getattr(user, UserModel.get_email_field_name()))
except UserModel.DoesNotExist:
return list()