Beispiel #1
0
 def test_UYCIField(self):
     error_format = ['Enter a valid CI number in X.XXX.XXX-X,XXXXXXX-X or XXXXXXXX format.']
     error_invalid = ['Enter a valid CI number.']
     valid = {
         '4098053': '4098053',
         '409805-3': '409805-3',
         '409.805-3': '409.805-3',
         '10054112': '10054112',
         '1005411-2': '1005411-2',
         '1.005.411-2': '1.005.411-2',
     }
     invalid = {
         'foo': ['Enter a valid CI number in X.XXX.XXX-X,XXXXXXX-X or XXXXXXXX format.'],
         '409805-2': ['Enter a valid CI number.'],
         '1.005.411-5': ['Enter a valid CI number.'],
     }
     self.assertFieldOutput(UYCIField, valid, invalid)
     self.assertEqual(get_validation_digit(409805), 3)
     self.assertEqual(get_validation_digit(1005411), 2)
Beispiel #2
0
    def clean(self, value):
        """
        Validates format and validation digit.

        The official format is [X.]XXX.XXX-X but usually dots and/or slash are
        omitted so, when validating, those characters are ignored if found in
        the correct place. The three typically used formats are supported:
        [X]XXXXXXX, [X]XXXXXX-X and [X.]XXX.XXX-X.
        """

        value = super(UYCIField, self).clean(value)
        if value in EMPTY_VALUES:
            return ''
        match = self.regex.match(value)
        if not match:
            raise ValidationError(self.error_messages['invalid'])

        number = int(match.group('num').replace('.', ''))
        validation_digit = int(match.group('val'))

        if not validation_digit == get_validation_digit(number):
            raise ValidationError(self.error_messages['invalid_validation_digit'])

        return value