Slightly improved the help text for the "Password" field in the `auth.User` admin form, and PEP8-cleaned up the area while I was there.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17326 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Julien Phalip 2012-01-02 14:51:25 +00:00
parent cb99b598c1
commit 991d3d6c12
1 changed files with 58 additions and 30 deletions

View File

@ -51,17 +51,24 @@ class ReadOnlyPasswordHashField(forms.Field):
class UserCreationForm(forms.ModelForm): class UserCreationForm(forms.ModelForm):
""" """
A form that creates a user, with no privileges, from the given username and password. A form that creates a user, with no privileges, from the given username and
password.
""" """
error_messages = { error_messages = {
'duplicate_username': _("A user with that username already exists."), 'duplicate_username': _("A user with that username already exists."),
'password_mismatch': _("The two password fields didn't match."), 'password_mismatch': _("The two password fields didn't match."),
} }
username = forms.RegexField(label=_("Username"), max_length=30, regex=r'^[\w.@+-]+$', username = forms.RegexField(label=_("Username"), max_length=30,
help_text = _("Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only."), regex=r'^[\w.@+-]+$',
error_messages = {'invalid': _("This value may contain only letters, numbers and @/./+/-/_ characters.")}) help_text = _("Required. 30 characters or fewer. Letters, digits and "
password1 = forms.CharField(label=_("Password"), widget=forms.PasswordInput) "@/./+/-/_ only."),
password2 = forms.CharField(label=_("Password confirmation"), widget=forms.PasswordInput, error_messages = {
'invalid': _("This value may contain only letters, numbers and "
"@/./+/-/_ characters.")})
password1 = forms.CharField(label=_("Password"),
widget=forms.PasswordInput)
password2 = forms.CharField(label=_("Password confirmation"),
widget=forms.PasswordInput,
help_text = _("Enter the same password as above, for verification.")) help_text = _("Enter the same password as above, for verification."))
class Meta: class Meta:
@ -82,7 +89,8 @@ class UserCreationForm(forms.ModelForm):
password1 = self.cleaned_data.get("password1", "") password1 = self.cleaned_data.get("password1", "")
password2 = self.cleaned_data["password2"] password2 = self.cleaned_data["password2"]
if password1 != password2: if password1 != password2:
raise forms.ValidationError(self.error_messages['password_mismatch']) raise forms.ValidationError(
self.error_messages['password_mismatch'])
return password2 return password2
def save(self, commit=True): def save(self, commit=True):
@ -94,10 +102,17 @@ class UserCreationForm(forms.ModelForm):
class UserChangeForm(forms.ModelForm): class UserChangeForm(forms.ModelForm):
username = forms.RegexField(label=_("Username"), max_length=30, regex=r'^[\w.@+-]+$', username = forms.RegexField(
help_text = _("Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only."), label=_("Username"), max_length=30, regex=r"^[\w.@+-]+$",
error_messages = {'invalid': _("This value may contain only letters, numbers and @/./+/-/_ characters.")}) help_text = _("Required. 30 characters or fewer. Letters, digits and "
password = ReadOnlyPasswordHashField(label=_("Password"), help_text=_("We don't store raw passwords, so there's no way to see this user's password, but you can change the password using <a href=\"password/\">this form</a>.")) "@/./+/-/_ only."),
error_messages = {
'invalid': _("This value may contain only letters, numbers and "
"@/./+/-/_ characters.")})
password = ReadOnlyPasswordHashField(label=_("Password"),
help_text=_("Raw passwords are not stored, so there is no way to see "
"this user's password, but you can change the password "
"using <a href=\"password/\">this form</a>."))
def clean_password(self): def clean_password(self):
return self.initial["password"] return self.initial["password"]
@ -144,9 +159,11 @@ class AuthenticationForm(forms.Form):
password = self.cleaned_data.get('password') password = self.cleaned_data.get('password')
if username and password: if username and password:
self.user_cache = authenticate(username=username, password=password) self.user_cache = authenticate(username=username,
password=password)
if self.user_cache is None: if self.user_cache is None:
raise forms.ValidationError(self.error_messages['invalid_login']) raise forms.ValidationError(
self.error_messages['invalid_login'])
elif not self.user_cache.is_active: elif not self.user_cache.is_active:
raise forms.ValidationError(self.error_messages['inactive']) raise forms.ValidationError(self.error_messages['inactive'])
self.check_for_test_cookie() self.check_for_test_cookie()
@ -179,12 +196,12 @@ class PasswordResetForm(forms.Form):
Validates that an active user exists with the given email address. Validates that an active user exists with the given email address.
""" """
email = self.cleaned_data["email"] email = self.cleaned_data["email"]
self.users_cache = User.objects.filter( self.users_cache = User.objects.filter(email__iexact=email,
email__iexact=email, is_active=True)
is_active=True)
if not len(self.users_cache): if not len(self.users_cache):
raise forms.ValidationError(self.error_messages['unknown']) raise forms.ValidationError(self.error_messages['unknown'])
if any((user.password == UNUSABLE_PASSWORD) for user in self.users_cache): if any((user.password == UNUSABLE_PASSWORD)
for user in self.users_cache):
raise forms.ValidationError(self.error_messages['unusable']) raise forms.ValidationError(self.error_messages['unusable'])
return email return email
@ -194,7 +211,8 @@ class PasswordResetForm(forms.Form):
use_https=False, token_generator=default_token_generator, use_https=False, token_generator=default_token_generator,
from_email=None, request=None): from_email=None, request=None):
""" """
Generates a one-use only link for resetting password and sends to the user Generates a one-use only link for resetting password and sends to the
user.
""" """
from django.core.mail import send_mail from django.core.mail import send_mail
for user in self.users_cache: for user in self.users_cache:
@ -222,14 +240,16 @@ class PasswordResetForm(forms.Form):
class SetPasswordForm(forms.Form): class SetPasswordForm(forms.Form):
""" """
A form that lets a user change set his/her password without A form that lets a user change set his/her password without entering the
entering the old password old password
""" """
error_messages = { error_messages = {
'password_mismatch': _("The two password fields didn't match."), 'password_mismatch': _("The two password fields didn't match."),
} }
new_password1 = forms.CharField(label=_("New password"), widget=forms.PasswordInput) new_password1 = forms.CharField(label=_("New password"),
new_password2 = forms.CharField(label=_("New password confirmation"), widget=forms.PasswordInput) widget=forms.PasswordInput)
new_password2 = forms.CharField(label=_("New password confirmation"),
widget=forms.PasswordInput)
def __init__(self, user, *args, **kwargs): def __init__(self, user, *args, **kwargs):
self.user = user self.user = user
@ -240,7 +260,8 @@ class SetPasswordForm(forms.Form):
password2 = self.cleaned_data.get('new_password2') password2 = self.cleaned_data.get('new_password2')
if password1 and password2: if password1 and password2:
if password1 != password2: if password1 != password2:
raise forms.ValidationError(self.error_messages['password_mismatch']) raise forms.ValidationError(
self.error_messages['password_mismatch'])
return password2 return password2
def save(self, commit=True): def save(self, commit=True):
@ -256,9 +277,11 @@ class PasswordChangeForm(SetPasswordForm):
their old password. their old password.
""" """
error_messages = dict(SetPasswordForm.error_messages, **{ error_messages = dict(SetPasswordForm.error_messages, **{
'password_incorrect': _("Your old password was entered incorrectly. Please enter it again."), 'password_incorrect': _("Your old password was entered incorrectly. "
"Please enter it again."),
}) })
old_password = forms.CharField(label=_("Old password"), widget=forms.PasswordInput) old_password = forms.CharField(label=_("Old password"),
widget=forms.PasswordInput)
def clean_old_password(self): def clean_old_password(self):
""" """
@ -266,9 +289,11 @@ class PasswordChangeForm(SetPasswordForm):
""" """
old_password = self.cleaned_data["old_password"] old_password = self.cleaned_data["old_password"]
if not self.user.check_password(old_password): if not self.user.check_password(old_password):
raise forms.ValidationError(self.error_messages['password_incorrect']) raise forms.ValidationError(
self.error_messages['password_incorrect'])
return old_password return old_password
PasswordChangeForm.base_fields.keyOrder = ['old_password', 'new_password1', 'new_password2'] PasswordChangeForm.base_fields.keyOrder = ['old_password', 'new_password1',
'new_password2']
class AdminPasswordChangeForm(forms.Form): class AdminPasswordChangeForm(forms.Form):
@ -278,8 +303,10 @@ class AdminPasswordChangeForm(forms.Form):
error_messages = { error_messages = {
'password_mismatch': _("The two password fields didn't match."), 'password_mismatch': _("The two password fields didn't match."),
} }
password1 = forms.CharField(label=_("Password"), widget=forms.PasswordInput) password1 = forms.CharField(label=_("Password"),
password2 = forms.CharField(label=_("Password (again)"), widget=forms.PasswordInput) widget=forms.PasswordInput)
password2 = forms.CharField(label=_("Password (again)"),
widget=forms.PasswordInput)
def __init__(self, user, *args, **kwargs): def __init__(self, user, *args, **kwargs):
self.user = user self.user = user
@ -290,7 +317,8 @@ class AdminPasswordChangeForm(forms.Form):
password2 = self.cleaned_data.get('password2') password2 = self.cleaned_data.get('password2')
if password1 and password2: if password1 and password2:
if password1 != password2: if password1 != password2:
raise forms.ValidationError(self.error_messages['password_mismatch']) raise forms.ValidationError(
self.error_messages['password_mismatch'])
return password2 return password2
def save(self, commit=True): def save(self, commit=True):