Ticket #11350: django-11350.diff
File django-11350.diff, 5.8 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..11bdc69
- + 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.translation import ugettext_lazy as _ 9 10 11 id_number_re = re.compile(r'^(?P<number>\d{1,8})(?P<check>\d)$') 12 13 class ILPostalCodeField(RegexField): 14 """ 15 A form field that validates its input as an Israeli postal code. 16 Valid form is XXXXX where X represents integer. 17 """ 18 19 default_error_messages = { 20 'invalid': _(u'Enter a postal code in the format XXXXX'), 21 } 22 23 def __init__(self, *args, **kwargs): 24 super(ILPostalCodeField, self).__init__(r'^\d{5}$', *args, **kwargs) 25 26 def clean(self, value): 27 return super(ILPostalCodeField, self).clean(value.replace(" ", "")) 28 29 30 class ILIDNumberField(Field): 31 """ 32 A form field that validates its input as an Israeli identification number. 33 Valid form is per the Israeli ID specification. 34 """ 35 36 default_error_messages = { 37 'invalid': _(u'Enter a valid ID number.'), 38 } 39 40 def clean(self, value): 41 value = super(ILIDNumberField, self).clean(value) 42 43 if value in EMPTY_VALUES: 44 return u'' 45 46 match = id_number_re.match(value) 47 if not match: 48 raise ValidationError(self.error_messages['invalid']) 49 50 number = match.groupdict()['number'].zfill(8) 51 check = int(match.groupdict()['check']) 52 53 sum = 0 54 weight = 1 55 for digit in number + str(check): 56 sum += (lambda x: x/10 + x % 10)(int(digit)*weight) 57 weight ^= 3 58 59 if sum % 10 != 0: 60 raise ValidationError(self.error_messages['invalid']) 61 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