Ticket #8229: 8229.diff

File 8229.diff, 1.9 KB (added by Marc Garcia, 16 years ago)

Fix for the issue, including a regression test for the failing case

  • django/contrib/localflavor/es/forms.py

     
    5656    according to a couple of simple checksum algorithms.
    5757
    5858    Value can include a space or hyphen separator between number and letters.
    59     Number length is not checked for NIF (or NIE), old values start with a 1,
     59    Number length is not checked for NIF (or NIE), old values start by 1,
    6060    and future values can contain digits greater than 8. The CIF control digit
    6161    can be a number or a letter depending on company type. Algorithm is not
    6262    public, and different authors have different opinions on which ones allows
     
    108108            if not letter2:
    109109                number, letter2 = number[:-1], int(number[-1])
    110110            checksum = cif_get_checksum(number)
    111             if letter2 in [checksum, self.cif_control[checksum]]:
     111            if letter2 in (checksum, self.cif_control[checksum]):
    112112                return value
    113113            else:
    114114                raise ValidationError, self.error_messages['invalid_cif']
     
    180180def cif_get_checksum(number):
    181181    s1 = sum([int(digit) for pos, digit in enumerate(number) if int(pos) % 2])
    182182    s2 = sum([sum([int(unit) for unit in str(int(digit) * 2)]) for pos, digit in enumerate(number) if not int(pos) % 2])
    183     return 10 - ((s1 + s2) % 10)
     183    return (10 - ((s1 + s2) % 10)) % 10
    184184
  • tests/regressiontests/forms/localflavor/es.py

     
    167167ValidationError: [u'Invalid checksum for NIE.']
    168168>>> f.clean('B38790911')
    169169'B38790911'
     170>>> f.clean('B31234560')
     171'B31234560'
    170172>>> f.clean('B-3879091A')
    171173'B3879091A'
    172174>>> f.clean('B 38790917')
Back to Top