def checksum(value): value = clean_iban(value) value = value[4:] + value[:2] + '00' value_digits = '' for x in value: if '0' <= x <= '9': value_digits += x elif 'A' <= x <= 'Z': value_digits += str(ord(x) - 55) else: raise Exception('{} is not a valid character for IBAN.'.format(x)) return '%02d' % (98 - int(value_digits) % 97)
def __call__(self, value): if value is not None: value = clean_iban(value) country_code = value[:2] iban_checksum = value[2:4] # Check if the country is in the known list if country_code not in IBAN_SPECIFICATION_CONFIG: raise ValidationError(_('The country code does not correspond to any of the know iban specifications')) # Decoding the specific country specification country_data = IBAN_SPECIFICATION_CONFIG[country_code] # Checking if the iban matches the country specification if not match(country_data.iban_regex, value): raise ValidationError(_('The IBAN does not match the expected format for this country')) if country_data.checksum(value) != iban_checksum: raise ValidationError(_('The IBAN checksum is invalid.'))
def prepare_value(self, value): if value is not None: value = clean_iban(value) return ' '.join( value[i:i + self.grouping] for i in range(0, len(value), self.grouping))
def to_python(self, value): value = CharField.to_python(self, value) if value is not None: return clean_iban(value)