summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYin Jifeng <jifeng.yin@gmail.com>2014-04-06 23:30:46 +0800
committerTim Graham <timograham@gmail.com>2014-07-10 09:36:43 -0400
commit849538d03df21b69f0754a38ee4ec5f48fa02c52 (patch)
treeee4f9403664685ea16f356ce0875850ee40cfe56
parentd88333661b687e178fcc6e47835a2d002ff5d17a (diff)
Fixed #13147 -- Moved User validation logic from form to model.
-rw-r--r--django/contrib/auth/forms.py28
-rw-r--r--django/contrib/auth/models.py10
-rw-r--r--django/contrib/auth/tests/test_forms.py14
-rw-r--r--docs/releases/1.8.txt4
4 files changed, 18 insertions, 38 deletions
diff --git a/django/contrib/auth/forms.py b/django/contrib/auth/forms.py
index 2ff75b268c..1bcc52d4e8 100644
--- a/django/contrib/auth/forms.py
+++ b/django/contrib/auth/forms.py
@@ -74,16 +74,8 @@ class UserCreationForm(forms.ModelForm):
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"),
@@ -94,19 +86,6 @@ class UserCreationForm(forms.ModelForm):
model = User
fields = ("username",)
- def clean_username(self):
- # Since User.username is unique, this check is redundant,
- # but it sets a nicer error message than the ORM. See #13147.
- username = self.cleaned_data["username"]
- try:
- User._default_manager.get(username=username)
- except User.DoesNotExist:
- return username
- raise forms.ValidationError(
- self.error_messages['duplicate_username'],
- code='duplicate_username',
- )
-
def clean_password2(self):
password1 = self.cleaned_data.get("password1")
password2 = self.cleaned_data.get("password2")
@@ -126,13 +105,6 @@ 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=_("Raw passwords are not stored, so there is no way to see "
"this user's password, but you can change the password "
diff --git a/django/contrib/auth/models.py b/django/contrib/auth/models.py
index 9c6f7247bf..94830ef00f 100644
--- a/django/contrib/auth/models.py
+++ b/django/contrib/auth/models.py
@@ -383,8 +383,14 @@ class AbstractUser(AbstractBaseUser, PermissionsMixin):
help_text=_('Required. 30 characters or fewer. Letters, digits and '
'@/./+/-/_ only.'),
validators=[
- validators.RegexValidator(r'^[\w.@+-]+$', _('Enter a valid username.'), 'invalid')
- ])
+ validators.RegexValidator(r'^[\w.@+-]+$',
+ _('Enter a valid username. '
+ 'This value may contain only letters, numbers '
+ 'and @/./+/-/_ characters.'), 'invalid'),
+ ],
+ error_messages={
+ 'unique': _("A user with that username already exists."),
+ })
first_name = models.CharField(_('first name'), max_length=30, blank=True)
last_name = models.CharField(_('last name'), max_length=30, blank=True)
email = models.EmailField(_('email address'), blank=True)
diff --git a/django/contrib/auth/tests/test_forms.py b/django/contrib/auth/tests/test_forms.py
index 1f7f3946d8..0ca3d9ccfa 100644
--- a/django/contrib/auth/tests/test_forms.py
+++ b/django/contrib/auth/tests/test_forms.py
@@ -4,7 +4,6 @@ import os
import re
from django import forms
-from django.contrib.auth import get_user_model
from django.contrib.auth.models import User
from django.contrib.auth.forms import (UserCreationForm, AuthenticationForm,
PasswordChangeForm, SetPasswordForm, UserChangeForm, PasswordResetForm,
@@ -36,7 +35,7 @@ class UserCreationFormTest(TestCase):
form = UserCreationForm(data)
self.assertFalse(form.is_valid())
self.assertEqual(form["username"].errors,
- [force_text(form.error_messages['duplicate_username'])])
+ [force_text(User._meta.get_field('username').error_messages['unique'])])
def test_invalid_data(self):
data = {
@@ -46,8 +45,8 @@ class UserCreationFormTest(TestCase):
}
form = UserCreationForm(data)
self.assertFalse(form.is_valid())
- self.assertEqual(form["username"].errors,
- [force_text(form.fields['username'].error_messages['invalid'])])
+ validator = next(v for v in User._meta.get_field('username').validators if v.code == 'invalid')
+ self.assertEqual(form["username"].errors, [force_text(validator.message)])
def test_password_verification(self):
# The verification password is incorrect.
@@ -190,8 +189,7 @@ class AuthenticationFormTest(TestCase):
username = CharField()
form = CustomAuthenticationForm()
- UserModel = get_user_model()
- username_field = UserModel._meta.get_field(UserModel.USERNAME_FIELD)
+ username_field = User._meta.get_field(User.USERNAME_FIELD)
self.assertEqual(form.fields['username'].label, capfirst(username_field.verbose_name))
def test_username_field_label_empty_string(self):
@@ -291,8 +289,8 @@ class UserChangeFormTest(TestCase):
data = {'username': 'not valid'}
form = UserChangeForm(data, instance=user)
self.assertFalse(form.is_valid())
- self.assertEqual(form['username'].errors,
- [force_text(form.fields['username'].error_messages['invalid'])])
+ validator = next(v for v in User._meta.get_field('username').validators if v.code == 'invalid')
+ self.assertEqual(form["username"].errors, [force_text(validator.message)])
def test_bug_14242(self):
# A regression test, introduce by adding an optimization for the
diff --git a/docs/releases/1.8.txt b/docs/releases/1.8.txt
index d2c2141415..259ee00e4b 100644
--- a/docs/releases/1.8.txt
+++ b/docs/releases/1.8.txt
@@ -414,6 +414,10 @@ Miscellaneous
parameters. Internally, Django will continue to provide the
``pk`` parameter in ``params`` for backwards compatibility.
+* ``UserCreationForm.errors_messages['duplicate_username']`` is no longer used.
+ If you wish to customize that error message, use
+ ``User.error_messages['unique']`` instead.
+
.. _deprecated-features-1.8:
Features deprecated in 1.8