Fixed #26821 -- Fixed forms.Email/URLField crash on None value.

This commit is contained in:
Priy 2016-07-03 16:41:59 +02:00 committed by Tim Graham
parent d44afd8892
commit a7b5dfd170
4 changed files with 27 additions and 7 deletions

View File

@ -532,9 +532,8 @@ class EmailField(CharField):
widget = EmailInput widget = EmailInput
default_validators = [validators.validate_email] default_validators = [validators.validate_email]
def clean(self, value): def __init__(self, *args, **kwargs):
value = self.to_python(value).strip() super(EmailField, self).__init__(*args, strip=True, **kwargs)
return super(EmailField, self).clean(value)
class FileField(Field): class FileField(Field):
@ -665,6 +664,9 @@ class URLField(CharField):
} }
default_validators = [validators.URLValidator()] default_validators = [validators.URLValidator()]
def __init__(self, *args, **kwargs):
super(URLField, self).__init__(*args, strip=True, **kwargs)
def to_python(self, value): def to_python(self, value):
def split_url(url): def split_url(url):
@ -696,10 +698,6 @@ class URLField(CharField):
value = urlunsplit(url_fields) value = urlunsplit(url_fields)
return value return value
def clean(self, value):
value = self.to_python(value).strip()
return super(URLField, self).clean(value)
class BooleanField(Field): class BooleanField(Field):
widget = CheckboxInput widget = CheckboxInput

View File

@ -334,6 +334,10 @@ Miscellaneous
new :meth:`.AbstractUser.clean` method so that normalization is applied in new :meth:`.AbstractUser.clean` method so that normalization is applied in
cases like model form validation. cases like model form validation.
* ``EmailField`` and ``URLField`` no longer accept the ``strip`` keyword
argument. Remove it because it doesn't have an effect in older versions of
Django as these fields alway strip whitespace.
.. _deprecated-features-1.11: .. _deprecated-features-1.11:
Features deprecated in 1.11 Features deprecated in 1.11

View File

@ -51,3 +51,12 @@ class EmailFieldTest(FormFieldAssertionsMixin, SimpleTestCase):
self.assertEqual('alf@foo.com', f.clean('alf@foo.com')) self.assertEqual('alf@foo.com', f.clean('alf@foo.com'))
with self.assertRaisesMessage(ValidationError, "'Ensure this value has at most 15 characters (it has 20).'"): with self.assertRaisesMessage(ValidationError, "'Ensure this value has at most 15 characters (it has 20).'"):
f.clean('alf123456788@foo.com') f.clean('alf123456788@foo.com')
def test_emailfield_strip_on_none_value(self):
f = EmailField(required=False, empty_value=None)
self.assertIsNone(f.clean(None))
def test_emailfield_unable_to_set_strip_kwarg(self):
msg = "__init__() got multiple values for keyword argument 'strip'"
with self.assertRaisesMessage(TypeError, msg):
EmailField(strip=False)

View File

@ -151,3 +151,12 @@ class URLFieldTest(FormFieldAssertionsMixin, SimpleTestCase):
def test_urlfield_normalization(self): def test_urlfield_normalization(self):
f = URLField() f = URLField()
self.assertEqual(f.clean('http://example.com/ '), 'http://example.com/') self.assertEqual(f.clean('http://example.com/ '), 'http://example.com/')
def test_urlfield_strip_on_none_value(self):
f = URLField(required=False, empty_value=None)
self.assertIsNone(f.clean(None))
def test_urlfield_unable_to_set_strip_kwarg(self):
msg = "__init__() got multiple values for keyword argument 'strip'"
with self.assertRaisesMessage(TypeError, msg):
URLField(strip=False)