|
0
|
1 |
""" |
|
|
2 |
Polish-specific form helpers |
|
|
3 |
""" |
|
|
4 |
|
|
|
5 |
import re |
|
|
6 |
|
|
|
7 |
from django.forms import ValidationError |
|
|
8 |
from django.forms.fields import Select, RegexField |
|
|
9 |
from django.utils.translation import ugettext_lazy as _ |
|
|
10 |
|
|
|
11 |
class PLProvinceSelect(Select): |
|
|
12 |
""" |
|
|
13 |
A select widget with list of Polish administrative provinces as choices. |
|
|
14 |
""" |
|
|
15 |
def __init__(self, attrs=None): |
|
|
16 |
from pl_voivodeships import VOIVODESHIP_CHOICES |
|
|
17 |
super(PLProvinceSelect, self).__init__(attrs, choices=VOIVODESHIP_CHOICES) |
|
|
18 |
|
|
|
19 |
class PLCountySelect(Select): |
|
|
20 |
""" |
|
|
21 |
A select widget with list of Polish administrative units as choices. |
|
|
22 |
""" |
|
|
23 |
def __init__(self, attrs=None): |
|
|
24 |
from pl_administrativeunits import ADMINISTRATIVE_UNIT_CHOICES |
|
|
25 |
super(PLCountySelect, self).__init__(attrs, choices=ADMINISTRATIVE_UNIT_CHOICES) |
|
|
26 |
|
|
|
27 |
class PLPESELField(RegexField): |
|
|
28 |
""" |
|
|
29 |
A form field that validates as Polish Identification Number (PESEL). |
|
|
30 |
|
|
|
31 |
Checks the following rules: |
|
|
32 |
* the length consist of 11 digits |
|
|
33 |
* has a valid checksum |
|
|
34 |
|
|
|
35 |
The algorithm is documented at http://en.wikipedia.org/wiki/PESEL. |
|
|
36 |
""" |
|
|
37 |
default_error_messages = { |
|
|
38 |
'invalid': _(u'National Identification Number consists of 11 digits.'), |
|
|
39 |
'checksum': _(u'Wrong checksum for the National Identification Number.'), |
|
|
40 |
} |
|
|
41 |
|
|
|
42 |
def __init__(self, *args, **kwargs): |
|
|
43 |
super(PLPESELField, self).__init__(r'^\d{11}$', |
|
|
44 |
max_length=None, min_length=None, *args, **kwargs) |
|
|
45 |
|
|
|
46 |
def clean(self,value): |
|
|
47 |
super(PLPESELField, self).clean(value) |
|
|
48 |
if not self.has_valid_checksum(value): |
|
|
49 |
raise ValidationError(self.error_messages['checksum']) |
|
|
50 |
return u'%s' % value |
|
|
51 |
|
|
|
52 |
def has_valid_checksum(self, number): |
|
|
53 |
""" |
|
|
54 |
Calculates a checksum with the provided algorithm. |
|
|
55 |
""" |
|
|
56 |
multiple_table = (1, 3, 7, 9, 1, 3, 7, 9, 1, 3, 1) |
|
|
57 |
result = 0 |
|
|
58 |
for i in range(len(number)): |
|
|
59 |
result += int(number[i]) * multiple_table[i] |
|
|
60 |
return result % 10 == 0 |
|
|
61 |
|
|
|
62 |
class PLNIPField(RegexField): |
|
|
63 |
""" |
|
|
64 |
A form field that validates as Polish Tax Number (NIP). |
|
|
65 |
Valid forms are: XXX-XXX-YY-YY or XX-XX-YYY-YYY. |
|
|
66 |
|
|
|
67 |
Checksum algorithm based on documentation at |
|
|
68 |
http://wipos.p.lodz.pl/zylla/ut/nip-rego.html |
|
|
69 |
""" |
|
|
70 |
default_error_messages = { |
|
|
71 |
'invalid': _(u'Enter a tax number field (NIP) in the format XXX-XXX-XX-XX or XX-XX-XXX-XXX.'), |
|
|
72 |
'checksum': _(u'Wrong checksum for the Tax Number (NIP).'), |
|
|
73 |
} |
|
|
74 |
|
|
|
75 |
def __init__(self, *args, **kwargs): |
|
|
76 |
super(PLNIPField, self).__init__(r'^\d{3}-\d{3}-\d{2}-\d{2}$|^\d{2}-\d{2}-\d{3}-\d{3}$', |
|
|
77 |
max_length=None, min_length=None, *args, **kwargs) |
|
|
78 |
|
|
|
79 |
def clean(self,value): |
|
|
80 |
super(PLNIPField, self).clean(value) |
|
|
81 |
value = re.sub("[-]", "", value) |
|
|
82 |
if not self.has_valid_checksum(value): |
|
|
83 |
raise ValidationError(self.error_messages['checksum']) |
|
|
84 |
return u'%s' % value |
|
|
85 |
|
|
|
86 |
def has_valid_checksum(self, number): |
|
|
87 |
""" |
|
|
88 |
Calculates a checksum with the provided algorithm. |
|
|
89 |
""" |
|
|
90 |
multiple_table = (6, 5, 7, 2, 3, 4, 5, 6, 7) |
|
|
91 |
result = 0 |
|
|
92 |
for i in range(len(number)-1): |
|
|
93 |
result += int(number[i]) * multiple_table[i] |
|
|
94 |
|
|
|
95 |
result %= 11 |
|
|
96 |
if result == int(number[-1]): |
|
|
97 |
return True |
|
|
98 |
else: |
|
|
99 |
return False |
|
|
100 |
|
|
|
101 |
class PLREGONField(RegexField): |
|
|
102 |
""" |
|
|
103 |
A form field that validates its input is a REGON number. |
|
|
104 |
|
|
|
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. |
|
|
107 |
""" |
|
|
108 |
default_error_messages = { |
|
|
109 |
'invalid': _(u'National Business Register Number (REGON) consists of 9 or 14 digits.'), |
|
|
110 |
'checksum': _(u'Wrong checksum for the National Business Register Number (REGON).'), |
|
|
111 |
} |
|
|
112 |
|
|
|
113 |
def __init__(self, *args, **kwargs): |
|
|
114 |
super(PLREGONField, self).__init__(r'^\d{9,14}$', |
|
|
115 |
max_length=None, min_length=None, *args, **kwargs) |
|
|
116 |
|
|
|
117 |
def clean(self,value): |
|
|
118 |
super(PLREGONField, self).clean(value) |
|
|
119 |
if not self.has_valid_checksum(value): |
|
|
120 |
raise ValidationError(self.error_messages['checksum']) |
|
|
121 |
return u'%s' % value |
|
|
122 |
|
|
|
123 |
def has_valid_checksum(self, number): |
|
|
124 |
""" |
|
|
125 |
Calculates a checksum with the provided algorithm. |
|
|
126 |
""" |
|
|
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 |
|
|
|
133 |
weights = [table for table in weights if len(table) == len(number)] |
|
|
134 |
|
|
|
135 |
for table in weights: |
|
|
136 |
checksum = sum([int(n) * w for n, w in zip(number, table)]) |
|
|
137 |
if checksum % 11 % 10: |
|
|
138 |
return False |
|
|
139 |
|
|
|
140 |
return bool(weights) |
|
|
141 |
|
|
|
142 |
class PLPostalCodeField(RegexField): |
|
|
143 |
""" |
|
|
144 |
A form field that validates as Polish postal code. |
|
|
145 |
Valid code is XX-XXX where X is digit. |
|
|
146 |
""" |
|
|
147 |
default_error_messages = { |
|
|
148 |
'invalid': _(u'Enter a postal code in the format XX-XXX.'), |
|
|
149 |
} |
|
|
150 |
|
|
|
151 |
def __init__(self, *args, **kwargs): |
|
|
152 |
super(PLPostalCodeField, self).__init__(r'^\d{2}-\d{3}$', |
|
|
153 |
max_length=None, min_length=None, *args, **kwargs) |