Ticket #7507: 7507.diff
File 7507.diff, 5.9 KB (added by , 16 years ago) |
---|
-
django/contrib/auth/forms.py
14 14 error_message = _("This value must contain only letters, numbers and underscores.")) 15 15 password1 = forms.CharField(label=_("Password"), max_length=60, widget=forms.PasswordInput) 16 16 password2 = forms.CharField(label=_("Password confirmation"), max_length=60, widget=forms.PasswordInput) 17 17 18 18 class Meta: 19 19 model = User 20 20 fields = ("username",) 21 21 22 22 def clean_username(self): 23 23 username = self.cleaned_data["username"] 24 24 try: … … 26 26 except User.DoesNotExist: 27 27 return username 28 28 raise forms.ValidationError(_("A user with that username already exists.")) 29 29 30 30 def clean_password2(self): 31 31 password1 = self.cleaned_data["password1"] 32 32 password2 = self.cleaned_data["password2"] 33 33 if password1 != password2: 34 34 raise forms.ValidationError(_("The two password fields didn't match.")) 35 35 return password2 36 36 37 37 def save(self, commit=True): 38 38 user = super(UserCreationForm, self).save(commit=False) 39 39 user.set_password(self.cleaned_data["password1"]) … … 46 46 Base class for authenticating users. Extend this to get a form that accepts 47 47 username/password logins. 48 48 """ 49 username = forms.CharField(label=_("Username") , max_length=30)50 password = forms.CharField(label=_("Password"), max_length=30,widget=forms.PasswordInput)51 49 username = forms.CharField(label=_("Username")) 50 password = forms.CharField(label=_("Password"), widget=forms.PasswordInput) 51 52 52 def __init__(self, request=None, *args, **kwargs): 53 53 """ 54 54 If request is passed in, the form will validate that cookies are … … 59 59 self.request = request 60 60 self.user_cache = None 61 61 super(AuthenticationForm, self).__init__(*args, **kwargs) 62 62 63 63 def clean(self): 64 64 username = self.cleaned_data.get('username') 65 65 password = self.cleaned_data.get('password') 66 66 67 67 if username and password: 68 68 self.user_cache = authenticate(username=username, password=password) 69 69 if self.user_cache is None: 70 70 raise forms.ValidationError(_("Please enter a correct username and password. Note that both fields are case-sensitive.")) 71 71 elif not self.user_cache.is_active: 72 72 raise forms.ValidationError(_("This account is inactive.")) 73 73 74 74 # TODO: determine whether this should move to its own method. 75 75 if self.request: 76 76 if not self.request.session.test_cookie_worked(): 77 77 raise forms.ValidationError(_("Your Web browser doesn't appear to have cookies enabled. Cookies are required for logging in.")) 78 78 79 79 return self.cleaned_data 80 80 81 81 def get_user_id(self): 82 82 if self.user_cache: 83 return self.user_cache. id83 return self.user_cache.pk 84 84 return None 85 85 86 86 def get_user(self): 87 87 return self.user_cache 88 88 89 89 class PasswordResetForm(forms.Form): 90 90 email = forms.EmailField(label=_("E-mail"), max_length=40) 91 91 92 92 def clean_email(self): 93 93 """ 94 94 Validates that a user exists with the given e-mail address. … … 97 97 self.users_cache = User.objects.filter(email__iexact=email) 98 98 if len(self.users_cache) == 0: 99 99 raise forms.ValidationError(_("That e-mail address doesn't have an associated user account. Are you sure you've registered?")) 100 100 101 101 def save(self, domain_override=None, email_template_name='registration/password_reset_email.html'): 102 102 """ 103 103 Calculates a new password randomly and sends it to the user. … … 131 131 old_password = forms.CharField(label=_("Old password"), max_length=30, widget=forms.PasswordInput) 132 132 new_password1 = forms.CharField(label=_("New password"), max_length=30, widget=forms.PasswordInput) 133 133 new_password2 = forms.CharField(label=_("New password confirmation"), max_length=30, widget=forms.PasswordInput) 134 134 135 135 def __init__(self, user, *args, **kwargs): 136 136 self.user = user 137 137 super(PasswordChangeForm, self).__init__(*args, **kwargs) 138 138 139 139 def clean_old_password(self): 140 140 """ 141 141 Validates that the old_password field is correct. … … 144 144 if not self.user.check_password(old_password): 145 145 raise forms.ValidationError(_("Your old password was entered incorrectly. Please enter it again.")) 146 146 return old_password 147 147 148 148 def clean_new_password2(self): 149 149 password1 = self.cleaned_data.get('new_password1') 150 150 password2 = self.cleaned_data.get('new_password2') … … 152 152 if password1 != password2: 153 153 raise forms.ValidationError(_("The two password fields didn't match.")) 154 154 return password2 155 155 156 156 def save(self, commit=True): 157 157 self.user.set_password(self.cleaned_data['new_password1']) 158 158 if commit: … … 165 165 """ 166 166 password1 = forms.CharField(label=_("Password"), max_length=60, widget=forms.PasswordInput) 167 167 password2 = forms.CharField(label=_("Password (again)"), max_length=60, widget=forms.PasswordInput) 168 168 169 169 def __init__(self, user, *args, **kwargs): 170 170 self.user = user 171 171 super(AdminPasswordChangeForm, self).__init__(*args, **kwargs) 172 172 173 173 def clean_password2(self): 174 174 password1 = self.cleaned_data.get('password1') 175 175 password2 = self.cleaned_data.get('password2') … … 177 177 if password1 != password2: 178 178 raise forms.ValidationError(_("The two password fields didn't match.")) 179 179 return password2 180 180 181 181 def save(self, commit=True): 182 182 """ 183 183 Saves the new password.