make a regexp to valid domain
This commit is contained in:
parent
7977efe246
commit
6bf7e309c6
1 changed files with 29 additions and 25 deletions
|
@ -465,7 +465,21 @@ class DomainnameOption(Option):
|
|||
raise ValueError(_('allow_without_dot must be a boolean'))
|
||||
extra['_allow_ip'] = allow_ip
|
||||
extra['_allow_without_dot'] = allow_without_dot
|
||||
extra['_domain_re'] = re.compile(r'^[a-z\d][a-z\d\-]*$')
|
||||
# FIXME should be
|
||||
# regexp = r'^((?!-)[a-z0-9-]{1,63}(?<!-)\.{0,1})$'
|
||||
if type_ == 'domainname':
|
||||
if allow_without_dot:
|
||||
min_time = 0
|
||||
else:
|
||||
min_time = 1
|
||||
regexp = r'((?!-)[a-z0-9-]{{{1},{0}}}\.){{{1},}}[a-z0-9-]{{1,{0}}}'.format(self._get_len(type_), min_time)
|
||||
else:
|
||||
regexp = r'((?!-)[a-z0-9-]{{1,{0}}})'.format(self._get_len(type_))
|
||||
if allow_ip:
|
||||
regexp = r'^(?:{0}|(?:(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){{3}}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)))$'.format(regexp)
|
||||
else:
|
||||
regexp = r'^{0}$'.format(regexp)
|
||||
extra['_domain_re'] = re.compile(regexp)
|
||||
extra['_has_upper'] = re.compile('[A-Z]')
|
||||
|
||||
super(DomainnameOption, self).__init__(name, doc, default=default,
|
||||
|
@ -480,6 +494,12 @@ class DomainnameOption(Option):
|
|||
warnings_only=warnings_only,
|
||||
extra=extra)
|
||||
|
||||
def _get_len(self, type_):
|
||||
if type_ == 'netbios':
|
||||
return 15
|
||||
else:
|
||||
return 63
|
||||
|
||||
def _validate(self, value, context=undefined, current_opt=undefined):
|
||||
err = self._impl_valid_unicode(value)
|
||||
if err:
|
||||
|
@ -505,10 +525,7 @@ class DomainnameOption(Option):
|
|||
pass
|
||||
else:
|
||||
raise ValueError(_('must not be an IP'))
|
||||
if self._get_extra('_dom_type') == 'netbios':
|
||||
part_name_length = 15
|
||||
else:
|
||||
part_name_length = 63
|
||||
part_name_length = self._get_len(self._get_extra('_dom_type'))
|
||||
if self._get_extra('_dom_type') == 'domainname':
|
||||
if not self._get_extra('_allow_without_dot') and not "." in value:
|
||||
return ValueError(_("must have dot"))
|
||||
|
@ -522,26 +539,13 @@ class DomainnameOption(Option):
|
|||
return _valid_length(value)
|
||||
|
||||
def _second_level_validation(self, value, warnings_only):
|
||||
def _valid_char(val):
|
||||
if self._get_extra('_has_upper').search(val):
|
||||
return ValueError(_('some characters are uppercase'))
|
||||
if not self._get_extra('_domain_re').search(val):
|
||||
if warnings_only:
|
||||
return ValueError(_('some characters may cause problems'))
|
||||
else:
|
||||
return ValueError()
|
||||
#not for IP
|
||||
if self._get_extra('_allow_ip') is True:
|
||||
try:
|
||||
IP('{0}/32'.format(value))
|
||||
return
|
||||
except ValueError:
|
||||
pass
|
||||
if self._get_extra('_dom_type') == 'domainname':
|
||||
for dom in value.split('.'):
|
||||
return _valid_char(dom)
|
||||
else:
|
||||
return _valid_char(value)
|
||||
if self._get_extra('_has_upper').search(value):
|
||||
return ValueError(_('some characters are uppercase'))
|
||||
if not self._get_extra('_domain_re').search(value):
|
||||
if warnings_only:
|
||||
return ValueError(_('some characters may cause problems'))
|
||||
else:
|
||||
return ValueError()
|
||||
|
||||
|
||||
class URLOption(DomainnameOption):
|
||||
|
|
Loading…
Reference in a new issue