Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Accept French Postal Services identifiers in forms #505

Merged
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 @@ -33,6 +33,7 @@ Authors
* Ben Konrath
* Bruno M. Custódio
* Burhan Khalid
* Célia Prat
* Claude Paroz
* Daniel Ampuero
* Daniela Ponader
Expand Down
2 changes: 2 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Modifications to existing flavors:
- Fix Belarus passport field description punctuation
(`gh-484 <https://github.com/django/django-localflavor/pull/484>`_).
- Change `Kiev` to `Kyiv` 🇺🇦 according to ISO_3166-2:UA
- Accept French Postal Services identifiers in forms
(`gh-505 <https://github.com/django/django-localflavor/pull/505>`_).

Other changes:

Expand Down
31 changes: 14 additions & 17 deletions localflavor/fr/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,21 +184,7 @@ def _check_foreign_countries(self, commune_of_origin, current_year, department_o
raise ValidationError(self.error_messages['invalid'], code='invalid')


class FRSIRENENumberMixin:
"""Abstract class for SIREN and SIRET numbers, from the SIRENE register."""

def clean(self, value):
value = super().clean(value)
if value in self.empty_values:
return value

value = value.replace(' ', '').replace('-', '')
if not self.r_valid.match(value) or not luhn.is_valid(value):
raise ValidationError(self.error_messages['invalid'], code='invalid')
return value


class FRSIRENField(FRSIRENENumberMixin, CharField):
class FRSIRENField(CharField):
"""
SIREN stands for "Système d'identification du répertoire des entreprises".
Expand All @@ -220,8 +206,18 @@ def prepare_value(self, value):
value = value.replace(' ', '').replace('-', '')
return ' '.join((value[:3], value[3:6], value[6:]))

def clean(self, value):
value = super().clean(value)
if value in self.empty_values:
return value

value = value.replace(' ', '').replace('-', '')
if not self.r_valid.match(value) or not luhn.is_valid(value):
raise ValidationError(self.error_messages['invalid'], code='invalid')
return value


class FRSIRETField(FRSIRENENumberMixin, CharField):
class FRSIRETField(CharField):
"""
SIRET stands for "Système d'identification du répertoire des établissements".
Expand All @@ -244,7 +240,8 @@ def clean(self, value):

value = value.replace(' ', '').replace('-', '')

if not luhn.is_valid(value[:9]):
if not self.r_valid.match(value) or not luhn.is_valid(value[:9]) or \
(value.startswith("356000000") and sum(int(x) for x in value) % 5 != 0):
raise ValidationError(self.error_messages['invalid'], code='invalid')
return value

Expand Down
4 changes: 4 additions & 0 deletions tests/test_fr.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,12 @@ def test_FRSIRENNumber(self):
'752932715': '752932715',
'752 932 715': '752932715',
'752-932-715': '752932715',
'356000000': '356000000'
}
invalid = {
'1234': error_format, # wrong size
'752932712': error_format, # Bad luhn on SIREN
'35600000014597' : error_format
}
self.assertFieldOutput(FRSIRENField, valid, invalid)

Expand All @@ -294,11 +296,13 @@ def test_FRSIRETNumber(self):
'75293271500010': '75293271500010',
'752 932 715 00010': '75293271500010',
'752-932-715-00010': '75293271500010',
'35600000014597' : '35600000014597', # Special case La Poste
}
invalid = {
'1234': error_format, # wrong size
'75293271200017': error_format, # Bad luhn on SIREN
'75293271000010': error_format, # Bad luhn on whole
'35600000014596' : error_format # Special case La Poste
}
self.assertFieldOutput(FRSIRETField, valid, invalid)

Expand Down
Loading