Author: mtredinnick
Date: 2007-10-20 04:24:19 -0500 (Sat, 20 Oct 2007)
New Revision: 6556
Modified:
django/trunk/django/contrib/localflavor/pl/forms.py
django/trunk/tests/regressiontests/forms/localflavor/pl.py
Log:
Fixed #5200 -- Added some more functionality to the Polish localflavor. Thanks,
Slawek Mikula.
Modified: django/trunk/django/contrib/localflavor/pl/forms.py
===================================================================
--- django/trunk/django/contrib/localflavor/pl/forms.py 2007-10-20 09:16:54 UTC
(rev 6555)
+++ django/trunk/django/contrib/localflavor/pl/forms.py 2007-10-20 09:24:19 UTC
(rev 6556)
@@ -2,6 +2,8 @@
Polish-specific form helpers
"""
+import re
+
from django.newforms import ValidationError
from django.newforms.fields import Select, RegexField
from django.utils.translation import ugettext as _
@@ -34,20 +36,6 @@
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.'),
@@ -59,18 +47,96 @@
raise ValidationError(_(u'Wrong checksum for the National
Identification Number.'))
return u'%s' % value
+ 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
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.
+
+ Checksum algorithm based on documentation at
+ http://wipos.p.lodz.pl/zylla/ut/nip-rego.html
"""
+
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
+ 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
+
+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 __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
+
+ 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
+
class PLPostalCodeField(RegexField):
"""
A form field that validates as Polish postal code.
Modified: django/trunk/tests/regressiontests/forms/localflavor/pl.py
===================================================================
--- django/trunk/tests/regressiontests/forms/localflavor/pl.py 2007-10-20
09:16:54 UTC (rev 6555)
+++ django/trunk/tests/regressiontests/forms/localflavor/pl.py 2007-10-20
09:24:19 UTC (rev 6556)
@@ -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.']
+
"""
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---