Index: django/contrib/localflavor/pl/forms.py =================================================================== --- django/contrib/localflavor/pl/forms.py (wersja 6397) +++ django/contrib/localflavor/pl/forms.py (kopia robocza) @@ -6,6 +6,8 @@ from django.newforms.fields import Select, RegexField from django.utils.translation import ugettext as _ +import re + class PLVoivodeshipSelect(Select): """ A select widget with list of Polish voivodeships (administrative provinces) @@ -64,13 +66,85 @@ """ A form field that validates as Polish Tax Number (NIP). Valid forms are: XXX-XXX-YY-YY or XX-XX-YYY-YYY. + + Checksum algorithm based on documentation at http://wipos.p.lodz.pl/zylla/ut/nip-rego.html """ + + 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 + 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) + def clean(self,value): + super(PLTaxNumberField, self).clean(value) + value = re.sub("[-]","",value) + if not self.has_valid_checksum(value): + raise ValidationError(_(u'Wrong checksum for the Tax Number (NIP).')) + return u'%s' % value +class PLNationalBusinessRegisterField(RegexField): + """ + A form field that validated as Polish National Official Business Register Number (REGON) + Valid forms are: 7 or 9 digits number + + More on the field: http://www.stat.gov.pl/bip/regon_ENG_HTML.htm + + The checksum algorithm is documented at http://wipos.p.lodz.pl/zylla/ut/nip-rego.html + """ + + def has_valid_checksum(self, number): + """ + Calculates a checksum with the provided algorithm. + """ + multiple_table_7 = (2, 3, 4, 5, 6, 7) + multiple_table_9 = (8, 9, 2, 3, 4, 5, 6, 7) + result = 0 + + if len(number) == 7: + multiple_table = multiple_table_7 + else: + multiple_table = multiple_table_9 + + for i in range(len(number)-1): + result += int(number[i])*multiple_table[i] + + result %= 11 + + if result == 10: + result = 0 + + if result == int(number[-1]): + return True + else: + return False + + def __init__(self, *args, **kwargs): + super(PLNationalBusinessRegisterField, self).__init__(r'^\d{7,9}$', + max_length=None, min_length=None, error_message=_(u'National Business Register Number (REGON) consists of 7 or 9 digits.'), + *args, **kwargs) + + def clean(self,value): + super(PLNationalBusinessRegisterField, self).clean(value) + if not self.has_valid_checksum(value): + raise ValidationError(_(u'Wrong checksum for the National Business Register Number (REGON).')) + return u'%s' % value + class PLPostalCodeField(RegexField): """ A form field that validates as Polish postal code. Index: tests/regressiontests/forms/localflavor/pl.py =================================================================== --- tests/regressiontests/forms/localflavor/pl.py (wersja 6397) +++ tests/regressiontests/forms/localflavor/pl.py (kopia robocza) @@ -35,11 +35,15 @@ Traceback (most recent call last): ... ValidationError: [u'Enter a tax number field (NIP) in the format XXX-XXX-XX-XX or XX-XX-XXX-XXX.'] ->>> f.clean('43-34-234-323') -u'43-34-234-323' ->>> f.clean('433-344-24-23') -u'433-344-24-23' - +>>> f.clean('64-62-414-124') +u'6462414124' +>>> f.clean('646-241-41-24') +u'6462414124' +>>> f.clean('646-241-41-23') +Traceback (most recent call last): +... +ValidationError: [u'Wrong checksum for the Tax Number (NIP).'] + # PLNationalIdentificationNumberField ############################################ >>> from django.contrib.localflavor.pl.forms import PLNationalIdentificationNumberField @@ -58,4 +62,20 @@ Traceback (most recent call last): ... ValidationError: [u'National Identification Number consists of 11 digits.'] + +# PLNationalBusinessRegisterField ################################################ + +>>> from django.contrib.localflavor.pl.forms import PLNationalBusinessRegisterField +>>> f = PLNationalBusinessRegisterField() +>>> f.clean('590096454') +u'590096454' +>>> f.clean('590096453') +Traceback (most recent call last): +... +ValidationError: [u'Wrong checksum for the National Business Register Number (REGON).'] +>>> f.clean('590096') +Traceback (most recent call last): +... +ValidationError: [u'National Business Register Number (REGON) consists of 7 or 9 digits.'] + """