'Check that the hostname is valid'

def validate_domain(self, value):

    if (len(value) > 255):
        raise serializers.ValidationError(u'Hostname    must    be    255    characters    or    less.')
    if (value[(-1):] == u'.'):
        value = value[:(-1)]
    labels = value.split(u'.')
    if (u'xip.io' in value):
        return value
    if (labels[0] == u'*'):
        raise serializers.ValidationError(u'Adding    a    wildcard    subdomain    is    currently    not    supported.')
    allowed = re.compile(u'^(?!-)[a-z0-9-]{1,63}(?<!-)$', re.IGNORECASE)
    for label in labels:
        match = allowed.match(label)
        if ((not match) or (u'--' in label) or label.isdigit() or ((len(labels) == 1) and any((char.isdigit() for char in label)))):
            raise serializers.ValidationError(u'Hostname    does    not    look    valid.')
    if models.Domain.objects.filter(domain=value).exists():
        raise serializers.ValidationError(u'The    domain    {}    is    already    in    use    by    another    app'.format(value))
    return value
