Skip to content

Instantly share code, notes, and snippets.

@claudep
Created June 19, 2016 18:52
Show Gist options
  • Save claudep/78e3661224a9b1767b8b0ea3b8ac8e9b to your computer and use it in GitHub Desktop.
Save claudep/78e3661224a9b1767b8b0ea3b8ac8e9b to your computer and use it in GitHub Desktop.
diff --git a/django/contrib/auth/models.py b/django/contrib/auth/models.py
index 87c3adf..49bdb32 100644
--- a/django/contrib/auth/models.py
+++ b/django/contrib/auth/models.py
@@ -144,9 +144,8 @@ class UserManager(BaseUserManager):
"""
if not username:
raise ValueError('The given username must be set')
- email = self.normalize_email(email)
- username = self.normalize_username(username)
user = self.model(username=username, email=email, **extra_fields)
+ user.clean()
user.set_password(password)
user.save(using=self._db)
return user
@@ -344,6 +343,11 @@ class AbstractUser(AbstractBaseUser, PermissionsMixin):
verbose_name_plural = _('users')
abstract = True
+ def clean(self):
+ super(AbstractUser, self).clean()
+ self.email = self.__class__.objects.normalize_email(self.email)
+ self.username = self.__class__.objects.normalize_username(self.username)
+
def get_full_name(self):
"""
Returns the first_name plus the last_name, with a space in between.
diff --git a/tests/auth_tests/test_forms.py b/tests/auth_tests/test_forms.py
index d2ce828..6b3aac2 100644
--- a/tests/auth_tests/test_forms.py
+++ b/tests/auth_tests/test_forms.py
@@ -129,13 +129,17 @@ class UserCreationFormTest(TestDataMixin, TestCase):
omega_username = 'iamtheΩ' # U+03A9 GREEK CAPITAL LETTER OMEGA
ohm_username = 'iamtheΩ' # U+2126 OHM SIGN
self.assertNotEqual(omega_username, ohm_username)
- User.objects.create_user(username=omega_username, password='pwd')
data = {
- 'username': ohm_username,
+ 'username': omega_username,
'password1': 'pwd2',
'password2': 'pwd2',
}
form = UserCreationForm(data)
+ self.assertTrue(form.is_valid())
+ form.save()
+
+ data['username'] = ohm_username
+ form = UserCreationForm(data)
self.assertFalse(form.is_valid())
self.assertEqual(
form.errors['username'], ["A user with that username already exists."]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment