1 | Index: django/contrib/localflavor/pl/forms.py
|
---|
2 | ===================================================================
|
---|
3 | --- django/contrib/localflavor/pl/forms.py (wersja 6397)
|
---|
4 | +++ django/contrib/localflavor/pl/forms.py (kopia robocza)
|
---|
5 | @@ -6,6 +6,8 @@
|
---|
6 | from django.newforms.fields import Select, RegexField
|
---|
7 | from django.utils.translation import ugettext as _
|
---|
8 |
|
---|
9 | +import re
|
---|
10 | +
|
---|
11 | class PLVoivodeshipSelect(Select):
|
---|
12 | """
|
---|
13 | A select widget with list of Polish voivodeships (administrative provinces)
|
---|
14 | @@ -64,13 +66,85 @@
|
---|
15 | """
|
---|
16 | A form field that validates as Polish Tax Number (NIP).
|
---|
17 | Valid forms are: XXX-XXX-YY-YY or XX-XX-YYY-YYY.
|
---|
18 | +
|
---|
19 | + Checksum algorithm based on documentation at http://wipos.p.lodz.pl/zylla/ut/nip-rego.html
|
---|
20 | """
|
---|
21 | +
|
---|
22 | + def has_valid_checksum(self, number):
|
---|
23 | + """
|
---|
24 | + Calculates a checksum with the provided algorithm.
|
---|
25 | + """
|
---|
26 | + multiple_table = (6, 5, 7, 2, 3, 4, 5, 6, 7)
|
---|
27 | + result = 0
|
---|
28 | + for i in range(len(number)-1):
|
---|
29 | + result += int(number[i])*multiple_table[i]
|
---|
30 | +
|
---|
31 | + result %= 11
|
---|
32 | +
|
---|
33 | + if result == int(number[-1]):
|
---|
34 | + return True
|
---|
35 | + else:
|
---|
36 | + return False
|
---|
37 | +
|
---|
38 | def __init__(self, *args, **kwargs):
|
---|
39 | super(PLTaxNumberField, self).__init__(r'^\d{3}-\d{3}-\d{2}-\d{2}$|^\d{2}-\d{2}-\d{3}-\d{3}$',
|
---|
40 | max_length=None, min_length=None,
|
---|
41 | error_message=_(u'Enter a tax number field (NIP) in the format XXX-XXX-XX-XX or XX-XX-XXX-XXX.'), *args, **kwargs)
|
---|
42 |
|
---|
43 | + def clean(self,value):
|
---|
44 | + super(PLTaxNumberField, self).clean(value)
|
---|
45 | + value = re.sub("[-]","",value)
|
---|
46 | + if not self.has_valid_checksum(value):
|
---|
47 | + raise ValidationError(_(u'Wrong checksum for the Tax Number (NIP).'))
|
---|
48 | + return u'%s' % value
|
---|
49 |
|
---|
50 | +class PLNationalBusinessRegisterField(RegexField):
|
---|
51 | + """
|
---|
52 | + A form field that validated as Polish National Official Business Register Number (REGON)
|
---|
53 | + Valid forms are: 7 or 9 digits number
|
---|
54 | +
|
---|
55 | + More on the field: http://www.stat.gov.pl/bip/regon_ENG_HTML.htm
|
---|
56 | +
|
---|
57 | + The checksum algorithm is documented at http://wipos.p.lodz.pl/zylla/ut/nip-rego.html
|
---|
58 | + """
|
---|
59 | +
|
---|
60 | + def has_valid_checksum(self, number):
|
---|
61 | + """
|
---|
62 | + Calculates a checksum with the provided algorithm.
|
---|
63 | + """
|
---|
64 | + multiple_table_7 = (2, 3, 4, 5, 6, 7)
|
---|
65 | + multiple_table_9 = (8, 9, 2, 3, 4, 5, 6, 7)
|
---|
66 | + result = 0
|
---|
67 | +
|
---|
68 | + if len(number) == 7:
|
---|
69 | + multiple_table = multiple_table_7
|
---|
70 | + else:
|
---|
71 | + multiple_table = multiple_table_9
|
---|
72 | +
|
---|
73 | + for i in range(len(number)-1):
|
---|
74 | + result += int(number[i])*multiple_table[i]
|
---|
75 | +
|
---|
76 | + result %= 11
|
---|
77 | +
|
---|
78 | + if result == 10:
|
---|
79 | + result = 0
|
---|
80 | +
|
---|
81 | + if result == int(number[-1]):
|
---|
82 | + return True
|
---|
83 | + else:
|
---|
84 | + return False
|
---|
85 | +
|
---|
86 | + def __init__(self, *args, **kwargs):
|
---|
87 | + super(PLNationalBusinessRegisterField, self).__init__(r'^\d{7,9}$',
|
---|
88 | + max_length=None, min_length=None, error_message=_(u'National Business Register Number (REGON) consists of 7 or 9 digits.'),
|
---|
89 | + *args, **kwargs)
|
---|
90 | +
|
---|
91 | + def clean(self,value):
|
---|
92 | + super(PLNationalBusinessRegisterField, self).clean(value)
|
---|
93 | + if not self.has_valid_checksum(value):
|
---|
94 | + raise ValidationError(_(u'Wrong checksum for the National Business Register Number (REGON).'))
|
---|
95 | + return u'%s' % value
|
---|
96 | +
|
---|
97 | class PLPostalCodeField(RegexField):
|
---|
98 | """
|
---|
99 | A form field that validates as Polish postal code.
|
---|
100 | Index: tests/regressiontests/forms/localflavor/pl.py
|
---|
101 | ===================================================================
|
---|
102 | --- tests/regressiontests/forms/localflavor/pl.py (wersja 6397)
|
---|
103 | +++ tests/regressiontests/forms/localflavor/pl.py (kopia robocza)
|
---|
104 | @@ -35,11 +35,15 @@
|
---|
105 | Traceback (most recent call last):
|
---|
106 | ...
|
---|
107 | ValidationError: [u'Enter a tax number field (NIP) in the format XXX-XXX-XX-XX or XX-XX-XXX-XXX.']
|
---|
108 | ->>> f.clean('43-34-234-323')
|
---|
109 | -u'43-34-234-323'
|
---|
110 | ->>> f.clean('433-344-24-23')
|
---|
111 | -u'433-344-24-23'
|
---|
112 | -
|
---|
113 | +>>> f.clean('64-62-414-124')
|
---|
114 | +u'6462414124'
|
---|
115 | +>>> f.clean('646-241-41-24')
|
---|
116 | +u'6462414124'
|
---|
117 | +>>> f.clean('646-241-41-23')
|
---|
118 | +Traceback (most recent call last):
|
---|
119 | +...
|
---|
120 | +ValidationError: [u'Wrong checksum for the Tax Number (NIP).']
|
---|
121 | +
|
---|
122 | # PLNationalIdentificationNumberField ############################################
|
---|
123 |
|
---|
124 | >>> from django.contrib.localflavor.pl.forms import PLNationalIdentificationNumberField
|
---|
125 | @@ -58,4 +62,20 @@
|
---|
126 | Traceback (most recent call last):
|
---|
127 | ...
|
---|
128 | ValidationError: [u'National Identification Number consists of 11 digits.']
|
---|
129 | +
|
---|
130 | +# PLNationalBusinessRegisterField ################################################
|
---|
131 | +
|
---|
132 | +>>> from django.contrib.localflavor.pl.forms import PLNationalBusinessRegisterField
|
---|
133 | +>>> f = PLNationalBusinessRegisterField()
|
---|
134 | +>>> f.clean('590096454')
|
---|
135 | +u'590096454'
|
---|
136 | +>>> f.clean('590096453')
|
---|
137 | +Traceback (most recent call last):
|
---|
138 | +...
|
---|
139 | +ValidationError: [u'Wrong checksum for the National Business Register Number (REGON).']
|
---|
140 | +>>> f.clean('590096')
|
---|
141 | +Traceback (most recent call last):
|
---|
142 | +...
|
---|
143 | +ValidationError: [u'National Business Register Number (REGON) consists of 7 or 9 digits.']
|
---|
144 | +
|
---|
145 | """
|
---|