Ticket #11350: django-11350.diff

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

Converted tests to unittests.

  • 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"""
     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.translation import ugettext_lazy as _
     9
     10
     11id_number_re = re.compile(r'^(?P<number>\d{1,8})(?P<check>\d)$')
     12
     13class 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
     30class 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:  
    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