Ticket #13511: 13511-regexvalidator.diff
File 13511-regexvalidator.diff, 1.6 KB (added by , 14 years ago) |
---|
-
django/core/validators.py
16 16 URL_VALIDATOR_USER_AGENT = 'Django (http://www.djangoproject.com/)' 17 17 18 18 class RegexValidator(object): 19 regex = ''20 19 message = _(u'Enter a valid value.') 21 20 code = 'invalid' 22 21 23 def __init__(self, regex=None, message=None, code=None): 24 if regex is not None: 25 self.regex = regex 22 def __init__(self, regex, message=None, code=None): 26 23 if message is not None: 27 24 self.message = message 28 25 if code is not None: 29 26 self.code = code 30 27 31 if isinstance( self.regex, basestring):28 if isinstance(regex, basestring): 32 29 self.regex = re.compile(regex) 30 else: 31 # regex is already compiled 32 self.regex = regex 33 33 34 34 def __call__(self, value): 35 35 """ … … 39 39 raise ValidationError(self.message, code=self.code) 40 40 41 41 class URLValidator(RegexValidator): 42 regex= re.compile(42 URL_PATTERN = re.compile( 43 43 r'^https?://' # http:// or https:// 44 44 r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?|' #domain... 45 45 r'localhost|' #localhost... … … 48 48 r'(?:/?|[/?]\S+)$', re.IGNORECASE) 49 49 50 50 def __init__(self, verify_exists=False, validator_user_agent=URL_VALIDATOR_USER_AGENT): 51 super(URLValidator, self).__init__( )51 super(URLValidator, self).__init__(self.URL_PATTERN) 52 52 self.verify_exists = verify_exists 53 53 self.user_agent = validator_user_agent 54 54