Ticket #8068: localflavor_kw.patch

File localflavor_kw.patch, 5.6 KB (added by alibrahim, 16 years ago)

Kuwaiti localflavor patch

  • django/contrib/localflavor/kw/forms.py

    diff -rupN django-trunk/django/contrib/localflavor/kw/forms.py django-trunk-speedy/django/contrib/localflavor/kw/forms.py
    old new  
     1"""
     2Kuwait-specific Form helpers
     3"""
     4
     5from django.forms import ValidationError
     6from django.forms.fields import Field, RegexField, EMPTY_VALUES
     7from django.utils.translation import gettext as _
     8import re
     9from datetime import date
     10
     11id_re = re.compile(r'^(?P<initial>\d{1})(?P<yy>\d\d)(?P<mm>\d\d)(?P<dd>\d\d)(?P<mid>\d{4})(?P<checksum>\d{1})')
     12
     13class KWCivilIDNumberField(Field):
     14    """
     15    Kuwaiti Civil ID numbers are 12 digits, second to seventh digits
     16    represents the person's birthdate.
     17
     18    Checks the following rules to determine the validty of the number:
     19        * The number consist of 12 digits.
     20        * The birthdate of the person is a valid date.
     21        * The calculated checksum equals to the last digit of the Civil ID.
     22    """
     23    default_error_messages = {
     24        'invalid': _(u'Enter a valid Kuwaiti Civil ID number'),
     25    }
     26
     27    def has_valid_checksum(self, value):
     28        weight = (2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2)
     29        calculated_checksum = 0
     30        for i in range(11):
     31            calculated_checksum += int(value[i]) * weight[i]
     32
     33        remainder = calculated_checksum % 11
     34        checkdigit = 11 - remainder
     35        if checkdigit != int(value[11]):
     36            return False
     37        return True
     38
     39    def clean(self, value):
     40        super(KWCivilIDNumberField, self).clean(value)
     41        if value in EMPTY_VALUES:
     42            return u''
     43
     44        if not re.match(r'^\d{12}$', value):
     45            raise ValidationError(self.error_messages['invalid'])       
     46
     47        match = re.match(id_re, value)
     48
     49        if not match:
     50            raise ValidationError(self.error_messages['invalid'])
     51
     52        gd = match.groupdict()
     53
     54        try:
     55            d = date(int(gd['yy']), int(gd['mm']), int(gd['dd']))
     56        except ValueError:
     57            raise ValidationError(self.error_messages['invalid'])
     58
     59        if not self.has_valid_checksum(value):
     60            raise ValidationError(self.error_messages['invalid'])
     61
     62        return value
  • docs/localflavor.txt

    diff -rupN django-trunk/docs/localflavor.txt django-trunk-speedy/docs/localflavor.txt
    old new Countries currently supported by ``local  
    4444    * India_
    4545    * Italy_
    4646    * Japan_
     47    * Kuwait_
    4748    * Mexico_
    4849    * Norway_
    4950    * Peru_
    them::  
    8384.. _India: `India (django.contrib.localflavor.in\_)`_
    8485.. _Italy: `Italy (django.contrib.localflavor.it)`_
    8586.. _Japan: `Japan (django.contrib.localflavor.jp)`_
     87.. _Kuwait: `Kuwait (django.contrib.localflavor.kw)`_
    8688.. _Mexico: `Mexico (django.contrib.localflavor.mx)`_
    8789.. _Norway: `Norway (django.contrib.localflavor.no)`_
    8890.. _Peru: `Peru (django.contrib.localflavor.pe)`_
    JPPrefectureSelect  
    422424
    423425A ``Select`` widget that uses a list of Japanese prefectures as its choices.
    424426
     427Kuwait (``django.contrib.localflavor.kw``)
     428==========================================
     429
     430KWCivilIDNumberField
     431--------------------
     432
     433A form field that validates input as a Kuwaiti Civil ID number. A valid
     434Civil ID number must obey the following rules:
     435
     436    * The number consist of 12 digits.
     437    * The birthdate of the person is a valid date.
     438    * The calculated checksum equals to the last digit of the Civil ID.
     439
    425440Mexico (``django.contrib.localflavor.mx``)
    426441==========================================
    427442
  • tests/regressiontests/forms/localflavor/kw.py

    diff -rupN django-trunk/tests/regressiontests/forms/localflavor/kw.py django-trunk-speedy/tests/regressiontests/forms/localflavor/kw.py
    old new  
     1# -*- coding: utf-8 -*-
     2# Tests for the contrib/localflavor/ KW form fields.
     3
     4tests = r"""
     5# KWCivilIDNumberField ########################################################
     6
     7>>> from django.contrib.localflavor.kw.forms import KWCivilIDNumberField
     8>>> f = KWCivilIDNumberField()
     9>>> f.clean('282040701483')
     10'282040701483'
     11>>> f.clean('289332013455')
     12Traceback (most recent call last):
     13...
     14ValidationError: [u'Enter a valid Kuwaiti Civil ID number']
     15"""
  • tests/regressiontests/forms/tests.py

    diff -rupN django-trunk/tests/regressiontests/forms/tests.py django-trunk-speedy/tests/regressiontests/forms/tests.py
    old new from localflavor.generic import tests as  
    1818from localflavor.is_ import tests as localflavor_is_tests
    1919from localflavor.it import tests as localflavor_it_tests
    2020from localflavor.jp import tests as localflavor_jp_tests
     21from localflavor.kw import tests as localflavor_kw_tests
    2122from localflavor.nl import tests as localflavor_nl_tests
    2223from localflavor.pl import tests as localflavor_pl_tests
    2324from localflavor.ro import tests as localflavor_ro_tests
    __test__ = {  
    5152    'localflavor_is_tests': localflavor_is_tests,
    5253    'localflavor_it_tests': localflavor_it_tests,
    5354    'localflavor_jp_tests': localflavor_jp_tests,
     55    'localflavor_kw_tests': localflavor_kw_tests,
    5456    'localflavor_nl_tests': localflavor_nl_tests,
    5557    'localflavor_pl_tests': localflavor_pl_tests,
    5658    'localflavor_ro_tests': localflavor_ro_tests,
Back to Top