Ticket #1636: django_cidr.diff

File django_cidr.diff, 6.1 KB (added by Rob Shakir (rob@…, 19 years ago)

CIDR Patch for Django

  • django/db/models/fields/__init__.py

     
    670670    def validate(self, field_data, all_data):
    671671        validators.isValidIPAddress4(field_data, None)
    672672
     673class CIDRAddressField(Field):
     674    def __init__(self, *args, **kwargs):
     675        kwargs['maxlength'] = 18
     676        Field.__init__(self, *args, **kwargs)
     677
     678    def get_manipulator_field_objs(self):
     679        return [forms.CIDRAddressField]
     680
     681    def valid(self, field_data, all_data):
     682        validators.isValidCIDRAddress4(field_data, None)
     683
    673684class NullBooleanField(Field):
    674685    def __init__(self, *args, **kwargs):
    675686        kwargs['null'] = True
  • django/db/backends/ado_mssql/creation.py

     
    1111    'ImageField':        'varchar(100)',
    1212    'IntegerField':      'int',
    1313    'IPAddressField':    'char(15)',
     14    'CIDRAddressField':  'char(18)',
    1415    'ManyToManyField':   None,
    1516    'NullBooleanField':  'bit',
    1617    'OneToOneField':     'int',
  • django/db/backends/postgresql/introspection.py

     
    7474    21: 'SmallIntegerField',
    7575    23: 'IntegerField',
    7676    25: 'TextField',
     77    650: 'CIDRAddressField',
    7778    869: 'IPAddressField',
    7879    1043: 'CharField',
    7980    1082: 'DateField',
  • django/db/backends/postgresql/creation.py

     
    1515    'ImageField':        'varchar(100)',
    1616    'IntegerField':      'integer',
    1717    'IPAddressField':    'inet',
     18    'CIDRAddressField':  'cidr',
    1819    'ManyToManyField':   None,
    1920    'NullBooleanField':  'boolean',
    2021    'OneToOneField':     'integer',
  • django/db/backends/sqlite3/creation.py

     
    1414    'ImageField':                   'varchar(100)',
    1515    'IntegerField':                 'integer',
    1616    'IPAddressField':               'char(15)',
     17    'CIDRAddressField':             'char(18)',
    1718    'ManyToManyField':              None,
    1819    'NullBooleanField':             'bool',
    1920    'OneToOneField':                'integer',
  • django/db/backends/mysql/creation.py

     
    1515    'ImageField':        'varchar(100)',
    1616    'IntegerField':      'integer',
    1717    'IPAddressField':    'char(15)',
     18    'CIDRAddressField':  'char(18)',
    1819    'ManyToManyField':   None,
    1920    'NullBooleanField':  'bool',
    2021    'OneToOneField':     'integer',
  • django/forms/__init__.py

     
    863863        return data or None
    864864    html2python = staticmethod(html2python)
    865865
     866class CIDRAddressField(TextField):
     867    def __init__(self, field_name, length=18, maxlength=18, is_required=False, validator_list=[]):
     868        validator_list = [self.isValidCIDRAddress] + validator_list
     869        TextField.__init__(self, field_name, length=length, maxlength=maxlength,            is_required=is_required, validator_list=validator_list)
     870
     871    def isValidCIDRAddress(self, field_data, all_data):
     872        try:
     873           validators.isValidCIDRAddress4(field_data, all_data)
     874        except validators.ValidationError, e:
     875           raise validators.CriticalValidationError, e.messages
     876
    866877####################
    867878# MISCELLANEOUS    #
    868879####################
  • django/core/validators.py

     
    2323email_re = re.compile(r'^[A-Z0-9._%-][+A-Z0-9._%-]*@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}$', re.IGNORECASE)
    2424integer_re = re.compile(r'^-?\d+$')
    2525ip4_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}$')
     26ip4_cidr_re = re.compile(r'^([0-9]|1?\d\d|2[0-4]\d|25[0-5])\.([0-9]|1?\d\d|2[0-4]\d|25[0-5])\.([0-9]|1?\d\d|2[0-4]\d|25[0-5])\.([0-9]|1?\d\d|2[0-4]\d|25[0-5])(\/([1-9]|1\d|2\d|3[0-2]))$')
    2627phone_re = re.compile(r'^[A-PR-Y0-9]{3}-[A-PR-Y0-9]{3}-[A-PR-Y0-9]{4}$', re.IGNORECASE)
    2728slug_re = re.compile(r'^[-\w]+$')
    2829url_re = re.compile(r'^https?://\S+$')
     
    9899    if not ip4_re.search(field_data):
    99100        raise ValidationError, gettext("Please enter a valid IP address.")
    100101
     102def isValidCIDRAddress4(field_data, all_data):
     103    if not ip4_cidr_re.search(field_data):
     104        raise ValidationError, gettext("Please enter a valid CIDR address.")
     105    cidr_parts = field_data.split('/')
     106    if not 0 <= int(cidr_parts[1]) <= 32:
     107        raise ValidationError, gettext("CIDR Mask not in valid range.")
     108    inet_octets = cidr_parts[0].split('.')
     109    inet_dec = 0L
     110    inet_dec = inet_dec | (long(inet_octets[0]) << 24)
     111    inet_dec = inet_dec | (long(inet_octets[1]) << 16)
     112    inet_dec = inet_dec | (long(inet_octets[2]) << 8)
     113    inet_dec = inet_dec | (long(inet_octets[3]))
     114
     115    inet_bin = ''
     116    for i in xrange(0, 32):
     117        if inet_dec & 1:
     118           inet_bin = '1' + inet_bin
     119        else:
     120           inet_bin = '0' + inet_bin
     121        inet_dec = inet_dec >> 1
     122    for i in range(int(cidr_parts[1]), 32):
     123        if int(inet_bin[i]) == 1:
     124           raise ValidationError, gettext("CIDR addresses must not have host bits.")
     125   
     126
    101127def isNotEmpty(field_data, all_data):
    102128    if field_data.strip() == '':
    103129        raise ValidationError, gettext("Empty values are not allowed here.")
Back to Top