Ticket #14400: django-lv.diff
File django-lv.diff, 8.2 KB (added by , 14 years ago) |
---|
-
django/contrib/localflavor/lv/models.py
1 # needed for some testing, see ticket #7198 -
django/contrib/localflavor/lv/forms.py
1 # encoding: utf-8 2 import time 3 import re 4 5 from django.forms import ValidationError 6 from django.forms.fields import CharField, RegexField 7 from django.utils.translation import ugettext_lazy as _ 8 9 identity_number_digits_re = re.compile(r'^(\d{11})$') 10 11 class LVPhoneField(RegexField): 12 13 default_error_messages = { 14 'invalid': _('Phone numbers must be in XXXXXXXX format.'), 15 } 16 def __init__(self, *args, **kwargs): 17 super(LVPhoneField, self).__init__(r'^(\d{8})$', 18 max_length=None, min_length=None, *args, **kwargs) 19 20 class LVPostalCodeField(RegexField): 21 default_error_messages = { 22 'invalid' : _('Postal code fields must be in LV-XXXX format') 23 } 24 def __init__(self, *args, **kwargs): 25 super(LVPostalCodeField, self).__init__(r'LV-(\d{4})$', 26 max_length = None, min_length = None, *args, **kwargs) 27 28 class LVIdentityNumberField(CharField): 29 """ 30 Latvian identity number field. 31 Checks following rules that number is valid: 32 - first 6 digits form valid date 33 - conforms form XXXXXX-XXXXX 34 - last digit is valid checksum 35 algorithm for validation 36 (1101 – (1*PK[1] + 6*PK[2] + 3*PK[3] + 7*PK[4] + 9*PK[5] + 10*PK[6] + 5*PK[7] + 8*PK[8] + 4*PK[9] + 2*PK[10])) mod 11 37 """ 38 default_error_messages = { 39 'invalid': _('Identity numbers must be in XXXXXX-XXXXX format') 40 } 41 def clean(self, value): 42 super(LVIdentityNumberField, self).clean(value) 43 date = value[:6] 44 meta = value[7:] 45 value = date+ meta 46 if not identity_number_digits_re.match(value): 47 raise ValidationError(self.error_messages['invalid']) 48 try: 49 time.strptime(date, "%d%m%y") 50 except ValueError: 51 raise ValidationError(self.error_messages['invalid']) 52 def calculate_checksum(code): 53 """ common knownledge algorithm for calculating identity number checksum """ 54 checksum = 0 55 validation_data = [1,6,3,7,9,10,5,8,4,2] 56 for i in xrange(0, 10): 57 checksum += int(code[i]) * validation_data[i] 58 checksum = (1101 - checksum) % 11 59 return checksum 60 if calculate_checksum(value) != int(value[10]): 61 raise ValidationError(self.error_messages['invalid']) 62 return value -
tests/regressiontests/forms/tests.py
31 31 from localflavor.us import tests as localflavor_us_tests 32 32 from localflavor.uy import tests as localflavor_uy_tests 33 33 from localflavor.za import tests as localflavor_za_tests 34 from localflavor.lv import * 34 35 from regressions import tests as regression_tests 35 36 from util import tests as util_tests 36 37 from widgets import tests as widgets_tests -
tests/regressiontests/forms/localflavor/lv.py
1 import unittest 2 from django.forms import ValidationError 3 from django.contrib.localflavor.lv.forms import LVPhoneField 4 from django.contrib.localflavor.lv.forms import LVPostalCodeField 5 from django.contrib.localflavor.lv.forms import LVIdentityNumberField 6 7 class LVPhoneFieldTestCase(unittest.TestCase): 8 """ test LVPhone field """ 9 def test_valid(self): 10 """ test valid phone number """ 11 field = LVPhoneField() 12 self.assertEquals(field.clean("27175629"), "27175629") 13 def test_invalid(self): 14 """ test invalid phone number """ 15 field = LVPhoneField() 16 self.assertRaises(ValidationError, field.clean, "7175629") 17 18 19 class LVIdentityNumberFieldTestCase(unittest.TestCase): 20 def setUp(self): 21 self.field = LVIdentityNumberField() 22 def test_valid(self): 23 """ 24 test valid identity number field un publicly known identity number of 25 Aivars Lembergs 26 """ 27 self.assertEquals(self.field.clean("260953-11667"), "26095311667") 28 def test_with_invalid_date_and_valid_checksum(self): 29 """ test with invalid date and valid checksum """ 30 self.assertRaises(ValidationError, self.field.clean, "300290-11545") 31 def test_with_valid_date_and_invalid_checksum(self): 32 """ ValidationError on valid date and invalid checksum """ 33 self.assertRaises(ValidationError, self.field.clean, "251278-11634") 34 def test_with_characters_inside(self): 35 """ test that identity should fail if it contains characters """ 36 self.assertRaises(ValidationError, self.field.clean, "abcdef-12145") 37 def test_length_validation(self): 38 self.assertRaises(ValidationError, self.field.clean, "080690-111711") 39 self.assertRaises(ValidationError, self.field.clean, "080690-1177") 40 41 class LVPostalCodeFieldTestCase(unittest.TestCase): 42 def setUp(self): 43 self.field = LVPostalCodeField() 44 def test_with_invalid_prefix(self): 45 """ all postal code`s have prefix LV """ 46 self.assertRaises(ValidationError, self.field.clean, "LR-1024") 47 def test_with_invalid_code(self): 48 """ all postal codes contain prefix, dash and numbers """ 49 self.assertRaises(ValidationError, self.field.clean, "LV-10S3") 50 def test_with_invalid_digits(self): 51 """ there is just 4 digits """ 52 self.assertRaises(ValidationError, self.field.clean, "LV-10244") 53 self.assertRaises(ValidationError, self.field.clean, "LV-102") 54 def test_valid(self): 55 self.assertEqual(self.field.clean("LV-1024"), "LV-1024") 56 -
AUTHORS
517 517 Gasper Zejn <zejn@kiberpipa.org> 518 518 Jarek Zgoda <jarek.zgoda@gmail.com> 519 519 Cheng Zhang 520 Kristaps Kulis <Kristaps.Kulis@Gmail.com> 520 521 521 522 A big THANK YOU goes to: 522 523 -
docs/ref/contrib/localflavor.txt
53 53 * Italy_ 54 54 * Japan_ 55 55 * Kuwait_ 56 * Latvia_ 56 57 * Mexico_ 57 58 * `The Netherlands`_ 58 59 * Norway_ … … 100 101 .. _Italy: `Italy (it)`_ 101 102 .. _Japan: `Japan (jp)`_ 102 103 .. _Kuwait: `Kuwait (kw)`_ 104 .. _Latvia: `Latvia (lv)`_ 103 105 .. _Mexico: `Mexico (mx)`_ 104 106 .. _Norway: `Norway (no)`_ 105 107 .. _Peru: `Peru (pe)`_ … … 468 470 * The birthdate of the person is a valid date. 469 471 * The calculated checksum equals to the last digit of the Civil ID. 470 472 473 Latvia (``lv``) 474 =============== 475 .. class:: lv.forms.LVPhoneField 476 477 A form field that validates input as Latvian phone number. A valid phone 478 number must consist of 8 digits 479 480 .. class:: lv.forms.LVPostalCodeField 481 482 A form field that validates input as Latvian postal code number. A valid 483 postal code number must start with LV- and contain 4 digits 484 485 .. class:: lv.forms.LVIdentityNumberField 486 487 A form field that validates input as Latvian identity number. A valid identity 488 number must obey the following rules: 489 490 * The number starts with 6 digits, followed by dash and then 5 digits 491 * The first 6 digits form valid birthdate of person 492 * The last digit equals to calculated checksum 493 471 494 Mexico (``mx``) 472 495 =============== 473 496