Kaydet (Commit) 90c177f1 authored tarafından Malcolm Tredinnick's avatar Malcolm Tredinnick

Fixed #5200 -- Added Polish localflavor. Thanks, Slawek Mikula.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@5936 bcc190cf-cafb-0310-a4f2-bffc1f526a37
üst a901af5f
...@@ -196,6 +196,7 @@ answer newbie questions, and generally made Django that much better: ...@@ -196,6 +196,7 @@ answer newbie questions, and generally made Django that much better:
mccutchen@gmail.com mccutchen@gmail.com
michael.mcewan@gmail.com michael.mcewan@gmail.com
mikko@sorl.net mikko@sorl.net
Slawek Mikula <slawek dot mikula at gmail dot com>
mitakummaa@gmail.com mitakummaa@gmail.com
mmarshall mmarshall
Andreas Mock <andreas.mock@web.de> Andreas Mock <andreas.mock@web.de>
......
"""
Polish-specific form helpers
"""
from django.newforms import ValidationError
from django.newforms.fields import Select, RegexField
from django.utils.translation import ugettext as _
class PLVoivodeshipSelect(Select):
"""
A select widget with list of Polish voivodeships (administrative provinces)
as choices.
"""
def __init__(self, attrs=None):
from pl_voivodeships import VOIVODESHIP_CHOICES
super(PLVoivodeshipSelect, self).__init__(attrs, choices=VOIVODESHIP_CHOICES)
class PLAdministrativeUnitSelect(Select):
"""
A select widget with list of Polish administrative units as choices.
"""
def __init__(self, attrs=None):
from pl_administrativeunits import ADMINISTRATIVE_UNIT_CHOICES
super(PLAdministrativeUnitSelect, self).__init__(attrs, choices=ADMINISTRATIVE_UNIT_CHOICES)
class PLNationalIdentificationNumberField(RegexField):
"""
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.
"""
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]
if result % 10 == 0:
return True
else:
return False
def __init__(self, *args, **kwargs):
super(PLNationalIdentificationNumberField, self).__init__(r'^\d{11}$',
max_length=None, min_length=None, error_message=_(u'National Identification Number consists of 11 digits.'),
*args, **kwargs)
def clean(self,value):
super(PLNationalIdentificationNumberField, self).clean(value)
if not self.has_valid_checksum(value):
raise ValidationError(_(u'Wrong checksum for the National Identification Number.'))
return u'%s' % value
class PLTaxNumberField(RegexField):
"""
A form field that validates as Polish Tax Number (NIP).
Valid forms are: XXX-XXX-YY-YY or XX-XX-YYY-YYY.
"""
def __init__(self, *args, **kwargs):
super(PLTaxNumberField, self).__init__(r'^\d{3}-\d{3}-\d{2}-\d{2}$|^\d{2}-\d{2}-\d{3}-\d{3}$',
max_length=None, min_length=None,
error_message=_(u'Enter a tax number field (NIP) in the format XXX-XXX-XX-XX or XX-XX-XXX-XXX.'), *args, **kwargs)
class PLPostalCodeField(RegexField):
"""
A form field that validates as Polish postal code.
Valid code is XX-XXX where X is digit.
"""
def __init__(self, *args, **kwargs):
super(PLPostalCodeField, self).__init__(r'^\d{2}-\d{3}$',
max_length=None, min_length=None,
error_message=_(u'Enter a postal code in the format XX-XXX.'),
*args, **kwargs)
"""
Polish voivodeship as in http://en.wikipedia.org/wiki/Poland#Administrative_division
"""
from django.utils.translation import ugettext_lazy as _
VOIVODESHIP_CHOICES = (
('lower_silesia', _('Lower Silesia')),
('kuyavia-pomerania', _('Kuyavia-Pomerania')),
('lublin', _('Lublin')),
('lubusz', _('Lubusz')),
('lodz', _('Lodz')),
('lesser_poland', _('Lesser Poland')),
('masovia', _('Masovia')),
('opole', _('Opole')),
('subcarpatia', _('Subcarpatia')),
('podlasie', _('Podlasie')),
('pomerania', _('Pomerania')),
('silesia', _('Silesia')),
('swietokrzyskie', _('Swietokrzyskie')),
('warmia-masuria', _('Warmia-Masuria')),
('greater_poland', _('Greater Poland')),
('west_pomerania', _('West Pomerania')),
)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment