Ticket #5200: pl-localflavor-0.3.diff
File pl-localflavor-0.3.diff, 4.9 KB (added by , 17 years ago) |
---|
-
django/contrib/localflavor/pl/forms.py
6 6 from django.newforms.fields import Select, RegexField 7 7 from django.utils.translation import ugettext as _ 8 8 9 import re 10 9 11 class PLVoivodeshipSelect(Select): 10 12 """ 11 13 A select widget with list of Polish voivodeships (administrative provinces) … … 64 66 """ 65 67 A form field that validates as Polish Tax Number (NIP). 66 68 Valid forms are: XXX-XXX-YY-YY or XX-XX-YYY-YYY. 69 70 Checksum algorithm based on documentation at http://wipos.p.lodz.pl/zylla/ut/nip-rego.html 67 71 """ 72 73 def has_valid_checksum(self, number): 74 """ 75 Calculates a checksum with the provided algorithm. 76 """ 77 multiple_table = (6, 5, 7, 2, 3, 4, 5, 6, 7) 78 result = 0 79 for i in range(len(number)-1): 80 result += int(number[i])*multiple_table[i] 81 82 result %= 11 83 84 if result == int(number[-1]): 85 return True 86 else: 87 return False 88 68 89 def __init__(self, *args, **kwargs): 69 90 super(PLTaxNumberField, self).__init__(r'^\d{3}-\d{3}-\d{2}-\d{2}$|^\d{2}-\d{2}-\d{3}-\d{3}$', 70 91 max_length=None, min_length=None, 71 92 error_message=_(u'Enter a tax number field (NIP) in the format XXX-XXX-XX-XX or XX-XX-XXX-XXX.'), *args, **kwargs) 72 93 94 def clean(self,value): 95 super(PLTaxNumberField, self).clean(value) 96 value = re.sub("[-]","",value) 97 if not self.has_valid_checksum(value): 98 raise ValidationError(_(u'Wrong checksum for the Tax Number (NIP).')) 99 return u'%s' % value 73 100 101 class PLNationalBusinessRegisterField(RegexField): 102 """ 103 A form field that validated as Polish National Official Business Register Number (REGON) 104 Valid forms are: 105 106 More on the field: http://www.stat.gov.pl/bip/regon_ENG_HTML.htm 107 108 The checksum algorithm is documented at http://wipos.p.lodz.pl/zylla/ut/nip-rego.html 109 """ 110 111 def has_valid_checksum(self, number): 112 """ 113 Calculates a checksum with the provided algorithm. 114 """ 115 multiple_table_7 = (2, 3, 4, 5, 6, 7) 116 multiple_table_9 = (8, 9, 2, 3, 4, 5, 6, 7) 117 result = 0 118 119 if len(number) == 7: 120 multiple_table = multiple_table_7 121 else: 122 multiple_table = multiple_table_9 123 124 for i in range(len(number)-1): 125 result += int(number[i])*multiple_table[i] 126 127 result %= 11 128 129 if result == 10: 130 result = 0 131 132 if result == int(number[-1]): 133 return True 134 else: 135 return False 136 137 def __init__(self, *args, **kwargs): 138 super(PLNationalBusinessRegisterField, self).__init__(r'^\d{7,9}$', 139 max_length=None, min_length=None, error_message=_(u'National Business Register Number (REGON) consists of 7 or 9 digits.'), 140 *args, **kwargs) 141 142 def clean(self,value): 143 super(PLNationalBusinessRegisterField, self).clean(value) 144 if not self.has_valid_checksum(value): 145 raise ValidationError(_(u'Wrong checksum for the National Business Register Number (REGON).')) 146 return u'%s' % value 147 74 148 class PLPostalCodeField(RegexField): 75 149 """ 76 150 A form field that validates as Polish postal code. -
tests/regressiontests/forms/localflavor.py
1423 1423 Traceback (most recent call last): 1424 1424 ... 1425 1425 ValidationError: [u'Enter a tax number field (NIP) in the format XXX-XXX-XX-XX or XX-XX-XXX-XXX.'] 1426 >>> f.clean('43-34-234-323') 1427 u'43-34-234-323' 1428 >>> f.clean('433-344-24-23') 1429 u'433-344-24-23' 1426 >>> f.clean('64-62-414-124') 1427 u'6462414124' 1428 >>> f.clean('646-241-41-24') 1429 u'6462414124' 1430 >>> f.clean('646-241-41-23') 1431 Traceback (most recent call last): 1432 ... 1433 ValidationError: [u'Wrong checksum for the Tax Number (NIP).'] 1430 1434 1431 1435 # PLNationalIdentificationNumberField ############################################ 1432 1436 … … 1446 1450 Traceback (most recent call last): 1447 1451 ... 1448 1452 ValidationError: [u'National Identification Number consists of 11 digits.'] 1453 >>>>>>> .r5947 1454 1455 1456 # PLNationalBusinessRegisterField ################################################ 1457 1458 >>> from django.contrib.localflavor.pl.forms import PLNationalBusinessRegisterField 1459 >>> f = PLNationalBusinessRegisterField() 1460 >>> f.clean('590096454') 1461 u'590096454' 1462 >>> f.clean('590096453') 1463 Traceback (most recent call last): 1464 ... 1465 ValidationError: [u'Wrong checksum for the National Business Register Number (REGON).'] 1466 >>> f.clean('590096') 1467 Traceback (most recent call last): 1468 ... 1469 ValidationError: [u'National Business Register Number (REGON) consists of 7 or 9 digits.'] 1470 1449 1471 """