Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Imports
3from django import forms
4from django.contrib.auth.forms import PasswordResetForm as BasePasswordResetForm
5from django.utils.translation import ugettext_lazy as _
6from django.contrib.auth import get_user_model
8UserModel = get_user_model()
10# Exports
12__all__ = (
13 "PasswordResetForm",
14)
16# Forms
19class PasswordResetForm(BasePasswordResetForm):
20 """User password reset form a user name field."""
22 email = forms.CharField(
23 label = _("Email or User Name"),
24 max_length=255, # length of standard USERNAME_FIELD
25 required=True
26 )
28 def get_users(self, email_or_username):
29 """Also check the email argument as a user name argument."""
30 # Determine if the given value is an email address.
31 criteria = {
32 'is_active': True,
33 '%s__exact' % UserModel.get_email_field_name(): email_or_username,
34 }
36 # noinspection PyProtectedMember
37 if UserModel._default_manager.filter(**criteria).exists():
38 return super().get_users(email_or_username)
40 # It's not an email address, so get the email address associated with the given user name.
41 criteria = {
42 'is_active': True,
43 "%s__exact" % UserModel.USERNAME_FIELD: email_or_username,
44 }
46 try:
47 # noinspection PyProtectedMember
48 user = UserModel._default_manager.get(**criteria)
49 return super().get_users(getattr(user, UserModel.get_email_field_name()))
50 except UserModel.DoesNotExist:
51 return list()