Ticket #811: ipv6field.diff

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

Updated IPv6 Field patch

  • django/core/formfields.py

     
    807807
    808808class IPAddressField(TextField):
    809809    def __init__(self, field_name, length=15, maxlength=15, is_required=False, validator_list=[]):
    810         validator_list = [self.isValidIPAddress] + validator_list
     810        validator_list = [self.isValidIPv4Address] + validator_list
    811811        TextField.__init__(self, field_name, length=length, maxlength=maxlength,
    812812            is_required=is_required, validator_list=validator_list)
    813813
    814     def isValidIPAddress(self, field_data, all_data):
     814    def isValidIPv4Address(self, field_data, all_data):
    815815        try:
    816             validators.isValidIPAddress4(field_data, all_data)
     816            validators.isValidIPv4Address(field_data, all_data)
    817817        except validators.ValidationError, e:
    818818            raise validators.CriticalValidationError, e.messages
    819819
     
    821821        return data or None
    822822    html2python = staticmethod(html2python)
    823823
     824class IPv6AddressField(TextField):
     825    def __init__(self, field_name, length=39, maxlength=39, is_required=False, validator_list=[]):
     826        validator_list = [self.isValidIPv6Address] + validator_list
     827        TextField.__init__(self, field_name, length=length, maxlength=maxlength,
     828            is_required=is_required, validator_list=validator_list)
     829
     830    def isValidIPv6Address(self, field_data, all_data):
     831        try:
     832            validators.isValidIPv6Address(field_data, all_data)
     833        except validators.ValidationError, e:
     834            raise validators.CriticalValidationError, e.messages
     835
     836    def html2python(data):
     837        return data or None
     838    html2python = staticmethod(html2python)
     839
    824840####################
    825841# MISCELLANEOUS    #
    826842####################
  • django/core/db/backends/ado_mssql.py

     
    150150    'ImageField':        'varchar(100)',
    151151    'IntegerField':      'int',
    152152    'IPAddressField':    'char(15)',
     153    'IPv6AddressField':   'char(39)',
    153154    'ManyToManyField':   None,
    154155    'NullBooleanField':  'bit',
    155156    'OneToOneField':     'int',
  • django/core/db/backends/postgresql.py

     
    200200    'ImageField':        'varchar(100)',
    201201    'IntegerField':      'integer',
    202202    'IPAddressField':    'inet',
     203    'IPv6AddressField':   'inet',
    203204    'ManyToManyField':   None,
    204205    'NullBooleanField':  'boolean',
    205206    'OneToOneField':     'integer',
  • django/core/db/backends/sqlite3.py

     
    174174    'ImageField':                   'varchar(100)',
    175175    'IntegerField':                 'integer',
    176176    'IPAddressField':               'char(15)',
     177    'IPv6AddressField':              'char(39)',
    177178    'ManyToManyField':              None,
    178179    'NullBooleanField':             'bool',
    179180    'OneToOneField':                'integer',
  • django/core/db/backends/mysql.py

     
    184184    'ImageField':        'varchar(100)',
    185185    'IntegerField':      'integer',
    186186    'IPAddressField':    'char(15)',
     187    'IPv6AddressField':   'char(39)',
    187188    'ManyToManyField':   None,
    188189    'NullBooleanField':  'bool',
    189190    'OneToOneField':     'integer',
  • django/core/meta/fields.py

     
    580580    def get_manipulator_field_objs(self):
    581581        return [formfields.IPAddressField]
    582582
     583class IPv6AddressField(Field):
     584    def __init__(self, *args, **kwargs):
     585        kwargs['maxlength'] = 39
     586        Field.__init__(self, *args, **kwargs)
     587
     588    def get_manipulator_field_objs(self):
     589        return [formfields.IPv6AddressField]
     590
    583591class NullBooleanField(Field):
    584592    def __init__(self, *args, **kwargs):
    585593        kwargs['null'] = True
  • django/core/validators.py

     
    1919ansi_datetime_re = re.compile('^%s %s$' % (_datere, _timere))
    2020email_re = re.compile(r'^[A-Z0-9._%-][+A-Z0-9._%-]*@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}$', re.IGNORECASE)
    2121integer_re = re.compile(r'^-?\d+$')
    22 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}$')
     22ipv4_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}$')
     23_ipv6_hex_re = r'(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}'
     24_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})*)?)'
     25_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}'
     26_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}'
     27ipv6_re = re.compile('^(%s|%s|%s|%s)$' % (_ipv6_hex_re, _ipv6_hex_compressed_re, _ipv6_ipv4_compat_re, _ipv6_ipv4_compat_compressed_re))
    2328phone_re = re.compile(r'^[A-PR-Y0-9]{3}-[A-PR-Y0-9]{3}-[A-PR-Y0-9]{4}$', re.IGNORECASE)
    2429slug_re = re.compile(r'^[-\w]+$')
    2530url_re = re.compile(r'^https?://\S+$')
     
    95100        except ValidationError:
    96101            raise ValidationError, _("Enter valid e-mail addresses separated by commas.")
    97102
    98 def isValidIPAddress4(field_data, all_data):
    99     if not ip4_re.search(field_data):
    100         raise ValidationError, _("Please enter a valid IP address.")
     103def isValidIPAddress(field_data, all_data):
     104    if not (ipv4_re.search(field_data) or ipv6_re.search(field_data)):
     105        raise ValidationError, _("Please enter a valid IPv4 or IPv6 address.")
    101106
     107def isValidIPv4Address(field_data, all_data):
     108    if not ipv4_re.search(field_data):
     109        raise ValidationError, _("Please enter a valid IPv4 address.")
     110
     111def isValidIPv6Address(field_data, all_data):
     112    if not ipv6_re.search(field_data):
     113        raise ValidationError, _("Please enter a valid IPv6 address.")
     114
    102115def isNotEmpty(field_data, all_data):
    103116    if field_data.strip() == '':
    104117        raise ValidationError, _("Empty values are not allowed here.")
Back to Top