Code

Ticket #10204: patch_nifniecif_es.diff

File patch_nifniecif_es.diff, 2.2 KB (added by Cesar Ortiz <cesar.ortiz@…>, 5 years ago)

An alternative (and right) patch

Line 
1Index: django/contrib/localflavor/es/forms.py
2===================================================================
3--- django/contrib/localflavor/es/forms.py      (revision 10674)
4+++ django/contrib/localflavor/es/forms.py      (working copy)
5@@ -75,7 +75,7 @@
6         self.nif_control = 'TRWAGMYFPDXBNJZSQVHLCKE'
7         self.cif_control = 'JABCDEFGHI'
8         self.cif_types = 'ABCDEFGHKLMNPQS'
9-        self.nie_types = 'XT'
10+        self.nie_types = 'XYZ'
11         id_card_re = re.compile(r'^([%s]?)[ -]?(\d+)[ -]?([%s]?)$' % (self.cif_types + self.nie_types, self.nif_control + self.cif_control), re.IGNORECASE)
12         super(ESIdentityCardNumberField, self).__init__(id_card_re, max_length=None, min_length=None,
13                 error_message=self.default_error_messages['invalid%s' % (self.only_nif and '_only_nif' or '')],
14@@ -99,7 +99,7 @@
15                 raise ValidationError, self.error_messages['invalid_nif']
16         elif letter1 in self.nie_types and letter2:
17             # NIE
18-            if letter2 == nif_get_checksum(number):
19+            if letter2 == nif_get_checksum(str(self.nie_types.find(letter1)) + number):
20                 return value
21             else:
22                 raise ValidationError, self.error_messages['invalid_nie']
23Index: tests/regressiontests/forms/localflavor/es.py
24===================================================================
25--- tests/regressiontests/forms/localflavor/es.py       (revision 10674)
26+++ tests/regressiontests/forms/localflavor/es.py       (working copy)
27@@ -155,8 +155,16 @@
28 'X0012953G'
29 >>> f.clean('x-3287690-r')
30 'X3287690R'
31+>>> f.clean('y3287690r')
32+Traceback (most recent call last):
33+...
34+ValidationError: [u'Invalid checksum for NIE.']
35+>>> f.clean('y3287690s')
36+'Y3287690S'
37 >>> f.clean('t-03287690r')
38-'T03287690R'
39+Traceback (most recent call last):
40+...
41+ValidationError: [u'Please enter a valid NIF, NIE, or CIF.']
42 >>> f.clean('P2907500I')
43 'P2907500I'
44 >>> f.clean('X-03287690')
45@@ -226,7 +234,9 @@
46 >>> f.clean('x-3287690-r')
47 'X3287690R'
48 >>> f.clean('t-03287690r')
49-'T03287690R'
50+Traceback (most recent call last):
51+...
52+ValidationError: [u'Please enter a valid NIF, NIE, or CIF.']
53 >>> f.clean('X-03287690')
54 Traceback (most recent call last):
55 ...