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