Django

Code

Changeset 6357

Show
Ignore:
Timestamp:
09/16/07 06:38:32 (10 months ago)
Author:
mtredinnick
Message:

Fixed #4067 -- Fixed validation of IPAddressFields in newforms. Thanks to neils and the team in the Copenhagen sprint group.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/db/models/fields/__init__.py

    r6252 r6357  
    881881        validators.isValidIPAddress4(field_data, None) 
    882882 
     883    def formfield(self, **kwargs): 
     884        defaults = {'form_class': forms.IPAddressField} 
     885        defaults.update(kwargs) 
     886        return super(IPAddressField, self).formfield(**defaults) 
     887 
    883888class NullBooleanField(Field): 
    884889    empty_strings_allowed = False 
  • django/trunk/django/newforms/fields.py

    r6285 r6357  
    2727    'ChoiceField', 'NullBooleanField', 'MultipleChoiceField', 
    2828    'ComboField', 'MultiValueField', 'FloatField', 'DecimalField', 
    29     'SplitDateTimeField', 
     29    'SplitDateTimeField', 'IPAddressField', 
    3030) 
    3131 
     
    636636            return datetime.datetime.combine(*data_list) 
    637637        return None 
     638 
     639ipv4_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 
     641class 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) 
  • django/trunk/docs/newforms.txt

    r6352 r6357  
    12851285These control the range of values permitted in the field. 
    12861286 
     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 
    12871296``MultipleChoiceField`` 
    12881297~~~~~~~~~~~~~~~~~~~~~~~ 
     
    17111720    ``ImageField``                   ``ImageField`` 
    17121721    ``IntegerField``                 ``IntegerField`` 
    1713     ``IPAddressField``               ``CharField`` 
     1722    ``IPAddressField``               ``IPAddressField`` 
    17141723    ``ManyToManyField``              ``ModelMultipleChoiceField`` (see 
    17151724                                     below) 
  • django/trunk/tests/regressiontests/forms/tests.py

    r6352 r6357  
    38353835{'field1': u'some text,JP,2007-04-25 06:24:00'} 
    38363836 
     3837 
     3838# IPAddressField ################################################################## 
     3839 
     3840>>> f = IPAddressField() 
     3841>>> f.clean('') 
     3842Traceback (most recent call last): 
     3843... 
     3844ValidationError: [u'This field is required.'] 
     3845>>> f.clean(None) 
     3846Traceback (most recent call last): 
     3847... 
     3848ValidationError: [u'This field is required.'] 
     3849>>> f.clean('127.0.0.1') 
     3850u'127.0.0.1' 
     3851>>> f.clean('foo') 
     3852Traceback (most recent call last): 
     3853... 
     3854ValidationError: [u'Enter a valid IPv4 address.'] 
     3855>>> f.clean('127.0.0.') 
     3856Traceback (most recent call last): 
     3857... 
     3858ValidationError: [u'Enter a valid IPv4 address.'] 
     3859>>> f.clean('1.2.3.4.5') 
     3860Traceback (most recent call last): 
     3861... 
     3862ValidationError: [u'Enter a valid IPv4 address.'] 
     3863>>> f.clean('256.125.1.5') 
     3864Traceback (most recent call last): 
     3865... 
     3866ValidationError: [u'Enter a valid IPv4 address.'] 
     3867 
     3868>>> f = IPAddressField(required=False) 
     3869>>> f.clean('') 
     3870u'' 
     3871>>> f.clean(None) 
     3872u'' 
     3873>>> f.clean('127.0.0.1') 
     3874u'127.0.0.1' 
     3875>>> f.clean('foo') 
     3876Traceback (most recent call last): 
     3877... 
     3878ValidationError: [u'Enter a valid IPv4 address.'] 
     3879>>> f.clean('127.0.0.') 
     3880Traceback (most recent call last): 
     3881... 
     3882ValidationError: [u'Enter a valid IPv4 address.'] 
     3883>>> f.clean('1.2.3.4.5') 
     3884Traceback (most recent call last): 
     3885... 
     3886ValidationError: [u'Enter a valid IPv4 address.'] 
     3887>>> f.clean('256.125.1.5') 
     3888Traceback (most recent call last): 
     3889... 
     3890ValidationError: [u'Enter a valid IPv4 address.'] 
     3891 
    38373892################################# 
    38383893# Tests of underlying functions #