Ticket #9857: 9857_urlfield_with_timeout_argument.diff
File 9857_urlfield_with_timeout_argument.diff, 3.9 KB (added by , 14 years ago) |
---|
-
django/db/models/fields/__init__.py
1126 1126 class URLField(CharField): 1127 1127 description = _("URL") 1128 1128 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): 1130 1130 kwargs['max_length'] = kwargs.get('max_length', 200) 1131 1131 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)) 1133 1133 1134 1134 def formfield(self, **kwargs): 1135 1135 # As with CharField, this will cause URL validation to be performed twice -
django/forms/fields.py
570 570 } 571 571 572 572 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)) 577 576 578 577 def to_python(self, value): 579 578 if value: -
django/core/validators.py
1 import inspect 1 2 import re 3 import socket 2 4 import urllib2 3 5 import urlparse 4 6 … … 52 54 r'(?::\d+)?' # optional port 53 55 r'(?:/?|[/?]\S+)$', re.IGNORECASE) 54 56 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): 56 58 super(URLValidator, self).__init__() 57 59 self.verify_exists = verify_exists 58 60 self.user_agent = validator_user_agent 61 self.timeout = timeout 59 62 60 63 def __call__(self, value): 61 64 try: … … 89 92 _(u'This URL appears to be a broken link.'), code='invalid_link') 90 93 try: 91 94 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) 93 100 except ValueError: 94 101 raise ValidationError(_(u'Enter a valid URL.'), code='invalid') 95 102 except urllib2.HTTPError, e: … … 103 110 raise broken_error 104 111 else: 105 112 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. 107 121 raise broken_error 108 122 109 123