2007-08-19 15:38:39 +08:00
|
|
|
"""
|
|
|
|
Polish-specific form helpers
|
|
|
|
"""
|
|
|
|
|
2007-10-20 17:24:19 +08:00
|
|
|
import re
|
|
|
|
|
2008-07-19 09:22:26 +08:00
|
|
|
from django.forms import ValidationError
|
|
|
|
from django.forms.fields import Select, RegexField
|
2008-06-18 21:10:05 +08:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2007-08-19 15:38:39 +08:00
|
|
|
|
2008-08-14 12:29:02 +08:00
|
|
|
class PLProvinceSelect(Select):
|
2007-08-19 15:38:39 +08:00
|
|
|
"""
|
2008-08-14 12:29:02 +08:00
|
|
|
A select widget with list of Polish administrative provinces as choices.
|
2007-08-19 15:38:39 +08:00
|
|
|
"""
|
|
|
|
def __init__(self, attrs=None):
|
|
|
|
from pl_voivodeships import VOIVODESHIP_CHOICES
|
2008-08-14 12:29:02 +08:00
|
|
|
super(PLProvinceSelect, self).__init__(attrs, choices=VOIVODESHIP_CHOICES)
|
2007-08-19 15:38:39 +08:00
|
|
|
|
2008-08-28 07:05:25 +08:00
|
|
|
class PLCountySelect(Select):
|
2007-08-19 15:38:39 +08:00
|
|
|
"""
|
|
|
|
A select widget with list of Polish administrative units as choices.
|
|
|
|
"""
|
|
|
|
def __init__(self, attrs=None):
|
|
|
|
from pl_administrativeunits import ADMINISTRATIVE_UNIT_CHOICES
|
2008-08-28 07:05:25 +08:00
|
|
|
super(PLCountySelect, self).__init__(attrs, choices=ADMINISTRATIVE_UNIT_CHOICES)
|
2007-08-19 15:38:39 +08:00
|
|
|
|
2008-08-14 12:29:02 +08:00
|
|
|
class PLPESELField(RegexField):
|
2007-08-19 15:38:39 +08:00
|
|
|
"""
|
|
|
|
A form field that validates as Polish Identification Number (PESEL).
|
|
|
|
|
|
|
|
Checks the following rules:
|
|
|
|
* the length consist of 11 digits
|
|
|
|
* has a valid checksum
|
|
|
|
|
|
|
|
The algorithm is documented at http://en.wikipedia.org/wiki/PESEL.
|
|
|
|
"""
|
2007-12-17 16:05:27 +08:00
|
|
|
default_error_messages = {
|
|
|
|
'invalid': _(u'National Identification Number consists of 11 digits.'),
|
|
|
|
'checksum': _(u'Wrong checksum for the National Identification Number.'),
|
|
|
|
}
|
2007-08-19 15:38:39 +08:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2008-08-14 12:29:02 +08:00
|
|
|
super(PLPESELField, self).__init__(r'^\d{11}$',
|
2007-12-17 16:05:27 +08:00
|
|
|
max_length=None, min_length=None, *args, **kwargs)
|
2007-08-19 15:38:39 +08:00
|
|
|
|
|
|
|
def clean(self,value):
|
2008-08-14 12:29:02 +08:00
|
|
|
super(PLPESELField, self).clean(value)
|
2007-08-19 15:38:39 +08:00
|
|
|
if not self.has_valid_checksum(value):
|
2007-12-17 16:05:27 +08:00
|
|
|
raise ValidationError(self.error_messages['checksum'])
|
2007-08-19 15:38:39 +08:00
|
|
|
return u'%s' % value
|
|
|
|
|
2007-10-20 17:24:19 +08:00
|
|
|
def has_valid_checksum(self, number):
|
|
|
|
"""
|
|
|
|
Calculates a checksum with the provided algorithm.
|
|
|
|
"""
|
|
|
|
multiple_table = (1, 3, 7, 9, 1, 3, 7, 9, 1, 3, 1)
|
|
|
|
result = 0
|
|
|
|
for i in range(len(number)):
|
|
|
|
result += int(number[i]) * multiple_table[i]
|
|
|
|
return result % 10 == 0
|
2007-08-19 15:38:39 +08:00
|
|
|
|
2008-08-14 12:29:02 +08:00
|
|
|
class PLNIPField(RegexField):
|
2007-08-19 15:38:39 +08:00
|
|
|
"""
|
|
|
|
A form field that validates as Polish Tax Number (NIP).
|
|
|
|
Valid forms are: XXX-XXX-YY-YY or XX-XX-YYY-YYY.
|
2007-10-20 17:24:19 +08:00
|
|
|
|
|
|
|
Checksum algorithm based on documentation at
|
|
|
|
http://wipos.p.lodz.pl/zylla/ut/nip-rego.html
|
2007-08-19 15:38:39 +08:00
|
|
|
"""
|
2007-12-17 16:05:27 +08:00
|
|
|
default_error_messages = {
|
|
|
|
'invalid': _(u'Enter a tax number field (NIP) in the format XXX-XXX-XX-XX or XX-XX-XXX-XXX.'),
|
|
|
|
'checksum': _(u'Wrong checksum for the Tax Number (NIP).'),
|
|
|
|
}
|
2007-10-20 17:24:19 +08:00
|
|
|
|
2007-08-19 15:38:39 +08:00
|
|
|
def __init__(self, *args, **kwargs):
|
2008-08-14 12:29:02 +08:00
|
|
|
super(PLNIPField, self).__init__(r'^\d{3}-\d{3}-\d{2}-\d{2}$|^\d{2}-\d{2}-\d{3}-\d{3}$',
|
2007-12-17 16:05:27 +08:00
|
|
|
max_length=None, min_length=None, *args, **kwargs)
|
2007-08-19 15:38:39 +08:00
|
|
|
|
2007-10-20 17:24:19 +08:00
|
|
|
def clean(self,value):
|
2008-08-14 12:29:02 +08:00
|
|
|
super(PLNIPField, self).clean(value)
|
2007-10-20 17:24:19 +08:00
|
|
|
value = re.sub("[-]", "", value)
|
|
|
|
if not self.has_valid_checksum(value):
|
2007-12-17 16:05:27 +08:00
|
|
|
raise ValidationError(self.error_messages['checksum'])
|
2007-10-20 17:24:19 +08:00
|
|
|
return u'%s' % value
|
|
|
|
|
|
|
|
def has_valid_checksum(self, number):
|
|
|
|
"""
|
|
|
|
Calculates a checksum with the provided algorithm.
|
|
|
|
"""
|
|
|
|
multiple_table = (6, 5, 7, 2, 3, 4, 5, 6, 7)
|
|
|
|
result = 0
|
|
|
|
for i in range(len(number)-1):
|
|
|
|
result += int(number[i]) * multiple_table[i]
|
|
|
|
|
|
|
|
result %= 11
|
|
|
|
if result == int(number[-1]):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2008-08-14 12:29:02 +08:00
|
|
|
class PLREGONField(RegexField):
|
2007-10-20 17:24:19 +08:00
|
|
|
"""
|
2009-04-10 09:03:44 +08:00
|
|
|
A form field that validates its input is a REGON number.
|
2007-10-20 17:24:19 +08:00
|
|
|
|
2009-04-10 09:03:44 +08:00
|
|
|
Valid regon number consists of 9 or 14 digits.
|
|
|
|
See http://www.stat.gov.pl/bip/regon_ENG_HTML.htm for more information.
|
2007-10-20 17:24:19 +08:00
|
|
|
"""
|
2007-12-17 16:05:27 +08:00
|
|
|
default_error_messages = {
|
2009-04-10 09:03:44 +08:00
|
|
|
'invalid': _(u'National Business Register Number (REGON) consists of 9 or 14 digits.'),
|
2007-12-17 16:05:27 +08:00
|
|
|
'checksum': _(u'Wrong checksum for the National Business Register Number (REGON).'),
|
|
|
|
}
|
|
|
|
|
2007-10-20 17:24:19 +08:00
|
|
|
def __init__(self, *args, **kwargs):
|
2009-04-10 09:03:44 +08:00
|
|
|
super(PLREGONField, self).__init__(r'^\d{9,14}$',
|
2007-12-17 16:05:27 +08:00
|
|
|
max_length=None, min_length=None, *args, **kwargs)
|
2007-10-20 17:24:19 +08:00
|
|
|
|
|
|
|
def clean(self,value):
|
2008-08-14 12:29:02 +08:00
|
|
|
super(PLREGONField, self).clean(value)
|
2007-10-20 17:24:19 +08:00
|
|
|
if not self.has_valid_checksum(value):
|
2007-12-17 16:05:27 +08:00
|
|
|
raise ValidationError(self.error_messages['checksum'])
|
2007-10-20 17:24:19 +08:00
|
|
|
return u'%s' % value
|
|
|
|
|
|
|
|
def has_valid_checksum(self, number):
|
|
|
|
"""
|
|
|
|
Calculates a checksum with the provided algorithm.
|
|
|
|
"""
|
2009-04-10 09:03:44 +08:00
|
|
|
weights = (
|
|
|
|
(8, 9, 2, 3, 4, 5, 6, 7, -1),
|
|
|
|
(2, 4, 8, 5, 0, 9, 7, 3, 6, 1, 2, 4, 8, -1),
|
|
|
|
(8, 9, 2, 3, 4, 5, 6, 7, -1, 0, 0, 0, 0, 0),
|
|
|
|
)
|
2007-10-20 17:24:19 +08:00
|
|
|
|
2009-04-10 09:03:44 +08:00
|
|
|
weights = [table for table in weights if len(table) == len(number)]
|
2007-10-20 17:24:19 +08:00
|
|
|
|
2009-04-10 09:03:44 +08:00
|
|
|
for table in weights:
|
|
|
|
checksum = sum([int(n) * w for n, w in zip(number, table)])
|
|
|
|
if checksum % 11 % 10:
|
|
|
|
return False
|
2007-10-20 17:24:19 +08:00
|
|
|
|
2009-04-10 09:03:44 +08:00
|
|
|
return bool(weights)
|
2007-08-19 15:38:39 +08:00
|
|
|
|
|
|
|
class PLPostalCodeField(RegexField):
|
|
|
|
"""
|
|
|
|
A form field that validates as Polish postal code.
|
|
|
|
Valid code is XX-XXX where X is digit.
|
|
|
|
"""
|
2007-12-17 16:05:27 +08:00
|
|
|
default_error_messages = {
|
|
|
|
'invalid': _(u'Enter a postal code in the format XX-XXX.'),
|
|
|
|
}
|
|
|
|
|
2007-08-19 15:38:39 +08:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(PLPostalCodeField, self).__init__(r'^\d{2}-\d{3}$',
|
2007-12-17 16:05:27 +08:00
|
|
|
max_length=None, min_length=None, *args, **kwargs)
|