Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/authors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,4 @@ Authors
* Abhineet Tamrakar
* Tudor Amariei
* Dishan Sachin
* Kacper Urbański
3 changes: 2 additions & 1 deletion docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ New fields for existing flavors:

Modifications to existing flavors:

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

Other changes:

Expand Down
5 changes: 3 additions & 2 deletions localflavor/pl/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ class PLREGONField(RegexField):
}

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

def clean(self, value):
value = super().clean(value)
Expand All @@ -204,6 +204,7 @@ def clean(self, value):

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

mod_result = checksum % 11

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

if mod_result % 10:
Expand Down
38 changes: 29 additions & 9 deletions tests/test_pl.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
from django.test import SimpleTestCase

from localflavor.pl.forms import (PLCountySelect, PLNationalIDCardNumberField, PLNIPField, PLPESELField,
PLPostalCodeField, PLProvinceSelect, PLREGONField)
from localflavor.pl.forms import (
PLCountySelect,
PLNationalIDCardNumberField,
PLNIPField,
PLPESELField,
PLPostalCodeField,
PLProvinceSelect,
PLREGONField,
)


class PLLocalFlavorTests(SimpleTestCase):
Expand Down Expand Up @@ -420,7 +427,9 @@ def test_PLPostalCodeField(self):
self.assertFieldOutput(PLPostalCodeField, valid, invalid)

def test_PLNIPField(self):
error_format = ['Enter a tax number field (NIP) in the format XXX-XXX-XX-XX, XXX-XX-XX-XXX or XXXXXXXXXX.']
error_format = [
'Enter a tax number field (NIP) in the format XXX-XXX-XX-XX, XXX-XX-XX-XXX or XXXXXXXXXX.'
]
error_checksum = ['Wrong checksum for the Tax Number (NIP).']
valid = {
'646-241-41-24': '6462414124',
Expand All @@ -437,7 +446,9 @@ def test_PLNIPField(self):
def test_PLPESELField(self):
error_checksum = ['Wrong checksum for the National Identification Number.']
error_format = ['National Identification Number consists of 11 digits.']
error_birthdate = ['The National Identification Number contains an invalid birth date.']
error_birthdate = [
'The National Identification Number contains an invalid birth date.'
]
valid = {
'80071610614': '80071610614',
}
Expand Down Expand Up @@ -465,25 +476,34 @@ def test_PLNationalIDCardNumberField(self):
self.assertFieldOutput(PLNationalIDCardNumberField, valid, invalid)

def test_PLREGONField(self):
error_checksum = ['Wrong checksum for the National Business Register Number (REGON).']
error_format = ['National Business Register Number (REGON) consists of 9 or 14 digits.']
VALID_LENGTHS = (9, 14)

error_checksum = [
'Wrong checksum for the National Business Register Number (REGON).'
]
error_format = [
'National Business Register Number (REGON) consists of 9 or 14 digits.'
]
valid = {
'12345678512347': '12345678512347',
'590096454': '590096454',

# A special case where the checksum == 10 and the control
# digit == '0'
'391023200': '391023200',
'00102110000342': '00102110000342',
# A special case where the 14 digits checksum == 10
# but then 9 digits checksum is not required to be 0
'00202110146340': '00202110146340',
}
invalid = {
'123456784': error_checksum,
'12345678412342': error_checksum,
'590096453': error_checksum,

# A special case where the checksum == 10,
# but the control digit != '0'
'111111111': error_checksum,

'590096': error_format,
# 8, 10, 11, 12, 13, 15 digits
**{'1' * i: error_format for i in range(8, 16) if i not in VALID_LENGTHS},
}
self.assertFieldOutput(PLREGONField, valid, invalid)