DomainnameOption add network support

This commit is contained in:
Emmanuel Garette 2019-11-19 18:48:13 +01:00
parent c5d8e0055b
commit 6b77b0c5ad
1 changed files with 19 additions and 16 deletions

View File

@ -51,8 +51,7 @@ class DomainnameOption(StrOption):
properties: Optional[List[str]]=None,
warnings_only: bool=False,
allow_ip: bool=False,
allow_network: bool=False,
network_cidr: bool=False,
allow_cidr_network: bool=False,
type: str='domainname',
allow_without_dot: bool=False,
allow_startswith_dot: bool=False) -> None:
@ -62,8 +61,8 @@ class DomainnameOption(StrOption):
extra = {'_dom_type': type}
if not isinstance(allow_ip, bool):
raise ValueError(_('allow_ip must be a boolean'))
if not isinstance(allow_network, bool):
raise ValueError(_('allow_network must be a boolean'))
if not isinstance(allow_cidr_network, bool):
raise ValueError(_('allow_cidr_network must be a boolean'))
if not isinstance(allow_without_dot, bool):
raise ValueError(_('allow_without_dot must be a boolean'))
if not isinstance(allow_startswith_dot, bool):
@ -93,11 +92,11 @@ class DomainnameOption(StrOption):
extra['_ip'] = IPOption(name,
doc)
extra['_allow_ip'] = allow_ip
if allow_network:
if allow_cidr_network:
extra['_network'] = NetworkOption(name,
doc,
cidr=network_cidr)
extra['_allow_network'] = allow_network
cidr=True)
extra['_allow_cidr_network'] = allow_cidr_network
extra['_allow_startswith_dot'] = allow_startswith_dot
super().__init__(name,
@ -143,17 +142,17 @@ class DomainnameOption(StrOption):
def _validate_ip_network(self,
value: str) -> None:
allow_ip = self.impl_get_extra('_allow_ip')
allow_network = self.impl_get_extra('_allow_network')
if allow_ip is False and allow_network is False:
allow_cidr_network = self.impl_get_extra('_allow_cidr_network')
if allow_ip is False and allow_cidr_network is False:
raise ValueError(_('must not be an IP'))
if allow_ip is True:
try:
self.impl_get_extra('_ip').validate(value)
return
except ValueError as err:
if allow_network is False:
if allow_cidr_network is False:
raise err
if allow_network is True:
if allow_cidr_network is True:
self.impl_get_extra('_network').validate(value)
def validate(self,
@ -172,7 +171,11 @@ class DomainnameOption(StrOption):
warnings_only: bool) -> None:
if self.impl_get_extra('_has_upper').search(value):
raise ValueError(_('some characters are uppercase'))
if not self.impl_get_extra('_domain_re').search(value):
if self.impl_get_extra('_allow_startswith_dot') and value.startswith('.'):
val = value[1:]
else:
val = value
if not self.impl_get_extra('_domain_re').search(val):
if warnings_only:
raise ValueError(self.impl_get_extra('_domain_re_message_warning'))
raise ValueError(self.impl_get_extra('_domain_re_message'))
@ -181,18 +184,18 @@ class DomainnameOption(StrOption):
value: str,
warnings_only: bool) -> None:
allow_ip = self.impl_get_extra('_allow_ip')
allow_network = self.impl_get_extra('_allow_network')
allow_cidr_network = self.impl_get_extra('_allow_cidr_network')
# it's an IP so validate with IPOption
if allow_ip is False and allow_network is False:
if allow_ip is False and allow_cidr_network is False:
raise ValueError(_('must not be an IP'))
if allow_ip is True:
try:
self.impl_get_extra('_ip').second_level_validation(value, warnings_only)
return
except ValueError as err:
if allow_network is False:
if allow_cidr_network is False:
raise err
if allow_network is True:
if allow_cidr_network is True:
self.impl_get_extra('_network').second_level_validation(value, warnings_only)
def second_level_validation(self,