Django

Code

Ticket #811: ip6field.diff

File ip6field.diff, 6.3 kB (added by mattimustang@gmail.com, 3 years ago)

IP6AddressField patch

  • django/core/formfields.py

    old new  
    712712        return data or None 
    713713    html2python = staticmethod(html2python) 
    714714 
     715class IP6AddressField(TextField): 
     716    def __init__(self, field_name, length=39, maxlength=39, is_required=False, validator_list=[]): 
     717        validator_list = [self.isValidIPAddress] + validator_list 
     718        TextField.__init__(self, field_name, length=length, maxlength=maxlength, 
     719            is_required=is_required, validator_list=validator_list) 
     720 
     721    def isValidIPAddress(self, field_data, all_data): 
     722        try: 
     723            validators.isValidIPAddress6(field_data, all_data) 
     724        except validators.ValidationError, e: 
     725            raise validators.CriticalValidationError, e.messages 
     726 
     727    def html2python(data): 
     728        return data or None 
     729    html2python = staticmethod(html2python) 
     730 
    715731#################### 
    716732# MISCELLANEOUS    # 
    717733#################### 
  • django/core/db/backends/ado_mssql.py

    old new  
    145145    'ImageField':        'varchar(100)', 
    146146    'IntegerField':      'int', 
    147147    'IPAddressField':    'char(15)', 
     148    'IP6AddressField':   'char(39)', 
    148149    'ManyToManyField':   None, 
    149150    'NullBooleanField':  'bit', 
    150151    'OneToOneField':     'int', 
  • django/core/db/backends/postgresql.py

    old new  
    166166    'ImageField':        'varchar(100)', 
    167167    'IntegerField':      'integer', 
    168168    'IPAddressField':    'inet', 
     169    'IP6AddressField':   'inet', 
    169170    'ManyToManyField':   None, 
    170171    'NullBooleanField':  'boolean', 
    171172    'OneToOneField':     'integer', 
  • django/core/db/backends/sqlite3.py

    old new  
    164164    'ImageField':                   'varchar(100)', 
    165165    'IntegerField':                 'integer', 
    166166    'IPAddressField':               'char(15)', 
     167    'IP6AddressField':              'char(39)', 
    167168    'ManyToManyField':              None, 
    168169    'NullBooleanField':             'bool', 
    169170    'OneToOneField':                'integer', 
  • django/core/db/backends/mysql.py

    old new  
    161161    'ImageField':        'varchar(100)', 
    162162    'IntegerField':      'integer', 
    163163    'IPAddressField':    'char(15)', 
     164    'IP6AddressField':   'char(39)', 
    164165    'ManyToManyField':   None, 
    165166    'NullBooleanField':  'bool', 
    166167    'OneToOneField':     'integer', 
  • django/core/meta/fields.py

    old new  
    507507    def get_manipulator_field_objs(self): 
    508508        return [formfields.IPAddressField] 
    509509 
     510class IP6AddressField(Field): 
     511    def __init__(self, *args, **kwargs): 
     512        kwargs['maxlength'] = 15 
     513        Field.__init__(self, *args, **kwargs) 
     514 
     515    def get_manipulator_field_objs(self): 
     516        return [formfields.IP6AddressField] 
     517 
    510518class NullBooleanField(Field): 
    511519    def __init__(self, *args, **kwargs): 
    512520        kwargs['null'] = True 
  • django/core/validators.py

    old new  
    2121email_re = re.compile(r'^((([\t\x20]*[!#-\'\*\+\-/-9=\?A-Z\^-~]+[\t\x20]*|"[\x01-\x09\x0B\x0C\x0E-\x21\x23-\x5B\x5D-\x7F]*")+)?[\t\x20]*<([\t\x20]*[!#-\'\*\+\-/-9=\?A-Z\^-~]+(\.[!#-\'\*\+\-/-9=\?A-Z\^-~]+)*|"[\x01-\x09\x0B\x0C\x0E-\x21\x23-\x5B\x5D-\x7F]*")@(([a-zA-Z0-9][-a-zA-Z0-9]*[a-zA-Z0-9]\.)+[a-zA-Z]{2,}|\[(([0-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\])>[\t\x20]*|([\t\x20]*[!#-\'\*\+\-/-9=\?A-Z\^-~]+(\.[!#-\'\*\+\-/-9=\?A-Z\^-~]+)*|"[\x01-\x09\x0B\x0C\x0E-\x21\x23-\x5B\x5D-\x7F]*")@(([a-zA-Z0-9][-a-zA-Z0-9]*[a-zA-Z0-9]\.)+[a-zA-Z]{2,}|\[(([0-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\]))$') 
    2222integer_re = re.compile(r'^-?\d+$') 
    2323ip4_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}$') 
     24_ip6_hex_re = r'(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}' 
     25_ip6_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})*)?)' 
     26_ip6_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}' 
     27_ip6_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}' 
     28ip6_re = re.compile('^(%s|%s|%s|%s)$' % (_ip6_hex_re, _ip6_hex_compressed_re, _ip6_ipv4_compat_re, _ip6_ipv4_compat_compressed_re)) 
    2429phone_re = re.compile(r'^[A-PR-Y0-9]{3}-[A-PR-Y0-9]{3}-[A-PR-Y0-9]{4}$', re.IGNORECASE) 
    2530slug_re = re.compile(r'^[-\w]+$') 
    2631url_re = re.compile(r'^http://\S+$') 
     
    9398        except ValidationError: 
    9499            raise ValidationError, _("Enter valid e-mail addresses separated by commas.") 
    95100 
     101def isValidIPAddress(field_data, all_data): 
     102    if not (ip4_re.search(field_data) or ip6_re.search(field_data)): 
     103        raise ValidationError, _("Please enter a valid IPv4 or IPv6 address.") 
     104 
    96105def isValidIPAddress4(field_data, all_data): 
    97106    if not ip4_re.search(field_data): 
    98         raise ValidationError, _("Please enter a valid IP address.") 
     107        raise ValidationError, _("Please enter a valid IPv4 address.") 
    99108 
     109def isValidIPAddress6(field_data, all_data): 
     110    if not ip6_re.search(field_data): 
     111        raise ValidationError, _("Please enter a valid IPv6 address.") 
     112 
    100113def isNotEmpty(field_data, all_data): 
    101114    if field_data.strip() == '': 
    102115        raise ValidationError, _("Empty values are not allowed here.")