Fix #17751: Added stripping of whitespace for IPAddressField/GenericIPAddressField
This commit is contained in:
parent
c12891e8cd
commit
21f333bcef
|
@ -129,7 +129,7 @@ validate_email = EmailValidator()
|
|||
slug_re = re.compile(r'^[-a-zA-Z0-9_]+$')
|
||||
validate_slug = RegexValidator(slug_re, _("Enter a valid 'slug' consisting of letters, numbers, underscores or hyphens."), 'invalid')
|
||||
|
||||
ipv4_re = re.compile(r'^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$')
|
||||
ipv4_re = re.compile(r'^\s*(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}\s*$')
|
||||
validate_ipv4_address = RegexValidator(ipv4_re, _('Enter a valid IPv4 address.'), 'invalid')
|
||||
|
||||
|
||||
|
|
|
@ -1096,6 +1096,10 @@ class IPAddressField(CharField):
|
|||
}
|
||||
default_validators = [validators.validate_ipv4_address]
|
||||
|
||||
def clean(self, value):
|
||||
value = self.to_python(value).strip()
|
||||
return super(IPAddressField, self).clean(value)
|
||||
|
||||
|
||||
class GenericIPAddressField(CharField):
|
||||
default_error_messages = {}
|
||||
|
@ -1107,6 +1111,10 @@ class GenericIPAddressField(CharField):
|
|||
self.default_error_messages['invalid'] = invalid_error_message
|
||||
super(GenericIPAddressField, self).__init__(*args, **kwargs)
|
||||
|
||||
def clean(self, value):
|
||||
value = self.to_python(value).strip()
|
||||
return super(GenericIPAddressField, self).clean(value)
|
||||
|
||||
def to_python(self, value):
|
||||
if value in validators.EMPTY_VALUES:
|
||||
return ''
|
||||
|
|
|
@ -116,20 +116,30 @@ class GenericIPAddressFieldTests(ValidationTestCase):
|
|||
def test_correct_generic_ip_passes(self):
|
||||
giptm = GenericIPAddressTestModel(generic_ip="1.2.3.4")
|
||||
self.assertEqual(None, giptm.full_clean())
|
||||
giptm = GenericIPAddressTestModel(generic_ip=" 1.2.3.4 ")
|
||||
self.assertEqual(None, giptm.full_clean())
|
||||
giptm = GenericIPAddressTestModel(generic_ip="2001::2")
|
||||
self.assertEqual(None, giptm.full_clean())
|
||||
giptm = GenericIPAddressTestModel(generic_ip=" 2001::2 ")
|
||||
self.assertEqual(None, giptm.full_clean())
|
||||
|
||||
def test_invalid_generic_ip_raises_error(self):
|
||||
giptm = GenericIPAddressTestModel(generic_ip="294.4.2.1")
|
||||
self.assertFailsValidation(giptm.full_clean, ['generic_ip',])
|
||||
giptm = GenericIPAddressTestModel(generic_ip="1.2.3 .4")
|
||||
self.assertFailsValidation(giptm.full_clean, ['generic_ip',])
|
||||
giptm = GenericIPAddressTestModel(generic_ip="1:2")
|
||||
self.assertFailsValidation(giptm.full_clean, ['generic_ip',])
|
||||
|
||||
def test_correct_v4_ip_passes(self):
|
||||
giptm = GenericIPAddressTestModel(v4_ip="1.2.3.4")
|
||||
self.assertEqual(None, giptm.full_clean())
|
||||
giptm = GenericIPAddressTestModel(v4_ip=" 1.2.3.4 ")
|
||||
self.assertEqual(None, giptm.full_clean())
|
||||
|
||||
def test_invalid_v4_ip_raises_error(self):
|
||||
giptm = GenericIPAddressTestModel(v4_ip="294.4.2.1")
|
||||
self.assertFailsValidation(giptm.full_clean, ['v4_ip',])
|
||||
giptm = GenericIPAddressTestModel(v4_ip="294.4 .2.1")
|
||||
self.assertFailsValidation(giptm.full_clean, ['v4_ip',])
|
||||
giptm = GenericIPAddressTestModel(v4_ip="2001::2")
|
||||
|
@ -138,10 +148,14 @@ class GenericIPAddressFieldTests(ValidationTestCase):
|
|||
def test_correct_v6_ip_passes(self):
|
||||
giptm = GenericIPAddressTestModel(v6_ip="2001::2")
|
||||
self.assertEqual(None, giptm.full_clean())
|
||||
giptm = GenericIPAddressTestModel(v6_ip=" 2001::2 ")
|
||||
self.assertEqual(None, giptm.full_clean())
|
||||
|
||||
def test_invalid_v6_ip_raises_error(self):
|
||||
giptm = GenericIPAddressTestModel(v6_ip="1.2.3.4")
|
||||
self.assertFailsValidation(giptm.full_clean, ['v6_ip',])
|
||||
giptm = GenericIPAddressTestModel(v6_ip="2001:: 2")
|
||||
self.assertFailsValidation(giptm.full_clean, ['v6_ip',])
|
||||
giptm = GenericIPAddressTestModel(v6_ip="1:2")
|
||||
self.assertFailsValidation(giptm.full_clean, ['v6_ip',])
|
||||
|
||||
|
|
Loading…
Reference in New Issue