Fix attempt for #695.

This commit is contained in:
Antoine Nguyen
2015-02-19 13:21:32 +01:00
parent 2e99cf7690
commit c4e693afeb
+10 -8
View File
@@ -333,17 +333,19 @@ class TabForms(object):
#
def is_valid_host(host):
"""IDN compatible domain validator
"""
host = host.encode('idna').lower()
if not hasattr(is_valid_host, '_re'):
is_valid_host._re = re.compile(r'^([0-9a-z][-\w]*[0-9a-z]\.)+[a-z0-9\-]{2,15}$')
return bool(is_valid_host._re.match(host))
def is_valid_hostname(hostname):
"""Domain name validaton."""
if len(hostname) > 255:
return False
if hostname[-1] == ".":
hostname = hostname[:-1] # strip exactly one dot from the
# right, if present
allowed = re.compile(r"(?!-)[A-Z\d-]{1,63}(?<!-)$", re.IGNORECASE)
return all(allowed.match(x) for x in hostname.split("."))
def validate_domain_name(value):
if not is_valid_host(value):
if not is_valid_hostname(value):
raise ValidationError(_('Enter a valid domain name'), 'invalid')