Ticket #7345: urlfield.diff

File urlfield.diff, 3.8 KB (added by Joel Watts, 16 years ago)

Patch to normalize path to / if none is specified (includes tests).

  • django/newforms/fields.py

    diff --git a/django/newforms/fields.py b/django/newforms/fields.py
    index 08e8b84..57f2341 100644
    a b import datetime  
    77import os
    88import re
    99import time
     10import urlparse
    1011# Python 2.3 fallbacks
    1112try:
    1213    from decimal import Decimal, DecimalException
    class URLField(RegexField):  
    509510        # If no URL scheme given, assume http://
    510511        if value and '://' not in value:
    511512            value = u'http://%s' % value
     513        # If no URL path given, assume /
     514        if value and not urlparse.urlsplit(value).path:
     515            value += '/'
    512516        value = super(URLField, self).clean(value)
    513517        if value == u'':
    514518            return value
  • tests/regressiontests/forms/fields.py

    diff --git a/tests/regressiontests/forms/fields.py b/tests/regressiontests/forms/fields.py
    index f3b6a96..c4e732e 100644
    a b Traceback (most recent call last):  
    816816...
    817817ValidationError: [u'This field is required.']
    818818>>> f.clean('http://localhost')
    819 u'http://localhost'
     819u'http://localhost/'
    820820>>> f.clean('http://example.com')
    821 u'http://example.com'
     821u'http://example.com/'
    822822>>> f.clean('http://www.example.com')
    823 u'http://www.example.com'
     823u'http://www.example.com/'
    824824>>> f.clean('http://www.example.com:8000/test')
    825825u'http://www.example.com:8000/test'
    826826>>> f.clean('http://200.8.9.10')
    827 u'http://200.8.9.10'
     827u'http://200.8.9.10/'
    828828>>> f.clean('http://200.8.9.10:8000/test')
    829829u'http://200.8.9.10:8000/test'
    830830>>> f.clean('foo')
    u''  
    854854>>> f.clean(None)
    855855u''
    856856>>> f.clean('http://example.com')
    857 u'http://example.com'
     857u'http://example.com/'
    858858>>> f.clean('http://www.example.com')
    859 u'http://www.example.com'
     859u'http://www.example.com/'
    860860>>> f.clean('foo')
    861861Traceback (most recent call last):
    862862...
    URLField takes an optional verify_exists parameter, which is False by default.  
    882882This verifies that the URL is live on the Internet and doesn't return a 404 or 500:
    883883>>> f = URLField(verify_exists=True)
    884884>>> f.clean('http://www.google.com') # This will fail if there's no Internet connection
    885 u'http://www.google.com'
     885u'http://www.google.com/'
    886886>>> f.clean('http://example')
    887887Traceback (most recent call last):
    888888...
    ValidationError: [u'This URL appears to be a broken link.']  
    899899>>> f.clean('')
    900900u''
    901901>>> f.clean('http://www.google.com') # This will fail if there's no Internet connection
    902 u'http://www.google.com'
     902u'http://www.google.com/'
    903903
    904904URLField also access min_length and max_length parameters, for convenience.
    905905>>> f = URLField(min_length=15, max_length=20)
    906906>>> f.clean('http://f.com')
    907907Traceback (most recent call last):
    908908...
    909 ValidationError: [u'Ensure this value has at least 15 characters (it has 12).']
     909ValidationError: [u'Ensure this value has at least 15 characters (it has 13).']
    910910>>> f.clean('http://example.com')
    911 u'http://example.com'
     911u'http://example.com/'
    912912>>> f.clean('http://abcdefghijklmnopqrstuvwxyz.com')
    913913Traceback (most recent call last):
    914914...
    915 ValidationError: [u'Ensure this value has at most 20 characters (it has 37).']
     915ValidationError: [u'Ensure this value has at most 20 characters (it has 38).']
    916916
    917917URLField should prepend 'http://' if no scheme was given
    918918>>> f = URLField(required=False)
    919919>>> f.clean('example.com')
    920 u'http://example.com'
     920u'http://example.com/'
    921921>>> f.clean('')
    922922u''
    923923>>> f.clean('https://example.com')
    924 u'https://example.com'
     924u'https://example.com/'
     925
     926URLField should append '/' if no path was given
     927>>> f = URLField()
     928>>> f.clean('http://example.com')
     929u'http://example.com/'
     930
     931URLField shouldn't change the path if it was given
     932>>> f.clean('http://example.com/test')
     933u'http://example.com/test'
    925934
    926935# BooleanField ################################################################
    927936
Back to Top