Fixed #8515 -- Fixed validation of Polish REGON numbers.

Patch from Piotr Lewandowski.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10460 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Malcolm Tredinnick 2009-04-10 01:03:44 +00:00
parent e5be4b4f3d
commit f6309cbf80
2 changed files with 27 additions and 24 deletions

View File

@ -100,20 +100,18 @@ class PLNIPField(RegexField):
class PLREGONField(RegexField): class PLREGONField(RegexField):
""" """
A form field that validated as Polish National Official Business Register A form field that validates its input is a REGON number.
Number (REGON). Valid numbers contain 7 or 9 digits.
More on the field: http://www.stat.gov.pl/bip/regon_ENG_HTML.htm Valid regon number consists of 9 or 14 digits.
See http://www.stat.gov.pl/bip/regon_ENG_HTML.htm for more information.
The checksum algorithm is documented at http://wipos.p.lodz.pl/zylla/ut/nip-rego.html
""" """
default_error_messages = { default_error_messages = {
'invalid': _(u'National Business Register Number (REGON) consists of 7 or 9 digits.'), 'invalid': _(u'National Business Register Number (REGON) consists of 9 or 14 digits.'),
'checksum': _(u'Wrong checksum for the National Business Register Number (REGON).'), 'checksum': _(u'Wrong checksum for the National Business Register Number (REGON).'),
} }
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(PLREGONField, self).__init__(r'^\d{7,9}$', super(PLREGONField, self).__init__(r'^\d{9,14}$',
max_length=None, min_length=None, *args, **kwargs) max_length=None, min_length=None, *args, **kwargs)
def clean(self,value): def clean(self,value):
@ -126,26 +124,21 @@ class PLREGONField(RegexField):
""" """
Calculates a checksum with the provided algorithm. Calculates a checksum with the provided algorithm.
""" """
multiple_table_7 = (2, 3, 4, 5, 6, 7) weights = (
multiple_table_9 = (8, 9, 2, 3, 4, 5, 6, 7) (8, 9, 2, 3, 4, 5, 6, 7, -1),
result = 0 (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),
)
if len(number) == 7: weights = [table for table in weights if len(table) == len(number)]
multiple_table = multiple_table_7
else:
multiple_table = multiple_table_9
for i in range(len(number)-1): for table in weights:
result += int(number[i]) * multiple_table[i] checksum = sum([int(n) * w for n, w in zip(number, table)])
if checksum % 11 % 10:
result %= 11
if result == 10:
result = 0
if result == int(number[-1]):
return True
else:
return False return False
return bool(weights)
class PLPostalCodeField(RegexField): class PLPostalCodeField(RegexField):
""" """
A form field that validates as Polish postal code. A form field that validates as Polish postal code.

View File

@ -67,8 +67,18 @@ ValidationError: [u'National Identification Number consists of 11 digits.']
>>> from django.contrib.localflavor.pl.forms import PLREGONField >>> from django.contrib.localflavor.pl.forms import PLREGONField
>>> f = PLREGONField() >>> f = PLREGONField()
>>> f.clean('12345678512347')
u'12345678512347'
>>> f.clean('590096454') >>> f.clean('590096454')
u'590096454' u'590096454'
>>> f.clean('123456784')
Traceback (most recent call last):
...
ValidationError: [u'Wrong checksum for the National Business Register Number (REGON).']
>>> f.clean('12345678412342')
Traceback (most recent call last):
...
ValidationError: [u'Wrong checksum for the National Business Register Number (REGON).']
>>> f.clean('590096453') >>> f.clean('590096453')
Traceback (most recent call last): Traceback (most recent call last):
... ...
@ -76,6 +86,6 @@ ValidationError: [u'Wrong checksum for the National Business Register Number (RE
>>> f.clean('590096') >>> f.clean('590096')
Traceback (most recent call last): Traceback (most recent call last):
... ...
ValidationError: [u'National Business Register Number (REGON) consists of 7 or 9 digits.'] ValidationError: [u'National Business Register Number (REGON) consists of 9 or 14 digits.']
""" """