| 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 validated as Polish National Official Business Register |
|---|
| 104 |
Number (REGON). Valid numbers contain 7 or 9 digits. |
|---|
| 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 |
default_error_messages = { |
|---|
| 111 |
'invalid': _(u'National Business Register Number (REGON) consists of 7 or 9 digits.'), |
|---|
| 112 |
'checksum': _(u'Wrong checksum for the National Business Register Number (REGON).'), |
|---|
| 113 |
} |
|---|
| 114 |
|
|---|
| 115 |
def __init__(self, *args, **kwargs): |
|---|
| 116 |
super(PLREGONField, self).__init__(r'^\d{7,9}$', |
|---|
| 117 |
max_length=None, min_length=None, *args, **kwargs) |
|---|
| 118 |
|
|---|
| 119 |
def clean(self,value): |
|---|
| 120 |
super(PLREGONField, self).clean(value) |
|---|
| 121 |
if not self.has_valid_checksum(value): |
|---|
| 122 |
raise ValidationError(self.error_messages['checksum']) |
|---|
| 123 |
return u'%s' % value |
|---|
| 124 |
|
|---|
| 125 |
def has_valid_checksum(self, number): |
|---|
| 126 |
""" |
|---|
| 127 |
Calculates a checksum with the provided algorithm. |
|---|
| 128 |
""" |
|---|
| 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 |
|---|
| 132 |
|
|---|
| 133 |
if len(number) == 7: |
|---|
| 134 |
multiple_table = multiple_table_7 |
|---|
| 135 |
else: |
|---|
| 136 |
multiple_table = multiple_table_9 |
|---|
| 137 |
|
|---|
| 138 |
for i in range(len(number)-1): |
|---|
| 139 |
result += int(number[i]) * multiple_table[i] |
|---|
| 140 |
|
|---|
| 141 |
result %= 11 |
|---|
| 142 |
if result == 10: |
|---|
| 143 |
result = 0 |
|---|
| 144 |
if result == int(number[-1]): |
|---|
| 145 |
return True |
|---|
| 146 |
else: |
|---|
| 147 |
return False |
|---|
| 148 |
|
|---|
| 149 |
class PLPostalCodeField(RegexField): |
|---|
| 150 |
""" |
|---|
| 151 |
A form field that validates as Polish postal code. |
|---|
| 152 |
Valid code is XX-XXX where X is digit. |
|---|
| 153 |
""" |
|---|
| 154 |
default_error_messages = { |
|---|
| 155 |
'invalid': _(u'Enter a postal code in the format XX-XXX.'), |
|---|
| 156 |
} |
|---|
| 157 |
|
|---|
| 158 |
def __init__(self, *args, **kwargs): |
|---|
| 159 |
super(PLPostalCodeField, self).__init__(r'^\d{2}-\d{3}$', |
|---|
| 160 |
max_length=None, min_length=None, *args, **kwargs) |
|---|