Ticket #8068: localflavor_kw.patch
File localflavor_kw.patch, 5.6 KB (added by , 16 years ago) |
---|
-
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 """ 2 Kuwait-specific Form helpers 3 """ 4 5 from django.forms import ValidationError 6 from django.forms.fields import Field, RegexField, EMPTY_VALUES 7 from django.utils.translation import gettext as _ 8 import re 9 from datetime import date 10 11 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})') 12 13 class 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 44 44 * India_ 45 45 * Italy_ 46 46 * Japan_ 47 * Kuwait_ 47 48 * Mexico_ 48 49 * Norway_ 49 50 * Peru_ … … them:: 83 84 .. _India: `India (django.contrib.localflavor.in\_)`_ 84 85 .. _Italy: `Italy (django.contrib.localflavor.it)`_ 85 86 .. _Japan: `Japan (django.contrib.localflavor.jp)`_ 87 .. _Kuwait: `Kuwait (django.contrib.localflavor.kw)`_ 86 88 .. _Mexico: `Mexico (django.contrib.localflavor.mx)`_ 87 89 .. _Norway: `Norway (django.contrib.localflavor.no)`_ 88 90 .. _Peru: `Peru (django.contrib.localflavor.pe)`_ … … JPPrefectureSelect 422 424 423 425 A ``Select`` widget that uses a list of Japanese prefectures as its choices. 424 426 427 Kuwait (``django.contrib.localflavor.kw``) 428 ========================================== 429 430 KWCivilIDNumberField 431 -------------------- 432 433 A form field that validates input as a Kuwaiti Civil ID number. A valid 434 Civil 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 425 440 Mexico (``django.contrib.localflavor.mx``) 426 441 ========================================== 427 442 -
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 4 tests = 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') 12 Traceback (most recent call last): 13 ... 14 ValidationError: [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 18 18 from localflavor.is_ import tests as localflavor_is_tests 19 19 from localflavor.it import tests as localflavor_it_tests 20 20 from localflavor.jp import tests as localflavor_jp_tests 21 from localflavor.kw import tests as localflavor_kw_tests 21 22 from localflavor.nl import tests as localflavor_nl_tests 22 23 from localflavor.pl import tests as localflavor_pl_tests 23 24 from localflavor.ro import tests as localflavor_ro_tests … … __test__ = { 51 52 'localflavor_is_tests': localflavor_is_tests, 52 53 'localflavor_it_tests': localflavor_it_tests, 53 54 'localflavor_jp_tests': localflavor_jp_tests, 55 'localflavor_kw_tests': localflavor_kw_tests, 54 56 'localflavor_nl_tests': localflavor_nl_tests, 55 57 'localflavor_pl_tests': localflavor_pl_tests, 56 58 'localflavor_ro_tests': localflavor_ro_tests,