Code

Ticket #811: ipv6field.diff

File ipv6field.diff, 6.8 KB (added by mattimustang@…, 8 years ago)

Updated IPv6 Field patch

Line 
1Index: django/core/formfields.py
2===================================================================
3--- django/core/formfields.py   (revision 2518)
4+++ django/core/formfields.py   (working copy)
5@@ -807,13 +807,13 @@
6 
7 class IPAddressField(TextField):
8     def __init__(self, field_name, length=15, maxlength=15, is_required=False, validator_list=[]):
9-        validator_list = [self.isValidIPAddress] + validator_list
10+        validator_list = [self.isValidIPv4Address] + validator_list
11         TextField.__init__(self, field_name, length=length, maxlength=maxlength,
12             is_required=is_required, validator_list=validator_list)
13 
14-    def isValidIPAddress(self, field_data, all_data):
15+    def isValidIPv4Address(self, field_data, all_data):
16         try:
17-            validators.isValidIPAddress4(field_data, all_data)
18+            validators.isValidIPv4Address(field_data, all_data)
19         except validators.ValidationError, e:
20             raise validators.CriticalValidationError, e.messages
21 
22@@ -821,6 +821,22 @@
23         return data or None
24     html2python = staticmethod(html2python)
25 
26+class IPv6AddressField(TextField):
27+    def __init__(self, field_name, length=39, maxlength=39, is_required=False, validator_list=[]):
28+        validator_list = [self.isValidIPv6Address] + validator_list
29+        TextField.__init__(self, field_name, length=length, maxlength=maxlength,
30+            is_required=is_required, validator_list=validator_list)
31+
32+    def isValidIPv6Address(self, field_data, all_data):
33+        try:
34+            validators.isValidIPv6Address(field_data, all_data)
35+        except validators.ValidationError, e:
36+            raise validators.CriticalValidationError, e.messages
37+
38+    def html2python(data):
39+        return data or None
40+    html2python = staticmethod(html2python)
41+
42 ####################
43 # MISCELLANEOUS    #
44 ####################
45Index: django/core/db/backends/ado_mssql.py
46===================================================================
47--- django/core/db/backends/ado_mssql.py        (revision 2518)
48+++ django/core/db/backends/ado_mssql.py        (working copy)
49@@ -150,6 +150,7 @@
50     'ImageField':        'varchar(100)',
51     'IntegerField':      'int',
52     'IPAddressField':    'char(15)',
53+    'IPv6AddressField':   'char(39)',
54     'ManyToManyField':   None,
55     'NullBooleanField':  'bit',
56     'OneToOneField':     'int',
57Index: django/core/db/backends/postgresql.py
58===================================================================
59--- django/core/db/backends/postgresql.py       (revision 2518)
60+++ django/core/db/backends/postgresql.py       (working copy)
61@@ -200,6 +200,7 @@
62     'ImageField':        'varchar(100)',
63     'IntegerField':      'integer',
64     'IPAddressField':    'inet',
65+    'IPv6AddressField':   'inet',
66     'ManyToManyField':   None,
67     'NullBooleanField':  'boolean',
68     'OneToOneField':     'integer',
69Index: django/core/db/backends/sqlite3.py
70===================================================================
71--- django/core/db/backends/sqlite3.py  (revision 2518)
72+++ django/core/db/backends/sqlite3.py  (working copy)
73@@ -174,6 +174,7 @@
74     'ImageField':                   'varchar(100)',
75     'IntegerField':                 'integer',
76     'IPAddressField':               'char(15)',
77+    'IPv6AddressField':              'char(39)',
78     'ManyToManyField':              None,
79     'NullBooleanField':             'bool',
80     'OneToOneField':                'integer',
81Index: django/core/db/backends/mysql.py
82===================================================================
83--- django/core/db/backends/mysql.py    (revision 2518)
84+++ django/core/db/backends/mysql.py    (working copy)
85@@ -184,6 +184,7 @@
86     'ImageField':        'varchar(100)',
87     'IntegerField':      'integer',
88     'IPAddressField':    'char(15)',
89+    'IPv6AddressField':   'char(39)',
90     'ManyToManyField':   None,
91     'NullBooleanField':  'bool',
92     'OneToOneField':     'integer',
93Index: django/core/meta/fields.py
94===================================================================
95--- django/core/meta/fields.py  (revision 2518)
96+++ django/core/meta/fields.py  (working copy)
97@@ -580,6 +580,14 @@
98     def get_manipulator_field_objs(self):
99         return [formfields.IPAddressField]
100 
101+class IPv6AddressField(Field):
102+    def __init__(self, *args, **kwargs):
103+        kwargs['maxlength'] = 39
104+        Field.__init__(self, *args, **kwargs)
105+
106+    def get_manipulator_field_objs(self):
107+        return [formfields.IPv6AddressField]
108+
109 class NullBooleanField(Field):
110     def __init__(self, *args, **kwargs):
111         kwargs['null'] = True
112Index: django/core/validators.py
113===================================================================
114--- django/core/validators.py   (revision 2518)
115+++ django/core/validators.py   (working copy)
116@@ -19,7 +19,12 @@
117 ansi_datetime_re = re.compile('^%s %s$' % (_datere, _timere))
118 email_re = re.compile(r'^[A-Z0-9._%-][+A-Z0-9._%-]*@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}$', re.IGNORECASE)
119 integer_re = re.compile(r'^-?\d+$')
120-ip4_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}$')
121+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}$')
122+_ipv6_hex_re = r'(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}'
123+_ipv6_hex_compressed_re = r'((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)::((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)'
124+_ipv6_ipv4_compat_re = r'((?:[0-9A-Fa-f]{1,4}:){6,6})(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}'
125+_ipv6_ipv4_compat_compressed_re = r'((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)::((?:[0-9A-Fa-f]{1,4}:)*)(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}'
126+ipv6_re = re.compile('^(%s|%s|%s|%s)$' % (_ipv6_hex_re, _ipv6_hex_compressed_re, _ipv6_ipv4_compat_re, _ipv6_ipv4_compat_compressed_re))
127 phone_re = re.compile(r'^[A-PR-Y0-9]{3}-[A-PR-Y0-9]{3}-[A-PR-Y0-9]{4}$', re.IGNORECASE)
128 slug_re = re.compile(r'^[-\w]+$')
129 url_re = re.compile(r'^https?://\S+$')
130@@ -95,10 +100,18 @@
131         except ValidationError:
132             raise ValidationError, _("Enter valid e-mail addresses separated by commas.")
133 
134-def isValidIPAddress4(field_data, all_data):
135-    if not ip4_re.search(field_data):
136-        raise ValidationError, _("Please enter a valid IP address.")
137+def isValidIPAddress(field_data, all_data):
138+    if not (ipv4_re.search(field_data) or ipv6_re.search(field_data)):
139+        raise ValidationError, _("Please enter a valid IPv4 or IPv6 address.")
140 
141+def isValidIPv4Address(field_data, all_data):
142+    if not ipv4_re.search(field_data):
143+        raise ValidationError, _("Please enter a valid IPv4 address.")
144+
145+def isValidIPv6Address(field_data, all_data):
146+    if not ipv6_re.search(field_data):
147+        raise ValidationError, _("Please enter a valid IPv6 address.")
148+
149 def isNotEmpty(field_data, all_data):
150     if field_data.strip() == '':
151         raise ValidationError, _("Empty values are not allowed here.")