Skip to content

Commit

Permalink
fix: Accept French Postal Services identifiers in forms
Browse files Browse the repository at this point in the history
  • Loading branch information
Chatewgne committed Aug 13, 2024
1 parent 1f13beb commit f673d11
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
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
39 changes: 23 additions & 16 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

class FRSIRETField(FRSIRENENumberMixin, CharField):
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(CharField):
"""
SIRET stands for "Système d'identification du répertoire des établissements".
Expand Down Expand Up @@ -254,6 +250,17 @@ def prepare_value(self, value):
value = value.replace(' ', '').replace('-', '')
return ' '.join((value[:3], value[3:6], value[6:9], value[9:]))

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_gvalid(value)) and not
(value.startswith("356000000") and sum(int(x) for x in value) % 5 == 0)):
raise ValidationError(self.error_messages['invalid'], code='invalid')
return value


class FRRNAField(CharField):
"""
Expand Down
2 changes: 2 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',
'35600000014597' : '35600000014597'
}
invalid = {
'1234': error_format, # wrong size
'752932712': error_format, # Bad luhn on SIREN
'35600000014596' : error_format
}
self.assertFieldOutput(FRSIRENField, valid, invalid)

Expand Down

0 comments on commit f673d11

Please sign in to comment.