def validate_personnummer(person_nummer): """ Validate a personnummer. Raises ValidationError if the personnummer is not valid """ valid = personnummer.valid(person_nummer) # If the personummer is invalid it could be a t-number. if not valid and personnummer_is_t_number(person_nummer): # T-numbers are a bit difficult to handle. Since the personnummer # library can't parse or format T-numbers, we must do it on our own. # This solution forces t-numbers to be the longest format. This way # we will always have the same format on all t-numbers which makes # lookup in the database easier. RegexValidator( r'^[0-9]{8}-.{4}$', message=_('Your personnummer must be on the format YYYYMMDD-XXXX' ))(person_nummer) # The letter in a t-number is counted as a 1 when validating it. # Therefor we exchange the letter for a 1 and validate it as a normal # personnummer. It's not guaranteed that this will work in all cases person_nummer = person_nummer[:-4] + '1' + person_nummer[-3:] valid = personnummer.valid(person_nummer) if not valid: raise ValidationError(_("Invalid personnummer")) return person_nummer
def test_with_control_digit(self): self.assertTrue(personnummer.valid(6403273813)) self.assertTrue(personnummer.valid('510818-9167')) self.assertTrue(personnummer.valid('851226-2190')) self.assertTrue(personnummer.valid('19900101-0017')) self.assertTrue(personnummer.valid('19130401+2931')) self.assertTrue(personnummer.valid('196408233234')) self.assertTrue(personnummer.valid('000101-0107')) self.assertTrue(personnummer.valid('0001010107')) self.assertTrue(personnummer.valid('200002296127')) self.assertTrue(personnummer.valid('200002283422'))
def testPersonnummerList(self): for item in test_data: for format in availableListFormats: self.assertEqual(personnummer.valid(item[format]), item['valid'])
def test_wrong_coordination_numbers(self): self.assertFalse(personnummer.valid('900161-0017')) self.assertFalse(personnummer.valid('640893-3231'))
def test_coordination_numbers(self): self.assertTrue(personnummer.valid('701063-2391')) self.assertTrue(personnummer.valid('640883-3231'))
def test_wrong_personnummer_or_types(self): self.assertFalse(personnummer.valid(None)) self.assertFalse(personnummer.valid([])) self.assertFalse(personnummer.valid({})) self.assertFalse(personnummer.valid(False)) self.assertFalse(personnummer.valid(True)) self.assertFalse(personnummer.valid(1122334455)) self.assertFalse(personnummer.valid('112233-4455')) self.assertFalse(personnummer.valid('19112233-4455')) self.assertFalse(personnummer.valid('9999999999')) self.assertFalse(personnummer.valid('199999999999')) self.assertFalse(personnummer.valid('9913131315')) self.assertFalse(personnummer.valid('9911311232')) self.assertFalse(personnummer.valid('9902291237')) self.assertFalse(personnummer.valid('19990919_3766')) self.assertFalse(personnummer.valid('990919_3766')) self.assertFalse(personnummer.valid('199909193776')) self.assertFalse(personnummer.valid('Just a string')) self.assertFalse(personnummer.valid('990919+3776')) self.assertFalse(personnummer.valid('990919-3776')) self.assertFalse(personnummer.valid('9909193776'))
def test_without_control_digit(self): self.assertFalse(personnummer.valid(640327381)) self.assertFalse(personnummer.valid('510818-916')) self.assertFalse(personnummer.valid('19900101-001')) self.assertFalse(personnummer.valid('100101+001'))
def test_exclude_of_coordination_numbers(self): self.assertFalse(personnummer.valid('701063-2391', False)) self.assertFalse(personnummer.valid('640883-3231', False))