from django.core import validators from django.utils import six from django.utils.deconstruct import deconstructible from django.utils.translation import ugettext_lazy as _ import re @deconstructible class HandleValidator(validators.RegexValidator): regex = r'^(?:[A-Z]+[0-9]+(-[A-Z]+)|AUTO)' message = _( 'Enter a valid handle (all uppercase)' ) flags = re.ASCII if six.PY3 else 0 @deconstructible class HandleValidatorWithSuffix(validators.RegexValidator): flags = re.ASCII if six.PY3 else 0 def __init__(self, suffix): self.regex = r'^(?:[A-Z]+[0-9]+-%s|AUTO)' % re.escape(suffix) print(self.regex) self.message = _( 'Enter a valid handle with suffix %s (all uppercase)' % suffix ) super(HandleValidatorWithSuffix, self).__init__()