Switched URLRedirectValidator and URLLinkValidator to subclass directly from RegexVal...
authormelinath <stephen.r.burrows@gmail.com>
Tue, 22 Jun 2010 02:01:25 +0000 (22:01 -0400)
committermelinath <stephen.r.burrows@gmail.com>
Tue, 22 Jun 2010 02:01:25 +0000 (22:01 -0400)
--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.

models.py
validators.py

index b66c4c3..c17cd70 100644 (file)
--- 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):
index 68ba293..1e73c0b 100644 (file)
@@ -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')