UI improvement
This commit is contained in:
parent
575edc836d
commit
9a272ee959
16 changed files with 2477 additions and 1604 deletions
|
|
@ -1,4 +1,5 @@
|
|||
import re
|
||||
import ipaddress
|
||||
|
||||
# Curated IANA timezone list for the dropdown. Validation accepts any entry from this set.
|
||||
VALID_TIMEZONES = [
|
||||
|
|
@ -113,12 +114,25 @@ def hostname(value, max_len=253):
|
|||
return _strip(value.lower(), r'[^a-z0-9\-.]', max_len)
|
||||
|
||||
def ip(value, max_len=45):
|
||||
"""IPv4 or IPv6 address: digits, dots, colons, hex letters."""
|
||||
return _strip(value, r'[^0-9a-fA-F.:]', max_len)
|
||||
"""IPv4 or IPv6 address. Returns '' if not a valid address."""
|
||||
cleaned = _strip(value, r'[^0-9a-fA-F.:]', max_len)
|
||||
try:
|
||||
ipaddress.ip_address(cleaned)
|
||||
return cleaned
|
||||
except ValueError:
|
||||
return ''
|
||||
|
||||
def ip_or_cidr(value, max_len=49):
|
||||
"""IP address or CIDR subnet: adds forward slash."""
|
||||
return _strip(value, r'[^0-9a-fA-F.:/]', max_len)
|
||||
"""IP address or CIDR subnet. Returns '' if not valid."""
|
||||
cleaned = _strip(value, r'[^0-9a-fA-F.:/]', max_len)
|
||||
try:
|
||||
if '/' in cleaned:
|
||||
ipaddress.ip_network(cleaned, strict=False)
|
||||
else:
|
||||
ipaddress.ip_address(cleaned)
|
||||
return cleaned
|
||||
except ValueError:
|
||||
return ''
|
||||
|
||||
def mac(value, max_len=17):
|
||||
"""MAC address: hex digits and colons."""
|
||||
|
|
@ -154,3 +168,24 @@ def email(value, max_len=254):
|
|||
def timezone(value):
|
||||
"""Timezone string: must be in VALID_TIMEZONES list. Returns '' if not found."""
|
||||
return value if value in _TIMEZONE_SET else ''
|
||||
|
||||
_DOTTED_TO_PREFIX = {
|
||||
'255.0.0.0': 8, '255.255.0.0': 16, '255.255.255.0': 24,
|
||||
'255.255.255.128': 25, '255.255.255.192': 26,
|
||||
'255.255.255.224': 27, '255.255.255.240': 28,
|
||||
'255.255.255.248': 29, '255.255.255.252': 30,
|
||||
}
|
||||
|
||||
def subnet_mask(value):
|
||||
"""Subnet prefix length 1-30 (integer). Also accepts legacy dotted notation.
|
||||
Returns int on success, None if invalid."""
|
||||
s = str(value).strip()
|
||||
if s in _DOTTED_TO_PREFIX:
|
||||
return _DOTTED_TO_PREFIX[s]
|
||||
try:
|
||||
n = int(s)
|
||||
if 1 <= n <= 30:
|
||||
return n
|
||||
except (ValueError, TypeError):
|
||||
pass
|
||||
return None
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue