From 5ae0c933a8572c173401386f9037901e6b5fff2b Mon Sep 17 00:00:00 2001 From: Florian Apolloner Date: Sat, 23 Feb 2013 18:42:53 +0100 Subject: [PATCH] Fixed errors introduced in 21f333bcefccc151d6439246f8203d609ab6ca79. Refs #17751 --- django/core/validators.py | 2 +- django/forms/fields.py | 16 +++++++--------- tests/modeltests/validation/tests.py | 18 ++---------------- 3 files changed, 10 insertions(+), 26 deletions(-) diff --git a/django/core/validators.py b/django/core/validators.py index 322741be39..948ae28673 100644 --- a/django/core/validators.py +++ b/django/core/validators.py @@ -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') diff --git a/django/forms/fields.py b/django/forms/fields.py index aa909a8aa5..277f0b093e 100644 --- a/django/forms/fields.py +++ b/django/forms/fields.py @@ -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 diff --git a/tests/modeltests/validation/tests.py b/tests/modeltests/validation/tests.py index dca99052e5..58b7b94e7f 100644 --- a/tests/modeltests/validation/tests.py +++ b/tests/modeltests/validation/tests.py @@ -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):