def validate_domain(self, value):
"""
Check that the hostname is valid
"""
if len(value) > 255:
raise serializers.ValidationError('Hostname must be 255 characters or less.')
if value[-1:] == ".":
value = value[:-1] # strip exactly one dot from the right, if present
labels = value.split('.')
if 'xip.io' in value:
return value
if labels[0] == '*':
raise serializers.ValidationError(
'Adding a wildcard subdomain is currently not supported.')
allowed = re.compile("^(?!-)[a-z0-9-]{1,63}(?<!-)$", re.IGNORECASE)
for label in labels:
match = allowed.match(label)
if not match or '--' in label or label.isdigit() or \
len(labels) == 1 and any(char.isdigit() for char in label):
raise serializers.ValidationError('Hostname does not look valid.')
if models.Domain.objects.filter(domain=value).exists():
raise serializers.ValidationError(
"The domain {} is already in use by another app".format(value))
return value
评论列表
文章目录