Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #4067 -- Fixed validation of IPAddressFields in newforms. Thank…

…s to neils and the team in the Copenhagen sprint group.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@6357 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit c012b8964e596423b68f4ec4b1b57253cf0ae7b0 1 parent c694587
Malcolm Tredinnick authored September 16, 2007
5  django/db/models/fields/__init__.py
@@ -880,6 +880,11 @@ def get_manipulator_field_objs(self):
880 880
     def validate(self, field_data, all_data):
881 881
         validators.isValidIPAddress4(field_data, None)
882 882
 
  883
+    def formfield(self, **kwargs):
  884
+        defaults = {'form_class': forms.IPAddressField}
  885
+        defaults.update(kwargs)
  886
+        return super(IPAddressField, self).formfield(**defaults)
  887
+
883 888
 class NullBooleanField(Field):
884 889
     empty_strings_allowed = False
885 890
     def __init__(self, *args, **kwargs):
10  django/newforms/fields.py
@@ -26,7 +26,7 @@
26 26
     'RegexField', 'EmailField', 'FileField', 'ImageField', 'URLField', 'BooleanField',
27 27
     'ChoiceField', 'NullBooleanField', 'MultipleChoiceField',
28 28
     'ComboField', 'MultiValueField', 'FloatField', 'DecimalField',
29  
-    'SplitDateTimeField',
  29
+    'SplitDateTimeField', 'IPAddressField',
30 30
 )
31 31
 
32 32
 # These values, if given to to_python(), will trigger the self.required check.
@@ -635,3 +635,11 @@ def compress(self, data_list):
635 635
                 raise ValidationError(ugettext(u'Enter a valid time.'))
636 636
             return datetime.datetime.combine(*data_list)
637 637
         return None
  638
+
  639
+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}$')
  640
+
  641
+class IPAddressField(RegexField):
  642
+    def __init__(self, *args, **kwargs):
  643
+        RegexField.__init__(self, ipv4_re,
  644
+                            error_message=ugettext(u'Enter a valid IPv4 address.'),
  645
+                            *args, **kwargs)
11  docs/newforms.txt
@@ -1284,6 +1284,15 @@ When you use a ``FileField`` on a form, you must also remember to
1284 1284
 Takes two optional arguments for validation, ``max_value`` and ``min_value``.
1285 1285
 These control the range of values permitted in the field.
1286 1286
 
  1287
+``IPAddressField``
  1288
+~~~~~~~~~~~~~~~~~~
  1289
+
  1290
+    * Default widget: ``TextInput``
  1291
+    * Empty value: ``''`` (an empty string)
  1292
+    * Normalizes to: A Unicode object.
  1293
+    * Validates that the given value is a valid IPv4 address, using a regular
  1294
+      expression.
  1295
+
1287 1296
 ``MultipleChoiceField``
1288 1297
 ~~~~~~~~~~~~~~~~~~~~~~~
1289 1298
 
@@ -1710,7 +1719,7 @@ the full list of conversions:
1710 1719
     ``ForeignKey``                   ``ModelChoiceField`` (see below)
1711 1720
     ``ImageField``                   ``ImageField``
1712 1721
     ``IntegerField``                 ``IntegerField``
1713  
-    ``IPAddressField``               ``CharField``
  1722
+    ``IPAddressField``               ``IPAddressField``
1714 1723
     ``ManyToManyField``              ``ModelMultipleChoiceField`` (see
1715 1724
                                      below)
1716 1725
     ``NullBooleanField``             ``CharField``
55  tests/regressiontests/forms/tests.py
@@ -3834,6 +3834,61 @@
3834 3834
 >>> f.cleaned_data
3835 3835
 {'field1': u'some text,JP,2007-04-25 06:24:00'}
3836 3836
 
  3837
+
  3838
+# IPAddressField ##################################################################
  3839
+
  3840
+>>> f = IPAddressField()
  3841
+>>> f.clean('')
  3842
+Traceback (most recent call last):
  3843
+...
  3844
+ValidationError: [u'This field is required.']
  3845
+>>> f.clean(None)
  3846
+Traceback (most recent call last):
  3847
+...
  3848
+ValidationError: [u'This field is required.']
  3849
+>>> f.clean('127.0.0.1')
  3850
+u'127.0.0.1'
  3851
+>>> f.clean('foo')
  3852
+Traceback (most recent call last):
  3853
+...
  3854
+ValidationError: [u'Enter a valid IPv4 address.']
  3855
+>>> f.clean('127.0.0.')
  3856
+Traceback (most recent call last):
  3857
+...
  3858
+ValidationError: [u'Enter a valid IPv4 address.']
  3859
+>>> f.clean('1.2.3.4.5')
  3860
+Traceback (most recent call last):
  3861
+...
  3862
+ValidationError: [u'Enter a valid IPv4 address.']
  3863
+>>> f.clean('256.125.1.5')
  3864
+Traceback (most recent call last):
  3865
+...
  3866
+ValidationError: [u'Enter a valid IPv4 address.']
  3867
+
  3868
+>>> f = IPAddressField(required=False)
  3869
+>>> f.clean('')
  3870
+u''
  3871
+>>> f.clean(None)
  3872
+u''
  3873
+>>> f.clean('127.0.0.1')
  3874
+u'127.0.0.1'
  3875
+>>> f.clean('foo')
  3876
+Traceback (most recent call last):
  3877
+...
  3878
+ValidationError: [u'Enter a valid IPv4 address.']
  3879
+>>> f.clean('127.0.0.')
  3880
+Traceback (most recent call last):
  3881
+...
  3882
+ValidationError: [u'Enter a valid IPv4 address.']
  3883
+>>> f.clean('1.2.3.4.5')
  3884
+Traceback (most recent call last):
  3885
+...
  3886
+ValidationError: [u'Enter a valid IPv4 address.']
  3887
+>>> f.clean('256.125.1.5')
  3888
+Traceback (most recent call last):
  3889
+...
  3890
+ValidationError: [u'Enter a valid IPv4 address.']
  3891
+
3837 3892
 #################################
3838 3893
 # Tests of underlying functions #
3839 3894
 #################################

0 notes on commit c012b89

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