Ticket #11350: django-11350.2.diff

File django-11350.2.diff, 5.5 KB (added by Alex Gaynor, 14 years ago)

Use the luhn function as suggested by jezdez.

  • 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"""
     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
     12id_number_re = re.compile(r'^(?P<number>\d{1,8})(?P<check>\d)$')
     13
     14class 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
     31class 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:  
    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..a61a23a
    - +  
     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("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  
    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