Ticket #11350: django-11350.2.diff
File django-11350.2.diff, 5.5 KB (added by , 14 years ago) |
---|
-
new file django/contrib/localflavor/il/forms.py
diff --git a/django/contrib/localflavor/il/__init__.py b/django/contrib/localflavor/il/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/django/contrib/localflavor/il/forms.py b/django/contrib/localflavor/il/forms.py new file mode 100644 index 0000000..f6a40a3
- + 1 """ 2 Israeli-specific form helpers 3 """ 4 import re 5 6 from django.core.exceptions import ValidationError 7 from django.forms.fields import RegexField, Field, EMPTY_VALUES 8 from django.utils.checksums import luhn 9 from django.utils.translation import ugettext_lazy as _ 10 11 12 id_number_re = re.compile(r'^(?P<number>\d{1,8})(?P<check>\d)$') 13 14 class ILPostalCodeField(RegexField): 15 """ 16 A form field that validates its input as an Israeli postal code. 17 Valid form is XXXXX where X represents integer. 18 """ 19 20 default_error_messages = { 21 'invalid': _(u'Enter a postal code in the format XXXXX'), 22 } 23 24 def __init__(self, *args, **kwargs): 25 super(ILPostalCodeField, self).__init__(r'^\d{5}$', *args, **kwargs) 26 27 def clean(self, value): 28 return super(ILPostalCodeField, self).clean(value.replace(" ", "")) 29 30 31 class ILIDNumberField(Field): 32 """ 33 A form field that validates its input as an Israeli identification number. 34 Valid form is per the Israeli ID specification. 35 """ 36 37 default_error_messages = { 38 'invalid': _(u'Enter a valid ID number.'), 39 } 40 41 def clean(self, value): 42 value = super(ILIDNumberField, self).clean(value) 43 44 if value in EMPTY_VALUES: 45 return u'' 46 47 match = id_number_re.match(value) 48 if not match: 49 raise ValidationError(self.error_messages['invalid']) 50 if not luhn(value): 51 raise ValidationError(self.error_messages['invalid']) 52 return value -
docs/ref/contrib/localflavor.txt
diff --git a/docs/ref/contrib/localflavor.txt b/docs/ref/contrib/localflavor.txt index ae81248..ea14d25 100644
a b Countries currently supported by :mod:`~django.contrib.localflavor` are: 51 51 * India_ 52 52 * Indonesia_ 53 53 * Ireland_ 54 * Israel_ 54 55 * Italy_ 55 56 * Japan_ 56 57 * Kuwait_ … … Here's an example of how to use them:: 99 100 .. _India: `India (in\_)`_ 100 101 .. _Indonesia: `Indonesia (id)`_ 101 102 .. _Ireland: `Ireland (ie)`_ 103 .. _Israel: `Israel (il)`_ 102 104 .. _Italy: `Italy (it)`_ 103 105 .. _Japan: `Japan (jp)`_ 104 106 .. _Kuwait: `Kuwait (kw)`_ … … Indonesia (``id``) 446 448 447 449 .. _NIK: http://en.wikipedia.org/wiki/Indonesian_identity_card 448 450 451 Israel (``il``) 452 =============== 453 454 .. class: il.forms.ILPostalCodeField 455 456 A form field that validates its input as an Israeli postal code. A valid 457 form is XXXXX where X represents integer. 458 459 .. class:: il.forms.ILIDNumberField 460 461 A form field that validates its input as an Israeli identification number. 462 Valid form is per the Israeli ID specification. 463 449 464 Italy (``it``) 450 465 ============== 451 466 -
new file tests/regressiontests/forms/localflavor/il.py
diff --git a/tests/regressiontests/forms/localflavor/il.py b/tests/regressiontests/forms/localflavor/il.py new file mode 100644 index 0000000..a61a23a
- + 1 from django.contrib.localflavor.il.forms import (ILPostalCodeField, 2 ILIDNumberField) 3 from django.core.exceptions import ValidationError 4 from django.utils.unittest import TestCase 5 6 7 class IsraelLocalFlavorTests(TestCase): 8 def test_postal_code_field(self): 9 f = ILPostalCodeField() 10 self.assertRaisesRegexp(ValidationError, 11 "Enter a postal code in the format XXXXX", 12 f.clean, "84545x" 13 ) 14 self.assertEqual(f.clean("69973"), "69973") 15 self.assertEqual(f.clean("699 73"), "69973") 16 self.assertEqual(f.clean("12345"), "12345") 17 self.assertRaisesRegexp(ValidationError, 18 "Enter a postal code in the format XXXXX", 19 f.clean, "123456" 20 ) 21 self.assertRaisesRegexp(ValidationError, 22 "Enter a postal code in the format XXXXX", 23 f.clean, "1234" 24 ) 25 self.assertRaisesRegexp(ValidationError, 26 "Enter a postal code in the format XXXXX", 27 f.clean, "123 4" 28 ) 29 30 def test_id_number_field(self): 31 f = ILIDNumberField() 32 self.assertEqual(f.clean("39337423"), "39337423") 33 self.assertEqual(f.clean("039337423"), "039337423") 34 self.assertEqual(f.clean("0091"), "0091") 35 self.assertRaisesRegexp(ValidationError, 36 "Enter a valid ID number.", 37 f.clean, "123456789" 38 ) 39 self.assertRaisesRegexp(ValidationError, 40 "Enter a valid ID number.", 41 f.clean, "012346578" 42 ) 43 self.assertRaisesRegexp(ValidationError, 44 "Enter a valid ID number.", 45 f.clean, "0001" 46 ) -
tests/regressiontests/forms/tests.py
diff --git a/tests/regressiontests/forms/tests.py b/tests/regressiontests/forms/tests.py index fb41501..635a989 100644
a b from localflavor.fr import tests as localflavor_fr_tests 17 17 from localflavor.generic import tests as localflavor_generic_tests 18 18 from localflavor.id import tests as localflavor_id_tests 19 19 from localflavor.ie import tests as localflavor_ie_tests 20 from localflavor.il import IsraelLocalFlavorTests 20 21 from localflavor.is_ import tests as localflavor_is_tests 21 22 from localflavor.it import tests as localflavor_it_tests 22 23 from localflavor.jp import tests as localflavor_jp_tests