def test_luhn(self): """ Check that function(value) equals output. """ items = ( (4111111111111111, True), ('4111111111111111', True), (4222222222222, True), (378734493671000, True), (5424000000000015, True), (5555555555554444, True), (1008, True), ('0000001008', True), ('000000001008', True), (4012888888881881, True), (1234567890123456789012345678909, True), (4111111111211111, False), (42222222222224, False), (100, False), ('100', False), ('0000100', False), ('abc', False), (None, False), (object(), False), ) for value, output in items: self.assertEqual(checksums.luhn(value), output, value)
def clean(self, value): super(ZAIDField, self).clean(value) if value in EMPTY_VALUES: return '' # strip spaces and dashes value = value.strip().replace(' ', '').replace('-', '') match = re.match(id_re, value) if not match: raise ValidationError(self.error_messages['invalid']) g = match.groupdict() try: # The year 2000 is conveniently a leapyear. # This algorithm will break in xx00 years which aren't leap years # There is no way to guess the century of a ZA ID number date(int(g['yy']) + 2000, int(g['mm']), int(g['dd'])) except ValueError: raise ValidationError(self.error_messages['invalid']) if not luhn(value): raise ValidationError(self.error_messages['invalid']) return value
def clean(self, value): super(ZAIDField, self).clean(value) if value in self.empty_values: return self.empty_value # strip spaces and dashes value = value.strip().replace(' ', '').replace('-', '') match = re.match(id_re, value) if not match: raise ValidationError(self.error_messages['invalid']) g = match.groupdict() try: # The year 2000 is conveniently a leapyear. # This algorithm will break in xx00 years which aren't leap years # There is no way to guess the century of a ZA ID number date(int(g['yy']) + 2000, int(g['mm']), int(g['dd'])) except ValueError: raise ValidationError(self.error_messages['invalid']) if not luhn(value): raise ValidationError(self.error_messages['invalid']) return value
def clean(self, value): if value not in EMPTY_VALUES: value = value.replace(' ', '').replace('-', '') ret = super(FRSIRETField, self).clean(value) if not luhn(ret[:9]): raise ValidationError(self.error_messages['invalid']) return ret
def clean(self, value): super(FRSIRENENumberMixin, self).clean(value) if value in EMPTY_VALUES: return '' value = value.replace(' ', '').replace('-', '') if not self.r_valid.match(value) or not luhn(value): raise ValidationError(self.error_messages['invalid']) return value
def clean(self, value): value = super(ILIDNumberField, self).clean(value) if value in EMPTY_VALUES: return '' match = id_number_re.match(value) if not match: raise ValidationError(self.error_messages['invalid']) value = match.group('number') + match.group('check') if not luhn(value): raise ValidationError(self.error_messages['invalid']) return value
def clean(self, value): super(CASocialInsuranceNumberField, self).clean(value) if value in EMPTY_VALUES: return "" match = re.match(sin_re, value) if not match: raise ValidationError(self.error_messages["invalid"]) number = "%s-%s-%s" % (match.group(1), match.group(2), match.group(3)) check_number = "%s%s%s" % (match.group(1), match.group(2), match.group(3)) if not luhn(check_number): raise ValidationError(self.error_messages["invalid"]) return number
def clean(self, value): super(CASocialInsuranceNumberField, self).clean(value) if value in EMPTY_VALUES: return '' match = re.match(sin_re, value) if not match: raise ValidationError(self.error_messages['invalid']) number = '%s-%s-%s' % (match.group(1), match.group(2), match.group(3)) check_number = '%s%s%s' % (match.group(1), match.group(2), match.group(3)) if not luhn(check_number): raise ValidationError(self.error_messages['invalid']) return number
def clean(self, value): super(GRSocialSecurityNumberCodeField, self).clean(value) if value in self.empty_values: return self.empty_value val = re.sub('[\-\s]', '', force_text(value)) if not val or len(val) < 11: raise ValidationError(self.error_messages['invalid']) if self.allow_test_value and val == '00000000000': return val if not all(char.isdigit() for char in val): raise ValidationError(self.error_messages['invalid']) self.check_date(val) if not luhn(val): raise ValidationError(self.error_messages['invalid']) return val
def clean(self, value): super(HRJMBAGField, self).clean(value) if value in EMPTY_VALUES: return '' value = re.sub(r'[\-\s]', '', value.strip()) matches = jmbag_re.search(value) if matches is None: raise ValidationError(self.error_messages['invalid']) # Make sure the issue number is not zero. if matches.group('copy') == '0': raise ValidationError(self.error_messages['copy']) # Validate checksum using Luhn algorithm. if not luhn(value): raise ValidationError(self.error_messages['invalid']) return '%s' % value