Ticket #8515: regon-validation.patch
File regon-validation.patch, 3.8 KB (added by , 16 years ago) |
---|
-
django/contrib/localflavor/pl/forms.py
100 100 101 101 class PLREGONField(RegexField): 102 102 """ 103 A form field that validated as Polish National Official Business Register 104 Number (REGON). Valid numbers contain 7 or 9 digits. 103 A form field that validates its input is a REGON number. 105 104 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 105 Valid regon number consists of 9 or 14 digits. 106 See http://www.stat.gov.pl/bip/regon_ENG_HTML.htm for more information. 109 107 """ 110 108 default_error_messages = { 111 'invalid': _(u'National Business Register Number (REGON) consists of 7 or 9digits.'),109 'invalid': _(u'National Business Register Number (REGON) consists of 9 or 14 digits.'), 112 110 'checksum': _(u'Wrong checksum for the National Business Register Number (REGON).'), 113 111 } 114 112 115 113 def __init__(self, *args, **kwargs): 116 super(PLREGONField, self).__init__(r'^\d{ 7,9}$',114 super(PLREGONField, self).__init__(r'^\d{9,14}$', 117 115 max_length=None, min_length=None, *args, **kwargs) 118 116 119 117 def clean(self,value): 120 118 super(PLREGONField, self).clean(value) 121 119 if not self.has_valid_checksum(value): 122 120 raise ValidationError(self.error_messages['checksum']) 123 return u '%s' % value121 return unicode(value) 124 122 125 123 def has_valid_checksum(self, number): 126 124 """ 127 125 Calculates a checksum with the provided algorithm. 128 126 """ 129 multiple_table_7 = (2, 3, 4, 5, 6, 7) 130 multiple_table_9 = (8, 9, 2, 3, 4, 5, 6, 7) 131 result = 0 127 weights = ( 128 (8, 9, 2, 3, 4, 5, 6, 7, -1), 129 (2, 4, 8, 5, 0, 9, 7, 3, 6, 1, 2, 4, 8, -1), 130 (8, 9, 2, 3, 4, 5, 6, 7, -1, 0, 0, 0, 0, 0), 131 ) 132 132 133 if len(number) == 7: 134 multiple_table = multiple_table_7 135 else: 136 multiple_table = multiple_table_9 133 weights = [table for table in weights if len(table) == len(number)] 137 134 138 for i in range(len(number)-1): 139 result += int(number[i]) * multiple_table[i] 135 for table in weights: 136 checksum = sum([int(n) * w for n, w in zip(number, table)]) 137 if checksum % 11 % 10 != 0: 138 return False 140 139 141 result %= 11 142 if result == 10: 143 result = 0 144 if result == int(number[-1]): 145 return True 146 else: 147 return False 140 return bool(weights) 148 141 149 142 class PLPostalCodeField(RegexField): 150 143 """ -
tests/regressiontests/forms/localflavor/pl.py
67 67 68 68 >>> from django.contrib.localflavor.pl.forms import PLREGONField 69 69 >>> f = PLREGONField() 70 >>> f.clean('12345678512347') 71 u'12345678512347' 70 72 >>> f.clean('590096454') 71 73 u'590096454' 74 >>> f.clean('123456784') 75 Traceback (most recent call last): 76 ... 77 ValidationError: [u'Wrong checksum for the National Business Register Number (REGON).'] 78 >>> f.clean('12345678412342') 79 Traceback (most recent call last): 80 ... 81 ValidationError: [u'Wrong checksum for the National Business Register Number (REGON).'] 72 82 >>> f.clean('590096453') 73 83 Traceback (most recent call last): 74 84 ... … … 76 86 >>> f.clean('590096') 77 87 Traceback (most recent call last): 78 88 ... 79 ValidationError: [u'National Business Register Number (REGON) consists of 7 or 9digits.']89 ValidationError: [u'National Business Register Number (REGON) consists of 9 or 14 digits.'] 80 90 81 91 """