Fixed #8068 - Added Kuwaiti (kw) localflavor. Thanks to Ahmad Al-Ibrahim for providing the patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12042 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jannis Leidel 2010-01-01 21:32:24 +00:00
parent 3825bb2350
commit 82b33b1d80
6 changed files with 93 additions and 0 deletions

View File

@ -31,6 +31,7 @@ answer newbie questions, and generally made Django that much better:
Andi Albrecht <albrecht.andi@gmail.com>
Marty Alchin <gulopine@gamemusic.org>
Ahmad Alhashemi <trans@ahmadh.com>
Ahmad Al-Ibrahim
Daniel Alves Barbosa de Oliveira Vaz <danielvaz@gmail.com>
AgarFu <heaven@croasanaso.sytes.net>
Dagur Páll Ammendrup <dagurp@gmail.com>

View File

@ -0,0 +1,61 @@
"""
Kuwait-specific Form helpers
"""
import re
from datetime import date
from django.forms import ValidationError
from django.forms.fields import Field, RegexField, EMPTY_VALUES
from django.utils.translation import gettext as _
id_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})')
class KWCivilIDNumberField(Field):
"""
Kuwaiti Civil ID numbers are 12 digits, second to seventh digits
represents the person's birthdate.
Checks the following rules to determine the validty of the number:
* The number consist of 12 digits.
* The birthdate of the person is a valid date.
* The calculated checksum equals to the last digit of the Civil ID.
"""
default_error_messages = {
'invalid': _('Enter a valid Kuwaiti Civil ID number'),
}
def has_valid_checksum(self, value):
weight = (2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2)
calculated_checksum = 0
for i in range(11):
calculated_checksum += int(value[i]) * weight[i]
remainder = calculated_checksum % 11
checkdigit = 11 - remainder
if checkdigit != int(value[11]):
return False
return True
def clean(self, value):
super(KWCivilIDNumberField, self).clean(value)
if value in EMPTY_VALUES:
return u''
if not re.match(r'^\d{12}$', value):
raise ValidationError(self.error_messages['invalid'])
match = re.match(id_re, value)
if not match:
raise ValidationError(self.error_messages['invalid'])
gd = match.groupdict()
try:
d = date(int(gd['yy']), int(gd['mm']), int(gd['dd']))
except ValueError:
raise ValidationError(self.error_messages['invalid'])
if not self.has_valid_checksum(value):
raise ValidationError(self.error_messages['invalid'])
return value

View File

@ -52,6 +52,7 @@ Countries currently supported by :mod:`~django.contrib.localflavor` are:
* India_
* Italy_
* Japan_
* Kuwait_
* Mexico_
* `The Netherlands`_
* Norway_
@ -95,6 +96,7 @@ Here's an example of how to use them::
.. _India: `India (in\_)`_
.. _Italy: `Italy (it)`_
.. _Japan: `Japan (jp)`_
.. _Kuwait: `Kuwait (kw)`_
.. _Mexico: `Mexico (mx)`_
.. _Norway: `Norway (no)`_
.. _Peru: `Peru (pe)`_
@ -410,6 +412,18 @@ Japan (``jp``)
A ``Select`` widget that uses a list of Japanese prefectures as its choices.
Kuwait (``kw``)
===============
.. class:: kw.forms.KWCivilIDNumberField
A form field that validates input as a Kuwaiti Civil ID number. A valid
Civil ID number must obey the following rules:
* The number consist of 12 digits.
* The birthdate of the person is a valid date.
* The calculated checksum equals to the last digit of the Civil ID.
Mexico (``mx``)
===============

View File

@ -0,0 +1,15 @@
# -*- coding: utf-8 -*-
# Tests for the contrib/localflavor/ KW form fields.
tests = r"""
# KWCivilIDNumberField ########################################################
>>> from django.contrib.localflavor.kw.forms import KWCivilIDNumberField
>>> f = KWCivilIDNumberField()
>>> f.clean('282040701483')
'282040701483'
>>> f.clean('289332013455')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid Kuwaiti Civil ID number']
"""

View File

@ -18,6 +18,7 @@ from localflavor.generic import tests as localflavor_generic_tests
from localflavor.is_ import tests as localflavor_is_tests
from localflavor.it import tests as localflavor_it_tests
from localflavor.jp import tests as localflavor_jp_tests
from localflavor.kw import tests as localflavor_kw_tests
from localflavor.nl import tests as localflavor_nl_tests
from localflavor.pl import tests as localflavor_pl_tests
from localflavor.ro import tests as localflavor_ro_tests
@ -55,6 +56,7 @@ __test__ = {
'localflavor_is_tests': localflavor_is_tests,
'localflavor_it_tests': localflavor_it_tests,
'localflavor_jp_tests': localflavor_jp_tests,
'localflavor_kw_tests': localflavor_kw_tests,
'localflavor_nl_tests': localflavor_nl_tests,
'localflavor_pl_tests': localflavor_pl_tests,
'localflavor_ro_tests': localflavor_ro_tests,