Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed #11350 -- added an Israeli localflavor. Thanks to Yuval and Ida…
…n Gazit for their work on the patch. git-svn-id: http://code.djangoproject.com/svn/django/trunk@14223 bcc190cf-cafb-0310-a4f2-bffc1f526a37
- Loading branch information
Showing
5 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,65 @@ | |||
""" | |||
Israeli-specific form helpers | |||
""" | |||
import re | |||
|
|||
from django.core.exceptions import ValidationError | |||
from django.forms.fields import RegexField, Field, EMPTY_VALUES | |||
from django.utils.checksums import luhn | |||
from django.utils.translation import ugettext_lazy as _ | |||
|
|||
# Israeli ID numbers consist of up to 8 digits followed by a checksum digit. | |||
# Numbers which are shorter than 8 digits are effectively left-zero-padded. | |||
# The checksum digit is occasionally separated from the number by a hyphen, | |||
# and is calculated using the luhn algorithm. | |||
# | |||
# Relevant references: | |||
# | |||
# (hebrew) http://he.wikipedia.org/wiki/%D7%9E%D7%A1%D7%A4%D7%A8_%D7%96%D7%94%D7%95%D7%AA_(%D7%99%D7%A9%D7%A8%D7%90%D7%9C) | |||
# (hebrew) http://he.wikipedia.org/wiki/%D7%A1%D7%A4%D7%A8%D7%AA_%D7%91%D7%99%D7%A7%D7%95%D7%A8%D7%AA | |||
|
|||
id_number_re = re.compile(r'^(?P<number>\d{1,8})-?(?P<check>\d)$') | |||
|
|||
class ILPostalCodeField(RegexField): | |||
""" | |||
A form field that validates its input as an Israeli postal code. | |||
Valid form is XXXXX where X represents integer. | |||
""" | |||
|
|||
default_error_messages = { | |||
'invalid': _(u'Enter a postal code in the format XXXXX'), | |||
} | |||
|
|||
def __init__(self, *args, **kwargs): | |||
super(ILPostalCodeField, self).__init__(r'^\d{5}$', *args, **kwargs) | |||
|
|||
def clean(self, value): | |||
if value is not None: | |||
value = value.replace(" ", "") | |||
return super(ILPostalCodeField, self).clean(value) | |||
|
|||
|
|||
class ILIDNumberField(Field): | |||
""" | |||
A form field that validates its input as an Israeli identification number. | |||
Valid form is per the Israeli ID specification. | |||
""" | |||
|
|||
default_error_messages = { | |||
'invalid': _(u'Enter a valid ID number.'), | |||
} | |||
|
|||
def clean(self, value): | |||
value = super(ILIDNumberField, self).clean(value) | |||
|
|||
if value in EMPTY_VALUES: | |||
return u'' | |||
|
|||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,57 @@ | |||
from django.contrib.localflavor.il.forms import (ILPostalCodeField, | |||
ILIDNumberField) | |||
from django.core.exceptions import ValidationError | |||
from django.utils.unittest import TestCase | |||
|
|||
|
|||
class IsraelLocalFlavorTests(TestCase): | |||
def test_postal_code_field(self): | |||
f = ILPostalCodeField() | |||
self.assertRaisesRegexp(ValidationError, | |||
"Enter a postal code in the format XXXXX", | |||
f.clean, "84545x" | |||
) | |||
self.assertEqual(f.clean("69973"), "69973") | |||
self.assertEqual(f.clean("699 73"), "69973") | |||
self.assertEqual(f.clean("12345"), "12345") | |||
self.assertRaisesRegexp(ValidationError, | |||
"Enter a postal code in the format XXXXX", | |||
f.clean, "123456" | |||
) | |||
self.assertRaisesRegexp(ValidationError, | |||
"Enter a postal code in the format XXXXX", | |||
f.clean, "1234" | |||
) | |||
self.assertRaisesRegexp(ValidationError, | |||
"Enter a postal code in the format XXXXX", | |||
f.clean, "123 4" | |||
) | |||
self.assertRaises(ValidationError, f.clean, None) | |||
|
|||
def test_id_number_field(self): | |||
f = ILIDNumberField() | |||
self.assertEqual(f.clean("3933742-3"), "39337423") | |||
self.assertEqual(f.clean("39337423"), "39337423") | |||
self.assertEqual(f.clean("039337423"), "039337423") | |||
self.assertEqual(f.clean("03933742-3"), "039337423") | |||
self.assertEqual(f.clean("0091"), "0091") | |||
self.assertRaisesRegexp(ValidationError, | |||
"Enter a valid ID number.", | |||
f.clean, "123456789" | |||
) | |||
self.assertRaisesRegexp(ValidationError, | |||
"Enter a valid ID number.", | |||
f.clean, "12345678-9" | |||
) | |||
self.assertRaisesRegexp(ValidationError, | |||
"Enter a valid ID number.", | |||
f.clean, "012346578" | |||
) | |||
self.assertRaisesRegexp(ValidationError, | |||
"Enter a valid ID number.", | |||
f.clean, "012346578-" | |||
) | |||
self.assertRaisesRegexp(ValidationError, | |||
"Enter a valid ID number.", | |||
f.clean, "0001" | |||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters