Created
November 17, 2016 22:18
-
-
Save exit99/1b776e6354994380c465be602b6f7235 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class _BaseDomainNameValidator(BaseValidator): | |
def __call__(self, form, field): | |
labels = get_labels(value) | |
if not labels: | |
return False | |
is_valid = True | |
for index, label in enumerate(labels): | |
if index == 0 and label == '*': | |
continue | |
if not label and index + 1 != len(labels): | |
is_valid = False | |
label_str = get_label_str(label) | |
if re.search(self.regex, test_str): | |
field.errors.append('Must be a valid domain name.') | |
is_valid = False | |
return is_valid | |
def get_labels(self, value): | |
if not value or value in ['@', '*']: | |
return [] | |
return value.split('.') | |
def get_label_str(label): | |
"""Can be overridden""" | |
return label | |
class DomainNameValidator(BaseValidator): | |
regex = '[^a-zA-Z0-9-_]' | |
class TxtRecordDomainNameValidator(BaseValidator): | |
regex = '[^a-zA-Z0-9-]' | |
def get_label_str(self, label): | |
return label[1:] if label.startswith('_') else label |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment