Ticket #8229: 8229.diff
File 8229.diff, 1.9 KB (added by , 16 years ago) |
---|
-
django/contrib/localflavor/es/forms.py
56 56 according to a couple of simple checksum algorithms. 57 57 58 58 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 a1,59 Number length is not checked for NIF (or NIE), old values start by 1, 60 60 and future values can contain digits greater than 8. The CIF control digit 61 61 can be a number or a letter depending on company type. Algorithm is not 62 62 public, and different authors have different opinions on which ones allows … … 108 108 if not letter2: 109 109 number, letter2 = number[:-1], int(number[-1]) 110 110 checksum = cif_get_checksum(number) 111 if letter2 in [checksum, self.cif_control[checksum]]:111 if letter2 in (checksum, self.cif_control[checksum]): 112 112 return value 113 113 else: 114 114 raise ValidationError, self.error_messages['invalid_cif'] … … 180 180 def cif_get_checksum(number): 181 181 s1 = sum([int(digit) for pos, digit in enumerate(number) if int(pos) % 2]) 182 182 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 184 184 -
tests/regressiontests/forms/localflavor/es.py
167 167 ValidationError: [u'Invalid checksum for NIE.'] 168 168 >>> f.clean('B38790911') 169 169 'B38790911' 170 >>> f.clean('B31234560') 171 'B31234560' 170 172 >>> f.clean('B-3879091A') 171 173 'B3879091A' 172 174 >>> f.clean('B 38790917')