From: melinath Date: Tue, 22 Jun 2010 02:01:25 +0000 (-0400) Subject: Switched URLRedirectValidator and URLLinkValidator to subclass directly from RegexVal... X-Git-Tag: philo-0.9~76^2~1^2 X-Git-Url: http://git.ithinksw.org/philo.git/commitdiff_plain/7515976d9cc927f6f72a191442c5788856f610fd?hp=d8c7acf5c1153bb75c51b632ee6bf2d913448a3a Switched URLRedirectValidator and URLLinkValidator to subclass directly from RegexValidator. --Avoids having to implement verify_exists for a link on a node that, say, might not even be on a site per se. If that's something I want later, I can look at it then... for now, this will fulfill its function just fine. --- diff --git a/models.py b/models.py index b66c4c3..c17cd70 100644 --- a/models.py +++ b/models.py @@ -19,7 +19,7 @@ from django.template.loader import get_template from django.http import Http404, HttpResponse, HttpResponseServerError, HttpResponseRedirect from django.core.servers.basehttp import FileWrapper from django.conf import settings -from philo.validators import URLRedirectValidator +from philo.validators import RedirectValidator def register_value_model(model): @@ -276,7 +276,7 @@ class Redirect(Node): (302, 'Temporary'), (301, 'Permanent'), ) - target = models.CharField(max_length=200,validators=[URLRedirectValidator()]) + target = models.CharField(max_length=200,validators=[RedirectValidator()]) status_code = models.IntegerField(choices=STATUS_CODES, default=302, verbose_name='redirect type') def render_to_response(self, request, path=None, subpath=None): diff --git a/validators.py b/validators.py index 68ba293..1e73c0b 100644 --- a/validators.py +++ b/validators.py @@ -1,6 +1,6 @@ from django.core.exceptions import ValidationError from django.utils.translation import ugettext_lazy as _ -from django.core.validators import URLValidator +from django.core.validators import RegexValidator import re @@ -80,23 +80,28 @@ class TreePositionValidator(object): message = property(get_message) -class URLRedirectValidator(URLValidator): +class RedirectValidator(RegexValidator): + """Based loosely on the URLValidator, but no option to verify_exists""" regex = re.compile( - r'^(?:https?://' # http:// or https:// - r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?|' #domain... - r'localhost|' #localhost... - r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip - r'(?::\d+)?' # optional port - r'|)' # also allow internal redirects - r'(?:/?|[/?]?\S+)$', re.IGNORECASE) + r'^(?:https?://' # http:// or https:// + r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?|' #domain... + r'localhost|' #localhost... + r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip + r'(?::\d+)?' # optional port + r'(?:/?|[/?#]?\S+)|' + r'[^?#\s]\S*)$', + re.IGNORECASE) + message = _(u'Enter a valid absolute or relative redirect target') -class URLLinkValidator(URLValidator): +class URLLinkValidator(RegexValidator): + """Based loosely on the URLValidator, but no option to verify_exists""" regex = re.compile( - r'^(?:https?://' # http:// or https:// - r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?|' #domain... - r'localhost|' #localhost... - r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip - r'(?::\d+)?' # optional port - r'|)' # also allow internal links - r'(?:/?|[/?#]?\S+)$', re.IGNORECASE) + r'^(?:https?://' # http:// or https:// + r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?|' #domain... + r'localhost|' #localhost... + r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip + r'(?::\d+)?' # optional port + r'|)' # also allow internal links + r'(?:/?|[/?#]?\S+)$', re.IGNORECASE) + message = _(u'Enter a valid absolute or relative redirect target')