Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fix #17751: Added stripping of whitespace for IPAddressField/GenericI…

…PAddressField
  • Loading branch information...
commit 21f333bcefccc151d6439246f8203d609ab6ca79 1 parent c12891e
Erik Romijn authored February 23, 2013
2  django/core/validators.py
@@ -129,7 +129,7 @@ def __call__(self, value):
129 129
 slug_re = re.compile(r'^[-a-zA-Z0-9_]+$')
130 130
 validate_slug = RegexValidator(slug_re, _("Enter a valid 'slug' consisting of letters, numbers, underscores or hyphens."), 'invalid')
131 131
 
132  
-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}$')
  132
+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*$')
133 133
 validate_ipv4_address = RegexValidator(ipv4_re, _('Enter a valid IPv4 address.'), 'invalid')
134 134
 
135 135
 
8  django/forms/fields.py
@@ -1096,6 +1096,10 @@ class IPAddressField(CharField):
1096 1096
     }
1097 1097
     default_validators = [validators.validate_ipv4_address]
1098 1098
 
  1099
+    def clean(self, value):
  1100
+        value = self.to_python(value).strip()
  1101
+        return super(IPAddressField, self).clean(value)
  1102
+
1099 1103
 
1100 1104
 class GenericIPAddressField(CharField):
1101 1105
     default_error_messages = {}
@@ -1107,6 +1111,10 @@ def __init__(self, protocol='both', unpack_ipv4=False, *args, **kwargs):
1107 1111
         self.default_error_messages['invalid'] = invalid_error_message
1108 1112
         super(GenericIPAddressField, self).__init__(*args, **kwargs)
1109 1113
 
  1114
+    def clean(self, value):
  1115
+        value = self.to_python(value).strip()
  1116
+        return super(GenericIPAddressField, self).clean(value)
  1117
+
1110 1118
     def to_python(self, value):
1111 1119
         if value in validators.EMPTY_VALUES:
1112 1120
             return ''
18  tests/modeltests/validation/tests.py
@@ -116,40 +116,54 @@ class GenericIPAddressFieldTests(ValidationTestCase):
116 116
     def test_correct_generic_ip_passes(self):
117 117
         giptm = GenericIPAddressTestModel(generic_ip="1.2.3.4")
118 118
         self.assertEqual(None, giptm.full_clean())
  119
+        giptm = GenericIPAddressTestModel(generic_ip=" 1.2.3.4 ")
  120
+        self.assertEqual(None, giptm.full_clean())
119 121
         giptm = GenericIPAddressTestModel(generic_ip="2001::2")
120 122
         self.assertEqual(None, giptm.full_clean())
  123
+        giptm = GenericIPAddressTestModel(generic_ip=" 2001::2 ")
  124
+        self.assertEqual(None, giptm.full_clean())
121 125
 
122 126
     def test_invalid_generic_ip_raises_error(self):
123 127
         giptm = GenericIPAddressTestModel(generic_ip="294.4.2.1")
124 128
         self.assertFailsValidation(giptm.full_clean, ['generic_ip',])
  129
+        giptm = GenericIPAddressTestModel(generic_ip="1.2.3 .4")
  130
+        self.assertFailsValidation(giptm.full_clean, ['generic_ip',])
125 131
         giptm = GenericIPAddressTestModel(generic_ip="1:2")
126 132
         self.assertFailsValidation(giptm.full_clean, ['generic_ip',])
127 133
 
128 134
     def test_correct_v4_ip_passes(self):
129 135
         giptm = GenericIPAddressTestModel(v4_ip="1.2.3.4")
130 136
         self.assertEqual(None, giptm.full_clean())
  137
+        giptm = GenericIPAddressTestModel(v4_ip=" 1.2.3.4 ")
  138
+        self.assertEqual(None, giptm.full_clean())
131 139
 
132 140
     def test_invalid_v4_ip_raises_error(self):
133 141
         giptm = GenericIPAddressTestModel(v4_ip="294.4.2.1")
134 142
         self.assertFailsValidation(giptm.full_clean, ['v4_ip',])
  143
+        giptm = GenericIPAddressTestModel(v4_ip="294.4 .2.1")
  144
+        self.assertFailsValidation(giptm.full_clean, ['v4_ip',])
135 145
         giptm = GenericIPAddressTestModel(v4_ip="2001::2")
136 146
         self.assertFailsValidation(giptm.full_clean, ['v4_ip',])
137 147
 
138 148
     def test_correct_v6_ip_passes(self):
139 149
         giptm = GenericIPAddressTestModel(v6_ip="2001::2")
140 150
         self.assertEqual(None, giptm.full_clean())
  151
+        giptm = GenericIPAddressTestModel(v6_ip=" 2001::2 ")
  152
+        self.assertEqual(None, giptm.full_clean())
141 153
 
142 154
     def test_invalid_v6_ip_raises_error(self):
143 155
         giptm = GenericIPAddressTestModel(v6_ip="1.2.3.4")
144 156
         self.assertFailsValidation(giptm.full_clean, ['v6_ip',])
  157
+        giptm = GenericIPAddressTestModel(v6_ip="2001:: 2")
  158
+        self.assertFailsValidation(giptm.full_clean, ['v6_ip',])
145 159
         giptm = GenericIPAddressTestModel(v6_ip="1:2")
146 160
         self.assertFailsValidation(giptm.full_clean, ['v6_ip',])
147 161
 
148 162
     def test_v6_uniqueness_detection(self):
149 163
         # These two addresses are the same with different syntax
150  
-        giptm = GenericIPAddressTestModel(generic_ip="2001::1:0:0:0:0:2")
  164
+        giptm = GenericIPAddressTestModel(generic_ip=" 2001::1:0:0:0:0:2")
151 165
         giptm.save()
152  
-        giptm = GenericIPAddressTestModel(generic_ip="2001:0:1:2")
  166
+        giptm = GenericIPAddressTestModel(generic_ip="2001:0:1:2 ")
153 167
         self.assertFailsValidation(giptm.full_clean, ['generic_ip',])
154 168
 
155 169
     def test_v4_unpack_uniqueness_detection(self):
30  tests/regressiontests/forms/tests/extra.py
@@ -474,7 +474,7 @@ def test_ipaddress(self):
474 474
         f = IPAddressField()
475 475
         self.assertFormErrors(['This field is required.'], f.clean, '')
476 476
         self.assertFormErrors(['This field is required.'], f.clean, None)
477  
-        self.assertEqual(f.clean('127.0.0.1'), '127.0.0.1')
  477
+        self.assertEqual(f.clean(' 127.0.0.1'), '127.0.0.1')
478 478
         self.assertFormErrors(['Enter a valid IPv4 address.'], f.clean, 'foo')
479 479
         self.assertFormErrors(['Enter a valid IPv4 address.'], f.clean, '127.0.0.')
480 480
         self.assertFormErrors(['Enter a valid IPv4 address.'], f.clean, '1.2.3.4.5')
@@ -483,7 +483,7 @@ def test_ipaddress(self):
483 483
         f = IPAddressField(required=False)
484 484
         self.assertEqual(f.clean(''), '')
485 485
         self.assertEqual(f.clean(None), '')
486  
-        self.assertEqual(f.clean('127.0.0.1'), '127.0.0.1')
  486
+        self.assertEqual(f.clean(' 127.0.0.1'), '127.0.0.1')
487 487
         self.assertFormErrors(['Enter a valid IPv4 address.'], f.clean, 'foo')
488 488
         self.assertFormErrors(['Enter a valid IPv4 address.'], f.clean, '127.0.0.')
489 489
         self.assertFormErrors(['Enter a valid IPv4 address.'], f.clean, '1.2.3.4.5')
@@ -499,13 +499,13 @@ def test_generic_ipaddress_as_generic(self):
499 499
         f = GenericIPAddressField()
500 500
         self.assertFormErrors(['This field is required.'], f.clean, '')
501 501
         self.assertFormErrors(['This field is required.'], f.clean, None)
502  
-        self.assertEqual(f.clean('127.0.0.1'), '127.0.0.1')
  502
+        self.assertEqual(f.clean(' 127.0.0.1 '), '127.0.0.1')
503 503
         self.assertFormErrors(['Enter a valid IPv4 or IPv6 address.'], f.clean, 'foo')
504 504
         self.assertFormErrors(['Enter a valid IPv4 or IPv6 address.'], f.clean, '127.0.0.')
505 505
         self.assertFormErrors(['Enter a valid IPv4 or IPv6 address.'], f.clean, '1.2.3.4.5')
506 506
         self.assertFormErrors(['Enter a valid IPv4 or IPv6 address.'], f.clean, '256.125.1.5')
507  
-        self.assertEqual(f.clean('fe80::223:6cff:fe8a:2e8a'), 'fe80::223:6cff:fe8a:2e8a')
508  
-        self.assertEqual(f.clean('2a02::223:6cff:fe8a:2e8a'), '2a02::223:6cff:fe8a:2e8a')
  507
+        self.assertEqual(f.clean(' fe80::223:6cff:fe8a:2e8a '), 'fe80::223:6cff:fe8a:2e8a')
  508
+        self.assertEqual(f.clean(' 2a02::223:6cff:fe8a:2e8a '), '2a02::223:6cff:fe8a:2e8a')
509 509
         self.assertFormErrors(['Enter a valid IPv4 or IPv6 address.'], f.clean, '12345:2:3:4')
510 510
         self.assertFormErrors(['Enter a valid IPv4 or IPv6 address.'], f.clean, '1::2:3::4')
511 511
         self.assertFormErrors(['Enter a valid IPv4 or IPv6 address.'], f.clean, 'foo::223:6cff:fe8a:2e8a')
@@ -516,7 +516,7 @@ def test_generic_ipaddress_as_ipv4_only(self):
516 516
         f = GenericIPAddressField(protocol="IPv4")
517 517
         self.assertFormErrors(['This field is required.'], f.clean, '')
518 518
         self.assertFormErrors(['This field is required.'], f.clean, None)
519  
-        self.assertEqual(f.clean('127.0.0.1'), '127.0.0.1')
  519
+        self.assertEqual(f.clean(' 127.0.0.1 '), '127.0.0.1')
520 520
         self.assertFormErrors(['Enter a valid IPv4 address.'], f.clean, 'foo')
521 521
         self.assertFormErrors(['Enter a valid IPv4 address.'], f.clean, '127.0.0.')
522 522
         self.assertFormErrors(['Enter a valid IPv4 address.'], f.clean, '1.2.3.4.5')
@@ -533,8 +533,8 @@ def test_generic_ipaddress_as_ipv6_only(self):
533 533
         self.assertFormErrors(['Enter a valid IPv6 address.'], f.clean, '127.0.0.')
534 534
         self.assertFormErrors(['Enter a valid IPv6 address.'], f.clean, '1.2.3.4.5')
535 535
         self.assertFormErrors(['Enter a valid IPv6 address.'], f.clean, '256.125.1.5')
536  
-        self.assertEqual(f.clean('fe80::223:6cff:fe8a:2e8a'), 'fe80::223:6cff:fe8a:2e8a')
537  
-        self.assertEqual(f.clean('2a02::223:6cff:fe8a:2e8a'), '2a02::223:6cff:fe8a:2e8a')
  536
+        self.assertEqual(f.clean(' fe80::223:6cff:fe8a:2e8a '), 'fe80::223:6cff:fe8a:2e8a')
  537
+        self.assertEqual(f.clean(' 2a02::223:6cff:fe8a:2e8a '), '2a02::223:6cff:fe8a:2e8a')
538 538
         self.assertFormErrors(['Enter a valid IPv6 address.'], f.clean, '12345:2:3:4')
539 539
         self.assertFormErrors(['Enter a valid IPv6 address.'], f.clean, '1::2:3::4')
540 540
         self.assertFormErrors(['Enter a valid IPv6 address.'], f.clean, 'foo::223:6cff:fe8a:2e8a')
@@ -550,8 +550,8 @@ def test_generic_ipaddress_as_generic_not_required(self):
550 550
         self.assertFormErrors(['Enter a valid IPv4 or IPv6 address.'], f.clean, '127.0.0.')
551 551
         self.assertFormErrors(['Enter a valid IPv4 or IPv6 address.'], f.clean, '1.2.3.4.5')
552 552
         self.assertFormErrors(['Enter a valid IPv4 or IPv6 address.'], f.clean, '256.125.1.5')
553  
-        self.assertEqual(f.clean('fe80::223:6cff:fe8a:2e8a'), 'fe80::223:6cff:fe8a:2e8a')
554  
-        self.assertEqual(f.clean('2a02::223:6cff:fe8a:2e8a'), '2a02::223:6cff:fe8a:2e8a')
  553
+        self.assertEqual(f.clean(' fe80::223:6cff:fe8a:2e8a '), 'fe80::223:6cff:fe8a:2e8a')
  554
+        self.assertEqual(f.clean(' 2a02::223:6cff:fe8a:2e8a '), '2a02::223:6cff:fe8a:2e8a')
555 555
         self.assertFormErrors(['Enter a valid IPv4 or IPv6 address.'], f.clean, '12345:2:3:4')
556 556
         self.assertFormErrors(['Enter a valid IPv4 or IPv6 address.'], f.clean, '1::2:3::4')
557 557
         self.assertFormErrors(['Enter a valid IPv4 or IPv6 address.'], f.clean, 'foo::223:6cff:fe8a:2e8a')
@@ -561,13 +561,13 @@ def test_generic_ipaddress_as_generic_not_required(self):
561 561
     def test_generic_ipaddress_normalization(self):
562 562
         # Test the normalising code
563 563
         f = GenericIPAddressField()
564  
-        self.assertEqual(f.clean('::ffff:0a0a:0a0a'), '::ffff:10.10.10.10')
565  
-        self.assertEqual(f.clean('::ffff:10.10.10.10'), '::ffff:10.10.10.10')
566  
-        self.assertEqual(f.clean('2001:000:a:0000:0:fe:fe:beef'), '2001:0:a::fe:fe:beef')
567  
-        self.assertEqual(f.clean('2001::a:0000:0:fe:fe:beef'), '2001:0:a::fe:fe:beef')
  564
+        self.assertEqual(f.clean(' ::ffff:0a0a:0a0a  '), '::ffff:10.10.10.10')
  565
+        self.assertEqual(f.clean(' ::ffff:10.10.10.10  '), '::ffff:10.10.10.10')
  566
+        self.assertEqual(f.clean(' 2001:000:a:0000:0:fe:fe:beef  '), '2001:0:a::fe:fe:beef')
  567
+        self.assertEqual(f.clean(' 2001::a:0000:0:fe:fe:beef  '), '2001:0:a::fe:fe:beef')
568 568
 
569 569
         f = GenericIPAddressField(unpack_ipv4=True)
570  
-        self.assertEqual(f.clean('::ffff:0a0a:0a0a'), '10.10.10.10')
  570
+        self.assertEqual(f.clean(' ::ffff:0a0a:0a0a'), '10.10.10.10')
571 571
 
572 572
     def test_smart_text(self):
573 573
         class Test:

0 notes on commit 21f333b

Please sign in to comment.
Something went wrong with that request. Please try again.