feat: can get params value easily
This commit is contained in:
parent
90947b5578
commit
6e4b22aea2
7 changed files with 61 additions and 49 deletions
|
@ -55,12 +55,17 @@ class DomainnameOption(StrOption):
|
|||
allow_without_dot: bool = False,
|
||||
allow_startswith_dot: bool = False,
|
||||
test_existence: bool = False,
|
||||
_extra: dict = None,
|
||||
**kwargs,
|
||||
) -> None:
|
||||
# pylint: disable=too-many-branches,too-many-locals,too-many-arguments
|
||||
if _extra is None:
|
||||
extra = {}
|
||||
else:
|
||||
extra = _extra
|
||||
if type not in ["netbios", "hostname", "domainname"]:
|
||||
raise ValueError(_("unknown type {0} for hostname").format(type))
|
||||
extra = {"_dom_type": type}
|
||||
extra["type"] = type
|
||||
if not isinstance(allow_ip, bool):
|
||||
raise ValueError(_("allow_ip must be a boolean"))
|
||||
if not isinstance(allow_cidr_network, bool):
|
||||
|
@ -69,8 +74,8 @@ class DomainnameOption(StrOption):
|
|||
raise ValueError(_("allow_without_dot must be a boolean"))
|
||||
if not isinstance(allow_startswith_dot, bool):
|
||||
raise ValueError(_("allow_startswith_dot must be a boolean"))
|
||||
extra["_allow_without_dot"] = allow_without_dot
|
||||
extra["_test_existence"] = test_existence
|
||||
extra["allow_without_dot"] = allow_without_dot
|
||||
extra["test_existence"] = test_existence
|
||||
if type == "domainname":
|
||||
if allow_without_dot:
|
||||
min_time = 0
|
||||
|
@ -114,15 +119,15 @@ class DomainnameOption(StrOption):
|
|||
name,
|
||||
doc,
|
||||
)
|
||||
extra["_allow_ip"] = allow_ip
|
||||
extra["allow_ip"] = allow_ip
|
||||
if allow_cidr_network:
|
||||
extra["_network"] = NetworkOption(
|
||||
name,
|
||||
doc,
|
||||
cidr=True,
|
||||
)
|
||||
extra["_allow_cidr_network"] = allow_cidr_network
|
||||
extra["_allow_startswith_dot"] = allow_startswith_dot
|
||||
extra["allow_cidr_network"] = allow_cidr_network
|
||||
extra["allow_startswith_dot"] = allow_startswith_dot
|
||||
|
||||
super().__init__(
|
||||
name,
|
||||
|
@ -146,13 +151,13 @@ class DomainnameOption(StrOption):
|
|||
_("invalid length (max {0})" "").format(part_name_length)
|
||||
)
|
||||
|
||||
part_name_length = self._get_len(self.impl_get_extra("_dom_type"))
|
||||
if self.impl_get_extra("_dom_type") == "domainname":
|
||||
if not self.impl_get_extra("_allow_without_dot") and not "." in value:
|
||||
part_name_length = self._get_len(self.impl_get_extra("type"))
|
||||
if self.impl_get_extra("type") == "domainname":
|
||||
if not self.impl_get_extra("allow_without_dot") and not "." in value:
|
||||
raise ValueError(_("must have dot"))
|
||||
if len(value) > 255:
|
||||
raise ValueError(_("invalid length (max 255)"))
|
||||
if self.impl_get_extra("_allow_startswith_dot") and value.startswith("."):
|
||||
if self.impl_get_extra("allow_startswith_dot") and value.startswith("."):
|
||||
val = value[1:]
|
||||
else:
|
||||
val = value
|
||||
|
@ -167,7 +172,7 @@ class DomainnameOption(StrOption):
|
|||
self._validate_domain_resolution(value)
|
||||
|
||||
def _validate_domain_resolution(self, value: str) -> None:
|
||||
if not value.startswith(".") and self.impl_get_extra("_test_existence") is True:
|
||||
if not value.startswith(".") and self.impl_get_extra("test_existence") is True:
|
||||
try:
|
||||
socket.gethostbyname(value)
|
||||
except socket.gaierror as err:
|
||||
|
@ -180,8 +185,8 @@ class DomainnameOption(StrOption):
|
|||
) from err
|
||||
|
||||
def _validate_ip_network(self, value: str) -> None:
|
||||
allow_ip = self.impl_get_extra("_allow_ip")
|
||||
allow_cidr_network = self.impl_get_extra("_allow_cidr_network")
|
||||
allow_ip = self.impl_get_extra("allow_ip")
|
||||
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:
|
||||
|
@ -207,7 +212,7 @@ class DomainnameOption(StrOption):
|
|||
def _second_level_validation_domain(self, value: str, warnings_only: bool) -> None:
|
||||
if self.impl_get_extra("_has_upper").search(value):
|
||||
raise ValueError(_("some characters are uppercase"))
|
||||
if self.impl_get_extra("_allow_startswith_dot") and value.startswith("."):
|
||||
if self.impl_get_extra("allow_startswith_dot") and value.startswith("."):
|
||||
val = value[1:]
|
||||
else:
|
||||
val = value
|
||||
|
@ -223,8 +228,8 @@ class DomainnameOption(StrOption):
|
|||
def _second_level_validation_ip_network(
|
||||
self, value: str, warnings_only: bool
|
||||
) -> None:
|
||||
allow_ip = self.impl_get_extra("_allow_ip")
|
||||
allow_cidr_network = self.impl_get_extra("_allow_cidr_network")
|
||||
allow_ip = self.impl_get_extra("allow_ip")
|
||||
allow_cidr_network = self.impl_get_extra("allow_cidr_network")
|
||||
# it's an IP so validate with IPOption
|
||||
if allow_ip is True and allow_cidr_network is False:
|
||||
try:
|
||||
|
|
|
@ -52,9 +52,9 @@ class FilenameOption(StrOption):
|
|||
if typ not in ["file", "directory"]:
|
||||
raise ValueError(f'unknown type "{typ}" for "{name}"')
|
||||
extra = {
|
||||
"_allow_relative": allow_relative,
|
||||
"_test_existence": test_existence,
|
||||
"_types": types,
|
||||
"allow_relative": allow_relative,
|
||||
"test_existence": test_existence,
|
||||
"types": types,
|
||||
}
|
||||
super().__init__(name, *args, extra=extra, **kwargs)
|
||||
|
||||
|
@ -63,10 +63,10 @@ class FilenameOption(StrOption):
|
|||
value: str,
|
||||
) -> None:
|
||||
super().validate(value)
|
||||
if not self.impl_get_extra("_allow_relative") and not value.startswith("/"):
|
||||
if not self.impl_get_extra("allow_relative") and not value.startswith("/"):
|
||||
raise ValueError(_('must starts with "/"'))
|
||||
if value is not None and self.impl_get_extra("_test_existence"):
|
||||
types = self.impl_get_extra("_types")
|
||||
if value is not None and self.impl_get_extra("test_existence"):
|
||||
types = self.impl_get_extra("types")
|
||||
file = Path(value)
|
||||
found = False
|
||||
if "file" in types and file.is_file():
|
||||
|
|
|
@ -43,9 +43,9 @@ class IPOption(StrOption):
|
|||
):
|
||||
if extra is None:
|
||||
extra = {}
|
||||
extra["_private_only"] = private_only
|
||||
extra["_allow_reserved"] = allow_reserved
|
||||
extra["_cidr"] = cidr
|
||||
extra["private_only"] = private_only
|
||||
extra["allow_reserved"] = allow_reserved
|
||||
extra["cidr"] = cidr
|
||||
super().__init__(*args, extra=extra, **kwargs)
|
||||
|
||||
def _validate_cidr(self, value):
|
||||
|
@ -66,7 +66,7 @@ class IPOption(StrOption):
|
|||
|
||||
def validate(self, value: str) -> None:
|
||||
super().validate(value)
|
||||
if self.impl_get_extra("_cidr"):
|
||||
if self.impl_get_extra("cidr"):
|
||||
if "/" not in value:
|
||||
raise ValueError(_('CIDR address must have a "/"'))
|
||||
self._validate_cidr(value)
|
||||
|
@ -75,13 +75,13 @@ class IPOption(StrOption):
|
|||
|
||||
def second_level_validation(self, value: str, warnings_only: bool) -> None:
|
||||
ip_obj = ip_interface(value)
|
||||
if not self.impl_get_extra("_allow_reserved") and ip_obj.is_reserved:
|
||||
if not self.impl_get_extra("allow_reserved") and ip_obj.is_reserved:
|
||||
if warnings_only:
|
||||
msg = _("shouldn't be reserved IP")
|
||||
else:
|
||||
msg = _("mustn't be reserved IP")
|
||||
raise ValueError(msg)
|
||||
if self.impl_get_extra("_private_only") and not ip_obj.is_private:
|
||||
if self.impl_get_extra("private_only") and not ip_obj.is_private:
|
||||
if warnings_only:
|
||||
msg = _("should be private IP")
|
||||
else:
|
||||
|
|
|
@ -32,14 +32,14 @@ class NetworkOption(StrOption):
|
|||
_type = "network address"
|
||||
|
||||
def __init__(self, *args, cidr=False, **kwargs):
|
||||
extra = {"_cidr": cidr}
|
||||
extra = {"cidr": cidr}
|
||||
super().__init__(*args, extra=extra, **kwargs)
|
||||
|
||||
def validate(self, value: str) -> None:
|
||||
super().validate(value)
|
||||
if value.count(".") != 3:
|
||||
raise ValueError()
|
||||
cidr = self.impl_get_extra("_cidr")
|
||||
cidr = self.impl_get_extra("cidr")
|
||||
if cidr:
|
||||
if "/" not in value:
|
||||
raise ValueError(_("must use CIDR notation"))
|
||||
|
|
|
@ -50,15 +50,21 @@ class PortOption(StrOption):
|
|||
allow_registred: bool = True,
|
||||
allow_protocol: bool = False,
|
||||
allow_private: bool = False,
|
||||
_extra: dict = None,
|
||||
**kwargs,
|
||||
) -> None:
|
||||
|
||||
extra = {
|
||||
"_allow_range": allow_range,
|
||||
"_allow_protocol": allow_protocol,
|
||||
"_min_value": None,
|
||||
"_max_value": None,
|
||||
}
|
||||
if _extra is None:
|
||||
extra = {}
|
||||
else:
|
||||
extra = _extra
|
||||
extra["allow_range"] = allow_range
|
||||
extra["allow_protocol"] = allow_protocol
|
||||
extra["allow_zero"] = allow_zero
|
||||
extra["allow_wellknown"] = allow_wellknown
|
||||
extra["allow_registred"] = allow_registred
|
||||
extra["allow_private"] = allow_private
|
||||
extra["_min_value"] = None
|
||||
extra["_max_value"] = None
|
||||
ports_min = [0, 1, 1024, 49152]
|
||||
ports_max = [0, 1023, 49151, 65535]
|
||||
is_finally = False
|
||||
|
@ -82,11 +88,11 @@ class PortOption(StrOption):
|
|||
|
||||
def validate(self, value: str) -> None:
|
||||
super().validate(value)
|
||||
if self.impl_get_extra("_allow_protocol") and (
|
||||
if self.impl_get_extra("allow_protocol") and (
|
||||
value.startswith("tcp:") or value.startswith("udp:")
|
||||
):
|
||||
value = [value[4:]]
|
||||
elif self.impl_get_extra("_allow_range") and ":" in str(value):
|
||||
elif self.impl_get_extra("allow_range") and ":" in str(value):
|
||||
value = value.split(":")
|
||||
if len(value) != 2:
|
||||
raise ValueError(_("range must have two values only"))
|
||||
|
@ -102,7 +108,7 @@ class PortOption(StrOption):
|
|||
raise ValueError()
|
||||
|
||||
def second_level_validation(self, value: str, warnings_only: bool) -> None:
|
||||
if self.impl_get_extra("_allow_protocol") and (
|
||||
if self.impl_get_extra("allow_protocol") and (
|
||||
value.startswith("tcp:") or value.startswith("udp:")
|
||||
):
|
||||
value = [value[4:]]
|
||||
|
|
|
@ -54,15 +54,16 @@ class URLOption(StrOption):
|
|||
**kwargs,
|
||||
) -> None:
|
||||
# pylint: disable=too-many-arguments,too-many-locals,redefined-builtin
|
||||
extra = {
|
||||
"_domainname": DomainnameOption(
|
||||
extra = {}
|
||||
extra["_domainname"] = DomainnameOption(
|
||||
name,
|
||||
doc,
|
||||
allow_ip=allow_ip,
|
||||
type=type,
|
||||
allow_without_dot=allow_without_dot,
|
||||
),
|
||||
"_port": PortOption(
|
||||
_extra=extra,
|
||||
)
|
||||
extra["_port"] = PortOption(
|
||||
name,
|
||||
doc,
|
||||
allow_range=allow_range,
|
||||
|
@ -70,8 +71,8 @@ class URLOption(StrOption):
|
|||
allow_wellknown=allow_wellknown,
|
||||
allow_registred=allow_registred,
|
||||
allow_private=allow_private,
|
||||
),
|
||||
}
|
||||
_extra=extra,
|
||||
)
|
||||
super().__init__(
|
||||
name,
|
||||
doc,
|
||||
|
|
|
@ -646,9 +646,9 @@ class TiramisuDict:
|
|||
if self.remotable == "all" or childapi.has_dependency():
|
||||
obj_form["remote"] = True
|
||||
if childtype == "IPOption" and (
|
||||
child.impl_get_extra("_private_only")
|
||||
or not child.impl_get_extra("_allow_reserved")
|
||||
or child.impl_get_extra("_cidr")
|
||||
child.impl_get_extra("private_only")
|
||||
or not child.impl_get_extra("allow_reserved")
|
||||
or child.impl_get_extra("cidr")
|
||||
):
|
||||
obj_form["remote"] = True
|
||||
if childtype == "DateOption":
|
||||
|
|
Loading…
Reference in a new issue