Ticket #4067: 4067.3.patch
File 4067.3.patch, 4.6 KB (added by , 17 years ago) |
---|
-
django/db/models/fields/__init__.py
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): -
django/newforms/fields.py
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. … … 631 631 raise ValidationError(ugettext(u'Enter a valid time.')) 632 632 return datetime.datetime.combine(*data_list) 633 633 return None 634 635 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}$') 636 637 class 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
3789 3789 >>> f.cleaned_data 3790 3790 {'field1': u'some text,JP,2007-04-25 06:24:00'} 3791 3791 3792 3793 # IPAddressField ################################################################## 3794 3795 >>> f = IPAddressField() 3796 >>> f.clean('') 3797 Traceback (most recent call last): 3798 ... 3799 ValidationError: [u'This field is required.'] 3800 >>> f.clean(None) 3801 Traceback (most recent call last): 3802 ... 3803 ValidationError: [u'This field is required.'] 3804 >>> f.clean('127.0.0.1') 3805 u'127.0.0.1' 3806 >>> f.clean('foo') 3807 Traceback (most recent call last): 3808 ... 3809 ValidationError: [u'Enter a valid IPv4 address.'] 3810 >>> f.clean('127.0.0.') 3811 Traceback (most recent call last): 3812 ... 3813 ValidationError: [u'Enter a valid IPv4 address.'] 3814 >>> f.clean('1.2.3.4.5') 3815 Traceback (most recent call last): 3816 ... 3817 ValidationError: [u'Enter a valid IPv4 address.'] 3818 >>> f.clean('256.125.1.5') 3819 Traceback (most recent call last): 3820 ... 3821 ValidationError: [u'Enter a valid IPv4 address.'] 3822 3823 >>> f = IPAddressField(required=False) 3824 >>> f.clean('') 3825 u'' 3826 >>> f.clean(None) 3827 u'' 3828 >>> f.clean('127.0.0.1') 3829 u'127.0.0.1' 3830 >>> f.clean('foo') 3831 Traceback (most recent call last): 3832 ... 3833 ValidationError: [u'Enter a valid IPv4 address.'] 3834 >>> f.clean('127.0.0.') 3835 Traceback (most recent call last): 3836 ... 3837 ValidationError: [u'Enter a valid IPv4 address.'] 3838 >>> f.clean('1.2.3.4.5') 3839 Traceback (most recent call last): 3840 ... 3841 ValidationError: [u'Enter a valid IPv4 address.'] 3842 >>> f.clean('256.125.1.5') 3843 Traceback (most recent call last): 3844 ... 3845 ValidationError: [u'Enter a valid IPv4 address.'] 3846 3792 3847 ################################# 3793 3848 # Tests of underlying functions # 3794 3849 ################################# -
docs/newforms.txt
1243 1243 Takes two optional arguments for validation, ``max_value`` and ``min_value``. 1244 1244 These control the range of values permitted in the field. 1245 1245 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 1246 1255 ``MultipleChoiceField`` 1247 1256 ~~~~~~~~~~~~~~~~~~~~~~~ 1248 1257 … … 1669 1678 ``ForeignKey`` ``ModelChoiceField`` (see below) 1670 1679 ``ImageField`` ``ImageField`` 1671 1680 ``IntegerField`` ``IntegerField`` 1681 ``IPAddressField`` ``IPAddressField`` 1672 1682 ``IPAddressField`` ``CharField`` 1673 1683 ``ManyToManyField`` ``ModelMultipleChoiceField`` (see 1674 1684 below)