Ticket #4067: 4067.2.patch

File 4067.2.patch, 4.4 KB (added by Niels Sandholt Busch, 17 years ago)

Newforms IPAddressField

  • django/db/models/fields/__init__.py

     
    880880    def validate(self, field_data, all_data):
    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
    885890    def __init__(self, *args, **kwargs):
  • django/newforms/fields.py

     
    2626    'RegexField', 'EmailField', 'FileField', 'ImageField', 'URLField', 'BooleanField',
    2727    'ChoiceField', 'NullBooleanField', 'MultipleChoiceField',
    2828    'ComboField', 'MultiValueField', 'FloatField', 'DecimalField',
    29     'SplitDateTimeField',
     29    'SplitDateTimeField', 'IPAddressField',
    3030)
    3131
    3232# These values, if given to to_python(), will trigger the self.required check.
     
    631631                raise ValidationError(ugettext(u'Enter a valid time.'))
    632632            return datetime.datetime.combine(*data_list)
    633633        return None
     634
     635ipv4_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}$')
     636
     637class IPAddressField(RegexField):
     638    def __init__(self, *args, **kwargs):
     639        RegexField.__init__(self, ipv4_re,
     640                            error_message=ugettext(u'Enter a valid IPv4 address.'),
     641                            *args, **kwargs)
  • tests/regressiontests/forms/tests.py

     
    37893789>>> f.cleaned_data
    37903790{'field1': u'some text,JP,2007-04-25 06:24:00'}
    37913791
     3792
     3793# IPAddressField ##################################################################
     3794
     3795>>> f = IPAddressField()
     3796>>> f.clean('')
     3797Traceback (most recent call last):
     3798...
     3799ValidationError: [u'This field is required.']
     3800>>> f.clean(None)
     3801Traceback (most recent call last):
     3802...
     3803ValidationError: [u'This field is required.']
     3804>>> f.clean('127.0.0.1')
     3805u'127.0.0.1'
     3806>>> f.clean('foo')
     3807Traceback (most recent call last):
     3808...
     3809ValidationError: [u'Enter a valid IPv4 address.']
     3810>>> f.clean('127.0.0.')
     3811Traceback (most recent call last):
     3812...
     3813ValidationError: [u'Enter a valid IPv4 address.']
     3814>>> f.clean('256.125.1.5')
     3815Traceback (most recent call last):
     3816...
     3817ValidationError: [u'Enter a valid IPv4 address.']
     3818
     3819>>> f = IPAddressField(required=False)
     3820>>> f.clean('')
     3821u''
     3822>>> f.clean(None)
     3823u''
     3824>>> f.clean('127.0.0.1')
     3825u'127.0.0.1'
     3826>>> f.clean('foo')
     3827Traceback (most recent call last):
     3828...
     3829ValidationError: [u'Enter a valid IPv4 address.']
     3830>>> f.clean('127.0.0.')
     3831Traceback (most recent call last):
     3832...
     3833ValidationError: [u'Enter a valid IPv4 address.']
     3834>>> f.clean('256.125.1.5')
     3835Traceback (most recent call last):
     3836...
     3837ValidationError: [u'Enter a valid IPv4 address.']
     3838
    37923839#################################
    37933840# Tests of underlying functions #
    37943841#################################
  • docs/newforms.txt

     
    12431243Takes two optional arguments for validation, ``max_value`` and ``min_value``.
    12441244These control the range of values permitted in the field.
    12451245
     1246``IPAddressField``
     1247~~~~~~~~~~~~~~~~~~
     1248
     1249    * Default widget: ``TextInput``
     1250    * Empty value: ``''`` (an empty string)
     1251    * Normalizes to: A Unicode object.
     1252    * Validates that the given value is a valid IPv4 address, using a regular
     1253      expression.
     1254
    12461255``MultipleChoiceField``
    12471256~~~~~~~~~~~~~~~~~~~~~~~
    12481257
     
    16691678    ``ForeignKey``                   ``ModelChoiceField`` (see below)
    16701679    ``ImageField``                   ``ImageField``
    16711680    ``IntegerField``                 ``IntegerField``
     1681    ``IPAddressField``               ``IPAddressField``
    16721682    ``IPAddressField``               ``CharField``
    16731683    ``ManyToManyField``              ``ModelMultipleChoiceField`` (see
    16741684                                     below)
Back to Top