Ticket #9857: 9857_urlfield_with_timeout_argument.diff

File 9857_urlfield_with_timeout_argument.diff, 3.9 KB (added by Fabian Topfstedt, 13 years ago)

A patch against rev 16150 (1.4 pre alpha) that allows e.g. models.URLField(verify_exists=True, timeout=5)

  • django/db/models/fields/__init__.py

     
    11261126class URLField(CharField):
    11271127    description = _("URL")
    11281128
    1129     def __init__(self, verbose_name=None, name=None, verify_exists=True, **kwargs):
     1129    def __init__(self, verbose_name=None, name=None, verify_exists=True, timeout=None, **kwargs):
    11301130        kwargs['max_length'] = kwargs.get('max_length', 200)
    11311131        CharField.__init__(self, verbose_name, name, **kwargs)
    1132         self.validators.append(validators.URLValidator(verify_exists=verify_exists))
     1132        self.validators.append(validators.URLValidator(verify_exists=verify_exists, timeout=timeout))
    11331133
    11341134    def formfield(self, **kwargs):
    11351135        # As with CharField, this will cause URL validation to be performed twice
  • django/forms/fields.py

     
    570570    }
    571571
    572572    def __init__(self, max_length=None, min_length=None, verify_exists=False,
    573             validator_user_agent=validators.URL_VALIDATOR_USER_AGENT, *args, **kwargs):
    574         super(URLField, self).__init__(max_length, min_length, *args,
    575                                        **kwargs)
    576         self.validators.append(validators.URLValidator(verify_exists=verify_exists, validator_user_agent=validator_user_agent))
     573            validator_user_agent=validators.URL_VALIDATOR_USER_AGENT, timeout=None, *args, **kwargs):
     574        super(URLField, self).__init__(max_length, min_length, *args, **kwargs)
     575        self.validators.append(validators.URLValidator(verify_exists=verify_exists, validator_user_agent=validator_user_agent, timeout=timeout))
    577576
    578577    def to_python(self, value):
    579578        if value:
  • django/core/validators.py

     
     1import inspect
    12import re
     3import socket
    24import urllib2
    35import urlparse
    46
     
    5254        r'(?::\d+)?' # optional port
    5355        r'(?:/?|[/?]\S+)$', re.IGNORECASE)
    5456
    55     def __init__(self, verify_exists=False, validator_user_agent=URL_VALIDATOR_USER_AGENT):
     57    def __init__(self, verify_exists=False, validator_user_agent=URL_VALIDATOR_USER_AGENT, timeout=None):
    5658        super(URLValidator, self).__init__()
    5759        self.verify_exists = verify_exists
    5860        self.user_agent = validator_user_agent
     61        self.timeout = timeout
    5962
    6063    def __call__(self, value):
    6164        try:
     
    8992                _(u'This URL appears to be a broken link.'), code='invalid_link')
    9093            try:
    9194                req = HeadRequest(url, None, headers)
    92                 u = urllib2.urlopen(req)
     95                if self.timeout is not None and \
     96                    'timeout' in inspect.getargspec(urllib2.urlopen).args:
     97                    u = urllib2.urlopen(req, timeout=self.timeout)
     98                else:
     99                    u = urllib2.urlopen(req)
    93100            except ValueError:
    94101                raise ValidationError(_(u'Enter a valid URL.'), code='invalid')
    95102            except urllib2.HTTPError, e:
     
    103110                        raise broken_error
    104111                else:
    105112                    raise broken_error
    106             except: # urllib2.URLError, httplib.InvalidURL, etc.
     113            except urllib2.URLError, exc:
     114                if isinstance(getattr(exc, 'reason', None), socket.timeout):
     115                    raise ValidationError(
     116                        _(u'Requesting this URL timed out.'),
     117                        code='timed_out')
     118                else:
     119                    raise broken_error
     120            except: # httplib.InvalidURL, etc.
    107121                raise broken_error
    108122
    109123
Back to Top