def validate(number): """Check if the number provided is a valid VAT number. This checks the length, formatting and check digit.""" number = compact(number) if not isdigits(number[1:-1]): raise InvalidFormat() if len(number) != 9: raise InvalidLength() if number[0] in 'KLM': # K: Spanish younger than 14 year old # L: Spanish living outside Spain without DNI # M: granted the tax to foreigners who have no NIE # these use the old checkdigit algorithm (the DNI one) if number[-1] != dni.calc_check_digit(number[1:-1]): raise InvalidChecksum() elif isdigits(number[0]): # natural resident dni.validate(number) elif number[0] in 'XYZ': # foreign natural person nie.validate(number) else: # otherwise it has to be a valid CIF cif.validate(number) return number
def validate(number): """Checks to see if the number provided is a valid DNI number. This checks the length, formatting and check digit.""" number = compact(number) if not number[1:-1].isdigit(): raise InvalidFormat() if len(number) != 9: raise InvalidLength() if number[0] in 'KLM': # K: Spanish younger than 14 year old # L: Spanish living outside Spain without DNI # M: granted the tax to foreigners who have no NIE # these use the old checkdigit algorithm (the DNI one) if number[-1] != dni.calc_check_digit(number[1:-1]): raise InvalidChecksum() elif number[0] in 'ABCDEFGHJNPQRSUVW': # there seems to be conflicting information on which organisation types # should have which type of check digit (alphabetic or numeric) so # we support either here if number[-1] not in calc_check_digits(number[:-1]): raise InvalidChecksum() else: # anything else is invalid raise InvalidFormat() return number
def validate(number): """Check if the number provided is a valid DNI number. This checks the length, formatting and check digit.""" number = compact(number) if not number[1:-1].isdigit(): raise InvalidFormat() if len(number) != 9: raise InvalidLength() if number[0] in 'KLM': # K: Spanish younger than 14 year old # L: Spanish living outside Spain without DNI # M: granted the tax to foreigners who have no NIE # these use the old checkdigit algorithm (the DNI one) if number[-1] != dni.calc_check_digit(number[1:-1]): raise InvalidChecksum() elif number[0] in 'ABCDEFGHJNPQRSUVW': # there seems to be conflicting information on which organisation types # should have which type of check digit (alphabetic or numeric) so # we support either here if number[-1] not in calc_check_digits(number[:-1]): raise InvalidChecksum() else: # anything else is invalid raise InvalidFormat() return number
def calc_check_digit(number): """Calculate the check digit. The number passed should not have the check digit included.""" # replace XYZ with 012 number = str('XYZ'.index(number[0])) + number[1:] return dni.calc_check_digit(number)