mirror of
https://github.com/django/django.git
synced 2025-05-29 18:26:29 +00:00
Fixed #36314 -- Fixed MinimumLengthValidator error message translation.
Regression in ec7d69035a408b357f1803ca05a7c991cc358cfa. Thank you Gabriel Trouvé for the report and Claude Paroz for the review.
This commit is contained in:
parent
4a293eff6f
commit
d469db978e
@ -106,15 +106,20 @@ class MinimumLengthValidator:
|
|||||||
|
|
||||||
def validate(self, password, user=None):
|
def validate(self, password, user=None):
|
||||||
if len(password) < self.min_length:
|
if len(password) < self.min_length:
|
||||||
raise ValidationError(self.get_error_message(), code="password_too_short")
|
raise ValidationError(
|
||||||
|
self.get_error_message(),
|
||||||
|
code="password_too_short",
|
||||||
|
params={"min_length": self.min_length},
|
||||||
|
)
|
||||||
|
|
||||||
def get_error_message(self):
|
def get_error_message(self):
|
||||||
return ngettext(
|
return (
|
||||||
"This password is too short. It must contain at least %d character."
|
ngettext(
|
||||||
% self.min_length,
|
"This password is too short. It must contain at least %d character.",
|
||||||
"This password is too short. It must contain at least %d characters."
|
"This password is too short. It must contain at least %d characters.",
|
||||||
% self.min_length,
|
self.min_length,
|
||||||
self.min_length,
|
)
|
||||||
|
% self.min_length
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_help_text(self):
|
def get_help_text(self):
|
||||||
|
@ -36,3 +36,7 @@ Bugfixes
|
|||||||
* Fixed a regression in Django 5.2 that caused improper values to be returned
|
* Fixed a regression in Django 5.2 that caused improper values to be returned
|
||||||
from ``QuerySet.values_list()`` when duplicate field names were specified
|
from ``QuerySet.values_list()`` when duplicate field names were specified
|
||||||
(:ticket:`36288`).
|
(:ticket:`36288`).
|
||||||
|
|
||||||
|
* Fixed a regression in Django 5.2 where the password validation error message
|
||||||
|
from ``MinimumLengthValidator`` was not translated when using non-English
|
||||||
|
locales (:ticket:`36314`).
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import os
|
import os
|
||||||
|
from unittest import mock
|
||||||
|
|
||||||
from django.contrib.auth import validators
|
from django.contrib.auth import validators
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
@ -132,11 +133,16 @@ class MinimumLengthValidatorTest(SimpleTestCase):
|
|||||||
with self.assertRaises(ValidationError) as cm:
|
with self.assertRaises(ValidationError) as cm:
|
||||||
MinimumLengthValidator().validate("1234567")
|
MinimumLengthValidator().validate("1234567")
|
||||||
self.assertEqual(cm.exception.messages, [expected_error % 8])
|
self.assertEqual(cm.exception.messages, [expected_error % 8])
|
||||||
self.assertEqual(cm.exception.error_list[0].code, "password_too_short")
|
error = cm.exception.error_list[0]
|
||||||
|
self.assertEqual(error.code, "password_too_short")
|
||||||
|
self.assertEqual(error.params, {"min_length": 8})
|
||||||
|
|
||||||
with self.assertRaises(ValidationError) as cm:
|
with self.assertRaises(ValidationError) as cm:
|
||||||
MinimumLengthValidator(min_length=3).validate("12")
|
MinimumLengthValidator(min_length=3).validate("12")
|
||||||
self.assertEqual(cm.exception.messages, [expected_error % 3])
|
self.assertEqual(cm.exception.messages, [expected_error % 3])
|
||||||
|
error = cm.exception.error_list[0]
|
||||||
|
self.assertEqual(error.code, "password_too_short")
|
||||||
|
self.assertEqual(error.params, {"min_length": 3})
|
||||||
|
|
||||||
def test_help_text(self):
|
def test_help_text(self):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
@ -144,6 +150,24 @@ class MinimumLengthValidatorTest(SimpleTestCase):
|
|||||||
"Your password must contain at least 8 characters.",
|
"Your password must contain at least 8 characters.",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@mock.patch("django.contrib.auth.password_validation.ngettext")
|
||||||
|
def test_l10n(self, mock_ngettext):
|
||||||
|
with self.subTest("get_error_message"):
|
||||||
|
MinimumLengthValidator().get_error_message()
|
||||||
|
mock_ngettext.assert_called_with(
|
||||||
|
"This password is too short. It must contain at least %d character.",
|
||||||
|
"This password is too short. It must contain at least %d characters.",
|
||||||
|
8,
|
||||||
|
)
|
||||||
|
mock_ngettext.reset()
|
||||||
|
with self.subTest("get_help_text"):
|
||||||
|
MinimumLengthValidator().get_help_text()
|
||||||
|
mock_ngettext.assert_called_with(
|
||||||
|
"Your password must contain at least %(min_length)d " "character.",
|
||||||
|
"Your password must contain at least %(min_length)d " "characters.",
|
||||||
|
8,
|
||||||
|
)
|
||||||
|
|
||||||
def test_custom_error(self):
|
def test_custom_error(self):
|
||||||
class CustomMinimumLengthValidator(MinimumLengthValidator):
|
class CustomMinimumLengthValidator(MinimumLengthValidator):
|
||||||
def get_error_message(self):
|
def get_error_message(self):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user