Skip to content

Commit

Permalink
Blacken.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nina Menezes committed Mar 31, 2024
1 parent b391b83 commit fc3bd96
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 43 deletions.
31 changes: 20 additions & 11 deletions django/core/validators.py
Expand Up @@ -52,7 +52,7 @@ def __call__(self, value):
regex_matches = self.regex.search(str(value))
invalid_input = regex_matches if self.inverse_match else not regex_matches
if invalid_input:
raise ValidationError(self.message, code=self.code, params={"value": value})
raise ValidationError(self.message, code=self.code, params={"value": value})

def __eq__(self, other):
return (
Expand All @@ -67,12 +67,14 @@ def __eq__(self, other):

@deconstructible
class DomainNameValidator(RegexValidator):
message = _('Enter a valid domain name.')
message = _("Enter a valid domain name.")
ul = "\u00a1-\uffff" # Unicode letters range (must not be a raw string).
# Max length for domain name labels is 63 characters per RFC 1034 sec. 3.1
hostname_re = r"[a-z" + ul + r"0-9](?:[a-z" + ul + r"0-9-]{0,61}[a-z" + ul + r"0-9])?"
hostname_re = (
r"[a-z" + ul + r"0-9](?:[a-z" + ul + r"0-9-]{0,61}[a-z" + ul + r"0-9])?"
)
domain_re = r"(?:\.(?!-)[a-z" + ul + r"0-9-]{1,63}(?<!-))*"
# Top-level domain
# Top-level domain
tld_re = (
r"\." # dot
r"(?!-)" # can't start with a dash
Expand All @@ -94,14 +96,20 @@ class DomainNameValidator(RegexValidator):
max_length = 255

def __init__(self, **kwargs):
self.accept_idna = kwargs.pop('accept_idna', True)
self.accept_idna = kwargs.pop("accept_idna", True)

if self.accept_idna:
self.regex = _lazy_re_compile(self.hostname_re + self.domain_re + self.tld_re, re.IGNORECASE)
self.regex = _lazy_re_compile(
self.hostname_re + self.domain_re + self.tld_re, re.IGNORECASE
)
else:
self.regex = _lazy_re_compile(self.ascii_only_hostname_re + self.ascii_only_domain_re + self.ascii_only_tld_re, re.IGNORECASE)
self.regex = _lazy_re_compile(
self.ascii_only_hostname_re
+ self.ascii_only_domain_re
+ self.ascii_only_tld_re,
re.IGNORECASE,
)
super().__init__(**kwargs)


def __call__(self, value):
if not isinstance(value, str) or len(value) > self.max_length:
Expand All @@ -111,11 +119,12 @@ def __call__(self, value):
super().__call__(value)
else:
if not value.isascii():
raise ValidationError(self.message, code=self.code, params={"value": value})
raise ValidationError(
self.message, code=self.code, params={"value": value}
)
super().__call__(value)



validate_domain_name = DomainNameValidator()


Expand Down Expand Up @@ -298,7 +307,7 @@ def __eq__(self, other):

validate_email = EmailValidator()

slug_re = _lazy_re_compile(r'^[-a-zA-Z0-9_]+\Z')
slug_re = _lazy_re_compile(r"^[-a-zA-Z0-9_]+\Z")
validate_slug = RegexValidator(
slug_re,
# Translators: "letters" means latin letters: a-z and A-Z.
Expand Down
66 changes: 34 additions & 32 deletions tests/validators/tests.py
Expand Up @@ -620,31 +620,35 @@
(ProhibitNullCharactersValidator(), "\x00something", ValidationError),
(ProhibitNullCharactersValidator(), "something", None),
(ProhibitNullCharactersValidator(), None, None),
(validate_domain_name, '000000.org', None),
(validate_domain_name, 'python.org', None),
(validate_domain_name, 'python.co.uk', None),
(validate_domain_name, 'python.tk', None),
(validate_domain_name, 'domain.with.idn.tld.उदाहरण.परीक्ष', None),
(validate_domain_name, 'ıçğü.com', None),
(validate_domain_name, 'xn--7ca6byfyc.com', None),
(validate_domain_name, 'hg.python.org', None),
(validate_domain_name, 'python.xyz', None),
(validate_domain_name, 'djangoproject.com', None),
(validate_domain_name, 'DJANGOPROJECT.COM', None),
(validate_domain_name, 'spam.eggs', None),
(validate_domain_name, 'python-python.com', None),
(validate_domain_name, 'python.name.uk', None),
(validate_domain_name, 'python.tips', None),
(validate_domain_name, 'http://例子.测试', None),
(validate_domain_name, 'http://dashinpunytld.xn---c', None),
(validate_domain_name, 'python..org', ValidationError),
(validate_domain_name, 'python-.org', ValidationError),
(validate_domain_name, 'too-long-name.'*20+'com', ValidationError),
(validate_domain_name, 'stupid-name试', ValidationError),
(DomainNameValidator(accept_idna=False), 'non-idna-domain-name-passes.com', None),
(DomainNameValidator(accept_idna=False), 'domain.with.idn.tld.उदाहरण.परीक्ष', ValidationError),
(DomainNameValidator(accept_idna=False), 'ıçğü.com', ValidationError),
(DomainNameValidator(accept_idna=False), 'not-domain-name', ValidationError)
(validate_domain_name, "000000.org", None),
(validate_domain_name, "python.org", None),
(validate_domain_name, "python.co.uk", None),
(validate_domain_name, "python.tk", None),
(validate_domain_name, "domain.with.idn.tld.उदाहरण.परीक्ष", None),
(validate_domain_name, "ıçğü.com", None),
(validate_domain_name, "xn--7ca6byfyc.com", None),
(validate_domain_name, "hg.python.org", None),
(validate_domain_name, "python.xyz", None),
(validate_domain_name, "djangoproject.com", None),
(validate_domain_name, "DJANGOPROJECT.COM", None),
(validate_domain_name, "spam.eggs", None),
(validate_domain_name, "python-python.com", None),
(validate_domain_name, "python.name.uk", None),
(validate_domain_name, "python.tips", None),
(validate_domain_name, "http://例子.测试", None),
(validate_domain_name, "http://dashinpunytld.xn---c", None),
(validate_domain_name, "python..org", ValidationError),
(validate_domain_name, "python-.org", ValidationError),
(validate_domain_name, "too-long-name." * 20 + "com", ValidationError),
(validate_domain_name, "stupid-name试", ValidationError),
(DomainNameValidator(accept_idna=False), "non-idna-domain-name-passes.com", None),
(
DomainNameValidator(accept_idna=False),
"domain.with.idn.tld.उदाहरण.परीक्ष",
ValidationError,
),
(DomainNameValidator(accept_idna=False), "ıçğü.com", ValidationError),
(DomainNameValidator(accept_idna=False), "not-domain-name", ValidationError),
]

# Add valid and invalid URL tests.
Expand Down Expand Up @@ -886,15 +890,13 @@ def test_domain_name_equality(self):
)
self.assertNotEqual(
DomainNameValidator(),
DomainNameValidator(code='custom_code'),
DomainNameValidator(code="custom_code"),
)
self.assertEqual(
DomainNameValidator(message='custom error message'),
DomainNameValidator(message='custom error message'),
DomainNameValidator(message="custom error message"),
DomainNameValidator(message="custom error message"),
)
self.assertNotEqual(
DomainNameValidator(message='custom error message'),
DomainNameValidator(
message='custom error message', code='custom_code'
),
DomainNameValidator(message="custom error message"),
DomainNameValidator(message="custom error message", code="custom_code"),
)

0 comments on commit fc3bd96

Please sign in to comment.