Skip to content

Commit d361126

Browse files
authored
Fix REGON validation (#529)
1 parent 6b81869 commit d361126

File tree

4 files changed

+35
-12
lines changed

4 files changed

+35
-12
lines changed

docs/authors.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,4 @@ Authors
138138
* Abhineet Tamrakar
139139
* Tudor Amariei
140140
* Dishan Sachin
141+
* Kacper Urbański

docs/changelog.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ New fields for existing flavors:
1414

1515
Modifications to existing flavors:
1616

17-
- None
17+
- Fix REGON validation for PL
18+
(`gh-529 <https://github.com/django/django-localflavor/pull/529>`_).
1819

1920
Other changes:
2021

localflavor/pl/forms.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ class PLREGONField(RegexField):
192192
}
193193

194194
def __init__(self, **kwargs):
195-
super().__init__(r'^\d{9,14}$', **kwargs)
195+
super().__init__(r'^(\d{9}|\d{14})$', **kwargs)
196196

197197
def clean(self, value):
198198
value = super().clean(value)
@@ -204,6 +204,7 @@ def clean(self, value):
204204

205205
def has_valid_checksum(self, number):
206206
"""Calculates a checksum with the provided algorithm."""
207+
# the 14 digits number must firstly pass 9 digits too
207208
weights = (
208209
(8, 9, 2, 3, 4, 5, 6, 7, -1),
209210
(2, 4, 8, 5, 0, 9, 7, 3, 6, 1, 2, 4, 8, -1),
@@ -217,7 +218,7 @@ def has_valid_checksum(self, number):
217218

218219
mod_result = checksum % 11
219220

220-
if mod_result == 10 and number[-1] != '0':
221+
if mod_result == 10 and number[table.index(-1)] != '0':
221222
return False
222223

223224
if mod_result % 10:

tests/test_pl.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
from django.test import SimpleTestCase
22

3-
from localflavor.pl.forms import (PLCountySelect, PLNationalIDCardNumberField, PLNIPField, PLPESELField,
4-
PLPostalCodeField, PLProvinceSelect, PLREGONField)
3+
from localflavor.pl.forms import (
4+
PLCountySelect,
5+
PLNationalIDCardNumberField,
6+
PLNIPField,
7+
PLPESELField,
8+
PLPostalCodeField,
9+
PLProvinceSelect,
10+
PLREGONField,
11+
)
512

613

714
class PLLocalFlavorTests(SimpleTestCase):
@@ -420,7 +427,9 @@ def test_PLPostalCodeField(self):
420427
self.assertFieldOutput(PLPostalCodeField, valid, invalid)
421428

422429
def test_PLNIPField(self):
423-
error_format = ['Enter a tax number field (NIP) in the format XXX-XXX-XX-XX, XXX-XX-XX-XXX or XXXXXXXXXX.']
430+
error_format = [
431+
'Enter a tax number field (NIP) in the format XXX-XXX-XX-XX, XXX-XX-XX-XXX or XXXXXXXXXX.'
432+
]
424433
error_checksum = ['Wrong checksum for the Tax Number (NIP).']
425434
valid = {
426435
'646-241-41-24': '6462414124',
@@ -437,7 +446,9 @@ def test_PLNIPField(self):
437446
def test_PLPESELField(self):
438447
error_checksum = ['Wrong checksum for the National Identification Number.']
439448
error_format = ['National Identification Number consists of 11 digits.']
440-
error_birthdate = ['The National Identification Number contains an invalid birth date.']
449+
error_birthdate = [
450+
'The National Identification Number contains an invalid birth date.'
451+
]
441452
valid = {
442453
'80071610614': '80071610614',
443454
}
@@ -465,25 +476,34 @@ def test_PLNationalIDCardNumberField(self):
465476
self.assertFieldOutput(PLNationalIDCardNumberField, valid, invalid)
466477

467478
def test_PLREGONField(self):
468-
error_checksum = ['Wrong checksum for the National Business Register Number (REGON).']
469-
error_format = ['National Business Register Number (REGON) consists of 9 or 14 digits.']
479+
VALID_LENGTHS = (9, 14)
480+
481+
error_checksum = [
482+
'Wrong checksum for the National Business Register Number (REGON).'
483+
]
484+
error_format = [
485+
'National Business Register Number (REGON) consists of 9 or 14 digits.'
486+
]
470487
valid = {
471488
'12345678512347': '12345678512347',
472489
'590096454': '590096454',
473-
474490
# A special case where the checksum == 10 and the control
475491
# digit == '0'
476492
'391023200': '391023200',
493+
'00102110000342': '00102110000342',
494+
# A special case where the 14 digits checksum == 10
495+
# but then 9 digits checksum is not required to be 0
496+
'00202110146340': '00202110146340',
477497
}
478498
invalid = {
479499
'123456784': error_checksum,
480500
'12345678412342': error_checksum,
481501
'590096453': error_checksum,
482-
483502
# A special case where the checksum == 10,
484503
# but the control digit != '0'
485504
'111111111': error_checksum,
486-
487505
'590096': error_format,
506+
# 8, 10, 11, 12, 13, 15 digits
507+
**{'1' * i: error_format for i in range(8, 16) if i not in VALID_LENGTHS},
488508
}
489509
self.assertFieldOutput(PLREGONField, valid, invalid)

0 commit comments

Comments
 (0)