From 991d3d6c12296b381959ae86e74fd505604daeac Mon Sep 17 00:00:00 2001
From: Julien Phalip <jphalip@gmail.com>
Date: Mon, 2 Jan 2012 14:51:25 +0000
Subject: [PATCH] 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
---
 django/contrib/auth/forms.py | 88 ++++++++++++++++++++++++------------
 1 file changed, 58 insertions(+), 30 deletions(-)

diff --git a/django/contrib/auth/forms.py b/django/contrib/auth/forms.py
index 336e302162..badd7602f6 100644
--- a/django/contrib/auth/forms.py
+++ b/django/contrib/auth/forms.py
@@ -51,17 +51,24 @@ class ReadOnlyPasswordHashField(forms.Field):
 
 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 = {
         'duplicate_username': _("A user with that username already exists."),
         'password_mismatch': _("The two password fields didn't match."),
     }
-    username = forms.RegexField(label=_("Username"), max_length=30, regex=r'^[\w.@+-]+$',
-        help_text = _("Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only."),
-        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,
+    username = forms.RegexField(label=_("Username"), max_length=30,
+        regex=r'^[\w.@+-]+$',
+        help_text = _("Required. 30 characters or fewer. Letters, digits and "
+                      "@/./+/-/_ only."),
+        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."))
 
     class Meta:
@@ -82,7 +89,8 @@ class UserCreationForm(forms.ModelForm):
         password1 = self.cleaned_data.get("password1", "")
         password2 = self.cleaned_data["password2"]
         if password1 != password2:
-            raise forms.ValidationError(self.error_messages['password_mismatch'])
+            raise forms.ValidationError(
+                self.error_messages['password_mismatch'])
         return password2
 
     def save(self, commit=True):
@@ -94,10 +102,17 @@ class UserCreationForm(forms.ModelForm):
 
 
 class UserChangeForm(forms.ModelForm):
-    username = forms.RegexField(label=_("Username"), max_length=30, regex=r'^[\w.@+-]+$',
-        help_text = _("Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only."),
-        error_messages = {'invalid': _("This value may contain only letters, numbers and @/./+/-/_ characters.")})
-    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>."))
+    username = forms.RegexField(
+        label=_("Username"), max_length=30, regex=r"^[\w.@+-]+$",
+        help_text = _("Required. 30 characters or fewer. Letters, digits and "
+                      "@/./+/-/_ 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):
         return self.initial["password"]
@@ -144,9 +159,11 @@ class AuthenticationForm(forms.Form):
         password = self.cleaned_data.get('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:
-                raise forms.ValidationError(self.error_messages['invalid_login'])
+                raise forms.ValidationError(
+                    self.error_messages['invalid_login'])
             elif not self.user_cache.is_active:
                 raise forms.ValidationError(self.error_messages['inactive'])
         self.check_for_test_cookie()
@@ -179,12 +196,12 @@ class PasswordResetForm(forms.Form):
         Validates that an active user exists with the given email address.
         """
         email = self.cleaned_data["email"]
-        self.users_cache = User.objects.filter(
-                                email__iexact=email,
-                                is_active=True)
+        self.users_cache = User.objects.filter(email__iexact=email,
+                                               is_active=True)
         if not len(self.users_cache):
             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'])
         return email
 
@@ -194,7 +211,8 @@ class PasswordResetForm(forms.Form):
              use_https=False, token_generator=default_token_generator,
              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
         for user in self.users_cache:
@@ -222,14 +240,16 @@ class PasswordResetForm(forms.Form):
 
 class SetPasswordForm(forms.Form):
     """
-    A form that lets a user change set his/her password without
-    entering the old password
+    A form that lets a user change set his/her password without entering the
+    old password
     """
     error_messages = {
         'password_mismatch': _("The two password fields didn't match."),
     }
-    new_password1 = forms.CharField(label=_("New password"), widget=forms.PasswordInput)
-    new_password2 = forms.CharField(label=_("New password confirmation"), widget=forms.PasswordInput)
+    new_password1 = forms.CharField(label=_("New password"),
+                                    widget=forms.PasswordInput)
+    new_password2 = forms.CharField(label=_("New password confirmation"),
+                                    widget=forms.PasswordInput)
 
     def __init__(self, user, *args, **kwargs):
         self.user = user
@@ -240,7 +260,8 @@ class SetPasswordForm(forms.Form):
         password2 = self.cleaned_data.get('new_password2')
         if password1 and password2:
             if password1 != password2:
-                raise forms.ValidationError(self.error_messages['password_mismatch'])
+                raise forms.ValidationError(
+                    self.error_messages['password_mismatch'])
         return password2
 
     def save(self, commit=True):
@@ -256,9 +277,11 @@ class PasswordChangeForm(SetPasswordForm):
     their old password.
     """
     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):
         """
@@ -266,9 +289,11 @@ class PasswordChangeForm(SetPasswordForm):
         """
         old_password = self.cleaned_data["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
-PasswordChangeForm.base_fields.keyOrder = ['old_password', 'new_password1', 'new_password2']
+PasswordChangeForm.base_fields.keyOrder = ['old_password', 'new_password1',
+                                           'new_password2']
 
 
 class AdminPasswordChangeForm(forms.Form):
@@ -278,8 +303,10 @@ class AdminPasswordChangeForm(forms.Form):
     error_messages = {
         'password_mismatch': _("The two password fields didn't match."),
     }
-    password1 = forms.CharField(label=_("Password"), widget=forms.PasswordInput)
-    password2 = forms.CharField(label=_("Password (again)"), widget=forms.PasswordInput)
+    password1 = forms.CharField(label=_("Password"),
+                                widget=forms.PasswordInput)
+    password2 = forms.CharField(label=_("Password (again)"),
+                                widget=forms.PasswordInput)
 
     def __init__(self, user, *args, **kwargs):
         self.user = user
@@ -290,7 +317,8 @@ class AdminPasswordChangeForm(forms.Form):
         password2 = self.cleaned_data.get('password2')
         if password1 and password2:
             if password1 != password2:
-                raise forms.ValidationError(self.error_messages['password_mismatch'])
+                raise forms.ValidationError(
+                    self.error_messages['password_mismatch'])
         return password2
 
     def save(self, commit=True):