Fixed errors introduced in 21f333bcef
. Refs #17751
This commit is contained in:
parent
cc53d9b30b
commit
5ae0c933a8
|
@ -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'^\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*$')
|
||||
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}$')
|
||||
validate_ipv4_address = RegexValidator(ipv4_re, _('Enter a valid IPv4 address.'), 'invalid')
|
||||
|
||||
|
||||
|
|
|
@ -1096,9 +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)
|
||||
def to_python(self, value):
|
||||
if value in EMPTY_VALUES:
|
||||
return ''
|
||||
return value.strip()
|
||||
|
||||
|
||||
class GenericIPAddressField(CharField):
|
||||
|
@ -1111,16 +1112,13 @@ 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 ''
|
||||
value = value.strip()
|
||||
if value and ':' in value:
|
||||
return clean_ipv6_address(value,
|
||||
self.unpack_ipv4, self.error_messages['invalid'])
|
||||
return clean_ipv6_address(value,
|
||||
self.unpack_ipv4, self.error_messages['invalid'])
|
||||
return value
|
||||
|
||||
|
||||
|
|
|
@ -116,54 +116,40 @@ 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")
|
||||
self.assertFailsValidation(giptm.full_clean, ['v4_ip',])
|
||||
|
||||
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',])
|
||||
|
||||
def test_v6_uniqueness_detection(self):
|
||||
# These two addresses are the same with different syntax
|
||||
giptm = GenericIPAddressTestModel(generic_ip=" 2001::1:0:0:0:0:2")
|
||||
giptm = GenericIPAddressTestModel(generic_ip="2001::1:0:0:0:0:2")
|
||||
giptm.save()
|
||||
giptm = GenericIPAddressTestModel(generic_ip="2001:0:1:2 ")
|
||||
giptm = GenericIPAddressTestModel(generic_ip="2001:0:1:2")
|
||||
self.assertFailsValidation(giptm.full_clean, ['generic_ip',])
|
||||
|
||||
def test_v4_unpack_uniqueness_detection(self):
|
||||
|
|
Loading…
Reference in New Issue