mirror of
https://github.com/django/django.git
synced 2025-02-21 14:54:35 +00:00
[4.2.x] Fixed #34438 -- Reallowed extending UserCreationForm.
Regression in 298d02a77a69321af8c0023df3250663e9d1362d. Backport of fcc7dc5781667932bf0bf8bec76df458836e5e95 from main
This commit is contained in:
parent
788f7b8100
commit
99ba5b43f0
@ -147,16 +147,22 @@ class BaseUserCreationForm(forms.ModelForm):
|
|||||||
|
|
||||||
|
|
||||||
class UserCreationForm(BaseUserCreationForm):
|
class UserCreationForm(BaseUserCreationForm):
|
||||||
error_messages = {
|
|
||||||
**BaseUserCreationForm.error_messages,
|
|
||||||
"unique": _("A user with that username already exists."),
|
|
||||||
}
|
|
||||||
|
|
||||||
def clean_username(self):
|
def clean_username(self):
|
||||||
"""Reject usernames that differ only in case."""
|
"""Reject usernames that differ only in case."""
|
||||||
username = self.cleaned_data.get("username")
|
username = self.cleaned_data.get("username")
|
||||||
if username and User.objects.filter(username__iexact=username).exists():
|
if (
|
||||||
raise forms.ValidationError(self.error_messages["unique"], code="unique")
|
username
|
||||||
|
and self._meta.model.objects.filter(username__iexact=username).exists()
|
||||||
|
):
|
||||||
|
self._update_errors(
|
||||||
|
ValidationError(
|
||||||
|
{
|
||||||
|
"username": self.instance.unique_error_message(
|
||||||
|
self._meta.model, ["username"]
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
return username
|
return username
|
||||||
|
|
||||||
|
@ -77,10 +77,6 @@ msgstr ""
|
|||||||
msgid "Enter the same password as before, for verification."
|
msgid "Enter the same password as before, for verification."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: contrib/auth/forms.py:152 contrib/auth/models.py:353
|
|
||||||
msgid "A user with that username already exists."
|
|
||||||
msgstr ""
|
|
||||||
|
|
||||||
#: contrib/auth/forms.py:168
|
#: contrib/auth/forms.py:168
|
||||||
msgid ""
|
msgid ""
|
||||||
"Raw passwords are not stored, so there is no way to see this user’s "
|
"Raw passwords are not stored, so there is no way to see this user’s "
|
||||||
@ -240,6 +236,10 @@ msgstr ""
|
|||||||
msgid "Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only."
|
msgid "Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
|
#: contrib/auth/models.py:353
|
||||||
|
msgid "A user with that username already exists."
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
#: contrib/auth/models.py:356
|
#: contrib/auth/models.py:356
|
||||||
msgid "first name"
|
msgid "first name"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
@ -372,6 +372,35 @@ class UserCreationFormTest(TestDataMixin, TestCase):
|
|||||||
["A user with that username already exists."],
|
["A user with that username already exists."],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@override_settings(AUTH_USER_MODEL="auth_tests.ExtensionUser")
|
||||||
|
def test_case_insensitive_username_custom_user_and_error_message(self):
|
||||||
|
class CustomUserCreationForm(UserCreationForm):
|
||||||
|
class Meta(UserCreationForm.Meta):
|
||||||
|
model = ExtensionUser
|
||||||
|
fields = UserCreationForm.Meta.fields + ("date_of_birth",)
|
||||||
|
error_messages = {
|
||||||
|
"username": {"unique": "This username has already been taken."}
|
||||||
|
}
|
||||||
|
|
||||||
|
ExtensionUser.objects.create_user(
|
||||||
|
username="testclient",
|
||||||
|
password="password",
|
||||||
|
email="testclient@example.com",
|
||||||
|
date_of_birth=datetime.date(1984, 3, 5),
|
||||||
|
)
|
||||||
|
data = {
|
||||||
|
"username": "TeStClIeNt",
|
||||||
|
"password1": "test123",
|
||||||
|
"password2": "test123",
|
||||||
|
"date_of_birth": "1980-01-01",
|
||||||
|
}
|
||||||
|
form = CustomUserCreationForm(data)
|
||||||
|
self.assertIs(form.is_valid(), False)
|
||||||
|
self.assertEqual(
|
||||||
|
form["username"].errors,
|
||||||
|
["This username has already been taken."],
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
# To verify that the login form rejects inactive users, use an authentication
|
# To verify that the login form rejects inactive users, use an authentication
|
||||||
# backend that allows them.
|
# backend that allows them.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user