Ticket #11350: django-11350.3.diff

File django-11350.3.diff, 6.5 KB (added by Idan Gazit, 14 years ago)

Adds checking for hyphenated checksum, cited references.

  • 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..00378c9
    - +  
     1"""
     2Israeli-specific form helpers
     3"""
     4import re
     5
     6from django.core.exceptions import ValidationError
     7from django.forms.fields import RegexField, Field, EMPTY_VALUES
     8from django.utils.checksums import luhn
     9from django.utils.translation import ugettext_lazy as _
     10
     11# Israeli ID numbers consist of up to 8 digits followed by a checksum digit.
     12# Numbers which are shorter than 8 digits are effectively left-zero-padded.
     13# The checksum digit is occasionally separated from the number by a hyphen,
     14# and is calculated using the luhn algorithm.
     15#
     16# Relevant references:
     17#
     18# (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)
     19# (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
     20
     21id_number_re = re.compile(r'^(?P<number>\d{1,8})-?(?P<check>\d)$')
     22
     23class ILPostalCodeField(RegexField):
     24    """
     25    A form field that validates its input as an Israeli postal code.
     26    Valid form is XXXXX where X represents integer.
     27    """
     28
     29    default_error_messages = {
     30        'invalid': _(u'Enter a postal code in the format XXXXX'),
     31    }
     32
     33    def __init__(self, *args, **kwargs):
     34        super(ILPostalCodeField, self).__init__(r'^\d{5}$', *args, **kwargs)
     35
     36    def clean(self, value):
     37        return super(ILPostalCodeField, self).clean(value.replace(" ", ""))
     38
     39
     40class ILIDNumberField(Field):
     41    """
     42    A form field that validates its input as an Israeli identification number.
     43    Valid form is per the Israeli ID specification.
     44    """
     45
     46    default_error_messages = {
     47        'invalid': _(u'Enter a valid ID number.'),
     48    }
     49
     50    def clean(self, value):
     51        value = super(ILIDNumberField, self).clean(value)
     52
     53        if value in EMPTY_VALUES:
     54            return u''
     55
     56        match = id_number_re.match(value)
     57        if not match:
     58            raise ValidationError(self.error_messages['invalid'])
     59       
     60        value = match.group('number') + match.group('check')
     61        if not luhn(value):
     62            raise ValidationError(self.error_messages['invalid'])
     63        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:  
    5151    * India_
    5252    * Indonesia_
    5353    * Ireland_
     54    * Israel_
    5455    * Italy_
    5556    * Japan_
    5657    * Kuwait_
    Here's an example of how to use them::  
    99100.. _India: `India (in\_)`_
    100101.. _Indonesia: `Indonesia (id)`_
    101102.. _Ireland: `Ireland (ie)`_
     103.. _Israel: `Israel (il)`_
    102104.. _Italy: `Italy (it)`_
    103105.. _Japan: `Japan (jp)`_
    104106.. _Kuwait: `Kuwait (kw)`_
    Indonesia (``id``)  
    446448
    447449.. _NIK: http://en.wikipedia.org/wiki/Indonesian_identity_card
    448450
     451Israel (``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
    449464Italy (``it``)
    450465==============
    451466
  • 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..e0ed68a
    - +  
     1from django.contrib.localflavor.il.forms import (ILPostalCodeField,
     2    ILIDNumberField)
     3from django.core.exceptions import ValidationError
     4from django.utils.unittest import TestCase
     5
     6
     7class 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("3933742-3"), "39337423")
     33        self.assertEqual(f.clean("39337423"), "39337423")
     34        self.assertEqual(f.clean("039337423"), "039337423")
     35        self.assertEqual(f.clean("03933742-3"), "039337423")
     36        self.assertEqual(f.clean("0091"), "0091")
     37        self.assertRaisesRegexp(ValidationError,
     38            "Enter a valid ID number.",
     39            f.clean, "123456789"
     40        )
     41        self.assertRaisesRegexp(ValidationError,
     42            "Enter a valid ID number.",
     43            f.clean, "12345678-9"
     44        )
     45        self.assertRaisesRegexp(ValidationError,
     46            "Enter a valid ID number.",
     47            f.clean, "012346578"
     48        )
     49        self.assertRaisesRegexp(ValidationError,
     50            "Enter a valid ID number.",
     51            f.clean, "012346578-"
     52        )
     53        self.assertRaisesRegexp(ValidationError,
     54            "Enter a valid ID number.",
     55            f.clean, "0001"
     56        )
  • 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  
    1717from localflavor.generic import tests as localflavor_generic_tests
    1818from localflavor.id import tests as localflavor_id_tests
    1919from localflavor.ie import tests as localflavor_ie_tests
     20from localflavor.il import IsraelLocalFlavorTests
    2021from localflavor.is_ import tests as localflavor_is_tests
    2122from localflavor.it import tests as localflavor_it_tests
    2223from localflavor.jp import tests as localflavor_jp_tests
Back to Top