Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix problem in ES validating NIE numbers starting with Y and Z. #217

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/authors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Authors
* Ben Konrath
* Bruno M. Custódio
* Claude Paroz
* Daniel Ampuero
* Daniel Roschka
* Daniela Ponader
* Douglas Miranda
Expand Down
3 changes: 3 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ Modifications to existing flavors:
- Updated IBANField to support the latest additions to the IBAN Registry (version 64 / March 2016).
- Fix bug with MXRFCField where some incorrect values would validate correctly.
(`gh-204 <https://github.com/django/django-localflavor/issues/204>`_).
- Fix bug in ESIdentityCardNumberField where some valid values for NIE numbers were not
validating.
(`gh-204 <https://github.com/django/django-localflavor/pull/217>`_)
- Fixed bug with IBANFormField validation.
(`gh-215 <https://github.com/django/django-localflavor/pull/215>`_).
- Update regex in DEZipCodeField to prohibit invalid postal codes.
Expand Down
5 changes: 3 additions & 2 deletions localflavor/es/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from __future__ import unicode_literals

import re
import six

from django.core.validators import EMPTY_VALUES
from django.forms import ValidationError
Expand Down Expand Up @@ -90,7 +91,7 @@ def __init__(self, only_nif=False, max_length=None, min_length=None, *args, **kw
self.nif_control = 'TRWAGMYFPDXBNJZSQVHLCKE'
self.cif_control = 'JABCDEFGHI'
self.cif_types = 'ABCDEFGHJKLMNPQRSVW'
self.nie_types = 'XTY'
self.nie_types = 'XYZ'
self.id_card_pattern = r'^([%s]?)[ -]?(\d+)[ -]?([%s]?)$'
id_card_re = re.compile(self.id_card_pattern %
(self.cif_types + self.nie_types,
Expand Down Expand Up @@ -122,7 +123,7 @@ def clean(self, value):
raise ValidationError(self.error_messages['invalid_nif'])
elif letter1 in self.nie_types and letter2:
# NIE
if letter2 == self.nif_get_checksum(number):
if letter2 == self.nif_get_checksum(six.text_type(self.nie_types.index(letter1)) + number):
return value
else:
raise ValidationError(self.error_messages['invalid_nie'])
Expand Down
8 changes: 6 additions & 2 deletions tests/test_es.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,10 @@ def test_ESIdentityCardNumberField(self):
'X-6124387-Q': 'X6124387Q',
'X 0012953 G': 'X0012953G',
'x-3287690-r': 'X3287690R',
'y-3287690-r': 'Y3287690R',
't-03287690r': 'T03287690R',
'y-0226481-Z': 'Y0226481Z',
'y-4710494-q': 'Y4710494Q',
'z-4256192-r': 'Z4256192R',
'z-3214469-v': 'Z3214469V',
'P2907500I': 'P2907500I',
'B38790911': 'B38790911',
'B31234560': 'B31234560',
Expand All @@ -153,6 +155,8 @@ def test_ESIdentityCardNumberField(self):
'78699688T': error_checksum_nif,
'X-03287690': error_invalid,
'X-03287690-T': error_checksum_nie,
'y-3287690-r': error_checksum_nie,
't-03287690r': error_invalid,
'B 38790917': error_checksum_cif,
'C28795567': error_checksum_cif,
'I38790911': error_invalid,
Expand Down