1 from django.utils.translation import ugettext_lazy as _
2 from django.core.validators import RegexValidator
3 from django.core.exceptions import ValidationError
4 from django.utils import simplejson as json
8 class RedirectValidator(RegexValidator):
9 """Based loosely on the URLValidator, but no option to verify_exists"""
11 r'^(?:https?://' # http:// or https://
12 r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?|' #domain...
13 r'localhost|' #localhost...
14 r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
15 r'(?::\d+)?' # optional port
19 message = _(u'Enter a valid absolute or relative redirect target')
22 class URLLinkValidator(RegexValidator):
23 """Based loosely on the URLValidator, but no option to verify_exists"""
25 r'^(?:https?://' # http:// or https://
26 r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?|' #domain...
27 r'localhost|' #localhost...
28 r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
29 r'(?::\d+)?' # optional port
30 r'|)' # also allow internal links
31 r'(?:/?|[/?#]?\S+)$', re.IGNORECASE)
32 message = _(u'Enter a valid absolute or relative redirect target')
35 def json_validator(value):
39 raise ValidationError(u'\'%s\' is not valid JSON' % value)