Author: mtredinnick
Date: 2007-09-16 06:38:32 -0500 (Sun, 16 Sep 2007)
New Revision: 6357
Modified:
django/trunk/django/db/models/fields/__init__.py
django/trunk/django/newforms/fields.py
django/trunk/docs/newforms.txt
django/trunk/tests/regressiontests/forms/tests.py
Log:
Fixed #4067 -- Fixed validation of IPAddressFields in newforms. Thanks to neils
and the team in the Copenhagen sprint group.
Modified: django/trunk/django/db/models/fields/__init__.py
===================================================================
--- django/trunk/django/db/models/fields/__init__.py 2007-09-16 11:27:40 UTC
(rev 6356)
+++ django/trunk/django/db/models/fields/__init__.py 2007-09-16 11:38:32 UTC
(rev 6357)
@@ -880,6 +880,11 @@
def validate(self, field_data, all_data):
validators.isValidIPAddress4(field_data, None)
+ def formfield(self, **kwargs):
+ defaults = {'form_class': forms.IPAddressField}
+ defaults.update(kwargs)
+ return super(IPAddressField, self).formfield(**defaults)
+
class NullBooleanField(Field):
empty_strings_allowed = False
def __init__(self, *args, **kwargs):
Modified: django/trunk/django/newforms/fields.py
===================================================================
--- django/trunk/django/newforms/fields.py 2007-09-16 11:27:40 UTC (rev
6356)
+++ django/trunk/django/newforms/fields.py 2007-09-16 11:38:32 UTC (rev
6357)
@@ -26,7 +26,7 @@
'RegexField', 'EmailField', 'FileField', 'ImageField', 'URLField',
'BooleanField',
'ChoiceField', 'NullBooleanField', 'MultipleChoiceField',
'ComboField', 'MultiValueField', 'FloatField', 'DecimalField',
- 'SplitDateTimeField',
+ 'SplitDateTimeField', 'IPAddressField',
)
# These values, if given to to_python(), will trigger the self.required check.
@@ -635,3 +635,11 @@
raise ValidationError(ugettext(u'Enter a valid time.'))
return datetime.datetime.combine(*data_list)
return None
+
+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}$')
+
+class IPAddressField(RegexField):
+ def __init__(self, *args, **kwargs):
+ RegexField.__init__(self, ipv4_re,
+ error_message=ugettext(u'Enter a valid IPv4
address.'),
+ *args, **kwargs)
Modified: django/trunk/docs/newforms.txt
===================================================================
--- django/trunk/docs/newforms.txt 2007-09-16 11:27:40 UTC (rev 6356)
+++ django/trunk/docs/newforms.txt 2007-09-16 11:38:32 UTC (rev 6357)
@@ -1284,6 +1284,15 @@
Takes two optional arguments for validation, ``max_value`` and ``min_value``.
These control the range of values permitted in the field.
+``IPAddressField``
+~~~~~~~~~~~~~~~~~~
+
+ * Default widget: ``TextInput``
+ * Empty value: ``''`` (an empty string)
+ * Normalizes to: A Unicode object.
+ * Validates that the given value is a valid IPv4 address, using a regular
+ expression.
+
``MultipleChoiceField``
~~~~~~~~~~~~~~~~~~~~~~~
@@ -1710,7 +1719,7 @@
``ForeignKey`` ``ModelChoiceField`` (see below)
``ImageField`` ``ImageField``
``IntegerField`` ``IntegerField``
- ``IPAddressField`` ``CharField``
+ ``IPAddressField`` ``IPAddressField``
``ManyToManyField`` ``ModelMultipleChoiceField`` (see
below)
``NullBooleanField`` ``CharField``
Modified: django/trunk/tests/regressiontests/forms/tests.py
===================================================================
--- django/trunk/tests/regressiontests/forms/tests.py 2007-09-16 11:27:40 UTC
(rev 6356)
+++ django/trunk/tests/regressiontests/forms/tests.py 2007-09-16 11:38:32 UTC
(rev 6357)
@@ -3834,6 +3834,61 @@
>>> f.cleaned_data
{'field1': u'some text,JP,2007-04-25 06:24:00'}
+
+# IPAddressField
##################################################################
+
+>>> f = IPAddressField()
+>>> f.clean('')
+Traceback (most recent call last):
+...
+ValidationError: [u'This field is required.']
+>>> f.clean(None)
+Traceback (most recent call last):
+...
+ValidationError: [u'This field is required.']
+>>> f.clean('127.0.0.1')
+u'127.0.0.1'
+>>> f.clean('foo')
+Traceback (most recent call last):
+...
+ValidationError: [u'Enter a valid IPv4 address.']
+>>> f.clean('127.0.0.')
+Traceback (most recent call last):
+...
+ValidationError: [u'Enter a valid IPv4 address.']
+>>> f.clean('1.2.3.4.5')
+Traceback (most recent call last):
+...
+ValidationError: [u'Enter a valid IPv4 address.']
+>>> f.clean('256.125.1.5')
+Traceback (most recent call last):
+...
+ValidationError: [u'Enter a valid IPv4 address.']
+
+>>> f = IPAddressField(required=False)
+>>> f.clean('')
+u''
+>>> f.clean(None)
+u''
+>>> f.clean('127.0.0.1')
+u'127.0.0.1'
+>>> f.clean('foo')
+Traceback (most recent call last):
+...
+ValidationError: [u'Enter a valid IPv4 address.']
+>>> f.clean('127.0.0.')
+Traceback (most recent call last):
+...
+ValidationError: [u'Enter a valid IPv4 address.']
+>>> f.clean('1.2.3.4.5')
+Traceback (most recent call last):
+...
+ValidationError: [u'Enter a valid IPv4 address.']
+>>> f.clean('256.125.1.5')
+Traceback (most recent call last):
+...
+ValidationError: [u'Enter a valid IPv4 address.']
+
#################################
# Tests of underlying functions #
#################################
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---