Author: Honza_Kral
Date: 2009-09-11 17:12:30 -0500 (Fri, 11 Sep 2009)
New Revision: 11525

Modified:
   django/branches/soc2009/model-validation/django/core/validators.py
   django/branches/soc2009/model-validation/django/forms/fields.py
   django/branches/soc2009/model-validation/tests/modeltests/validators/tests.py
   
django/branches/soc2009/model-validation/tests/regressiontests/forms/fields.py
Log:
[soc2009/model-validation] Fixed #11826 django.forms.fields.URLField rejects 
valid URLs with no abs_path component

Thanks wam

Modified: django/branches/soc2009/model-validation/django/core/validators.py
===================================================================
--- django/branches/soc2009/model-validation/django/core/validators.py  
2009-09-11 22:00:24 UTC (rev 11524)
+++ django/branches/soc2009/model-validation/django/core/validators.py  
2009-09-11 22:12:30 UTC (rev 11525)
@@ -44,7 +44,7 @@
         r'localhost|' #localhost...
         r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
         r'(?::\d+)?' # optional port
-        r'(?:/?|/\S+)$', re.IGNORECASE)
+        r'(?:/?|[/?]\S+)$', re.IGNORECASE)
 
     def __init__(self, verify_exists=False, 
validator_user_agent=URL_VALIDATOR_USER_AGENT):
         super(URLValidator, self).__init__()

Modified: django/branches/soc2009/model-validation/django/forms/fields.py
===================================================================
--- django/branches/soc2009/model-validation/django/forms/fields.py     
2009-09-11 22:00:24 UTC (rev 11524)
+++ django/branches/soc2009/model-validation/django/forms/fields.py     
2009-09-11 22:12:30 UTC (rev 11525)
@@ -551,12 +551,15 @@
         
self.validators.append(validators.URLValidator(verify_exists=verify_exists, 
validator_user_agent=validator_user_agent))
 
     def to_python(self, value):
-        # If no URL scheme given, assume http://
-        if value and '://' not in value:
-            value = u'http://%s' % value
-        # If no URL path given, assume /
-        if value and not urlparse.urlsplit(value)[2]:
-            value += '/'
+        if value: 
+            if '://' not in value: 
+                # If no URL scheme given, assume http:// 
+                value = u'http://%s' % value 
+            url_fields = list(urlparse.urlsplit(value)) 
+            if not url_fields[2]: 
+                # the path portion may need to be added before query params 
+                url_fields[2] = '/' 
+                value = urlparse.urlunsplit(url_fields) 
         return super(URLField, self).to_python(value)
 
 class BooleanField(Field):

Modified: 
django/branches/soc2009/model-validation/tests/modeltests/validators/tests.py
===================================================================
--- 
django/branches/soc2009/model-validation/tests/modeltests/validators/tests.py   
    2009-09-11 22:00:24 UTC (rev 11524)
+++ 
django/branches/soc2009/model-validation/tests/modeltests/validators/tests.py   
    2009-09-11 22:12:30 UTC (rev 11525)
@@ -106,6 +106,8 @@
     (URLValidator(), 'http://200.8.9.10/', None),
     (URLValidator(), 'http://200.8.9.10:8000/test', None),
     (URLValidator(), 'http://valid-----hyphens.com/', None),
+    (URLValidator(), 'http://example.com?something=value', None),
+    (URLValidator(), 
'http://example.com/index.php?something=value&another=value2', None),
 
     (URLValidator(), 'foo', ValidationError),
     (URLValidator(), 'http://', ValidationError),

Modified: 
django/branches/soc2009/model-validation/tests/regressiontests/forms/fields.py
===================================================================
--- 
django/branches/soc2009/model-validation/tests/regressiontests/forms/fields.py  
    2009-09-11 22:00:24 UTC (rev 11524)
+++ 
django/branches/soc2009/model-validation/tests/regressiontests/forms/fields.py  
    2009-09-11 22:12:30 UTC (rev 11525)
@@ -496,6 +496,10 @@
         f = URLField()
         self.assertEqual(u'http://example.com/', f.clean('http://example.com'))
         self.assertEqual(u'http://example.com/test', 
f.clean('http://example.com/test'))
+
+    def test_urlfield_ticket11826(self):
+        f = URLField()
+        self.assertEqual(u'http://example.com/?some_param=some_value', 
f.clean('http://example.com?some_param=some_value'))
         
     # BooleanField 
################################################################
 


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to