Refs #31670 -- Renamed whitelist argument and attribute of EmailValidator.

This commit is contained in:
David Smith 2020-06-17 07:35:09 +01:00 committed by Mariusz Felisiak
parent 26a413507a
commit 27c09043da
6 changed files with 104 additions and 18 deletions

View file

@ -1,10 +1,12 @@
import ipaddress
import re
import warnings
from pathlib import Path
from urllib.parse import urlsplit, urlunsplit
from django.core.exceptions import ValidationError
from django.utils.deconstruct import deconstructible
from django.utils.deprecation import RemovedInDjango41Warning
from django.utils.encoding import punycode
from django.utils.ipv6 import is_valid_ipv6_address
from django.utils.regex_helper import _lazy_re_compile
@ -167,15 +169,42 @@ class EmailValidator:
# literal form, ipv4 or ipv6 address (SMTP 4.1.3)
r'\[([A-f0-9:.]+)\]\Z',
re.IGNORECASE)
domain_whitelist = ['localhost']
domain_allowlist = ['localhost']
def __init__(self, message=None, code=None, whitelist=None):
@property
def domain_whitelist(self):
warnings.warn(
'The domain_whitelist attribute is deprecated in favor of '
'domain_allowlist.',
RemovedInDjango41Warning,
stacklevel=2,
)
return self.domain_allowlist
@domain_whitelist.setter
def domain_whitelist(self, allowlist):
warnings.warn(
'The domain_whitelist attribute is deprecated in favor of '
'domain_allowlist.',
RemovedInDjango41Warning,
stacklevel=2,
)
self.domain_allowlist = allowlist
def __init__(self, message=None, code=None, allowlist=None, *, whitelist=None):
if whitelist is not None:
allowlist = whitelist
warnings.warn(
'The whitelist argument is deprecated in favor of allowlist.',
RemovedInDjango41Warning,
stacklevel=2,
)
if message is not None:
self.message = message
if code is not None:
self.code = code
if whitelist is not None:
self.domain_whitelist = whitelist
if allowlist is not None:
self.domain_allowlist = allowlist
def __call__(self, value):
if not value or '@' not in value:
@ -186,7 +215,7 @@ class EmailValidator:
if not self.user_regex.match(user_part):
raise ValidationError(self.message, code=self.code)
if (domain_part not in self.domain_whitelist and
if (domain_part not in self.domain_allowlist and
not self.validate_domain_part(domain_part)):
# Try for possible IDN domain-part
try:
@ -215,7 +244,7 @@ class EmailValidator:
def __eq__(self, other):
return (
isinstance(other, EmailValidator) and
(self.domain_whitelist == other.domain_whitelist) and
(self.domain_allowlist == other.domain_allowlist) and
(self.message == other.message) and
(self.code == other.code)
)