Skip to content

Commit 085fb2b

Browse files
authored
Accept French Postal Services identifiers in forms (#505)
1 parent 1f13beb commit 085fb2b

File tree

4 files changed

+21
-17
lines changed

4 files changed

+21
-17
lines changed

docs/authors.rst

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Authors
3333
* Ben Konrath
3434
* Bruno M. Custódio
3535
* Burhan Khalid
36+
* Célia Prat
3637
* Claude Paroz
3738
* Daniel Ampuero
3839
* Daniela Ponader

docs/changelog.rst

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Modifications to existing flavors:
1717
- Fix Belarus passport field description punctuation
1818
(`gh-484 <https://github.com/django/django-localflavor/pull/484>`_).
1919
- Change `Kiev` to `Kyiv` 🇺🇦 according to ISO_3166-2:UA
20+
- Accept French Postal Services identifiers in forms
21+
(`gh-505 <https://github.com/django/django-localflavor/pull/505>`_).
2022

2123
Other changes:
2224

localflavor/fr/forms.py

+14-17
Original file line numberDiff line numberDiff line change
@@ -184,21 +184,7 @@ def _check_foreign_countries(self, commune_of_origin, current_year, department_o
184184
raise ValidationError(self.error_messages['invalid'], code='invalid')
185185

186186

187-
class FRSIRENENumberMixin:
188-
"""Abstract class for SIREN and SIRET numbers, from the SIRENE register."""
189-
190-
def clean(self, value):
191-
value = super().clean(value)
192-
if value in self.empty_values:
193-
return value
194-
195-
value = value.replace(' ', '').replace('-', '')
196-
if not self.r_valid.match(value) or not luhn.is_valid(value):
197-
raise ValidationError(self.error_messages['invalid'], code='invalid')
198-
return value
199-
200-
201-
class FRSIRENField(FRSIRENENumberMixin, CharField):
187+
class FRSIRENField(CharField):
202188
"""
203189
SIREN stands for "Système d'identification du répertoire des entreprises".
204190
@@ -220,8 +206,18 @@ def prepare_value(self, value):
220206
value = value.replace(' ', '').replace('-', '')
221207
return ' '.join((value[:3], value[3:6], value[6:]))
222208

209+
def clean(self, value):
210+
value = super().clean(value)
211+
if value in self.empty_values:
212+
return value
213+
214+
value = value.replace(' ', '').replace('-', '')
215+
if not self.r_valid.match(value) or not luhn.is_valid(value):
216+
raise ValidationError(self.error_messages['invalid'], code='invalid')
217+
return value
218+
223219

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

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

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

tests/test_fr.py

+4
Original file line numberDiff line numberDiff line change
@@ -271,10 +271,12 @@ def test_FRSIRENNumber(self):
271271
'752932715': '752932715',
272272
'752 932 715': '752932715',
273273
'752-932-715': '752932715',
274+
'356000000': '356000000'
274275
}
275276
invalid = {
276277
'1234': error_format, # wrong size
277278
'752932712': error_format, # Bad luhn on SIREN
279+
'35600000014597' : error_format
278280
}
279281
self.assertFieldOutput(FRSIRENField, valid, invalid)
280282

@@ -294,11 +296,13 @@ def test_FRSIRETNumber(self):
294296
'75293271500010': '75293271500010',
295297
'752 932 715 00010': '75293271500010',
296298
'752-932-715-00010': '75293271500010',
299+
'35600000014597' : '35600000014597', # Special case La Poste
297300
}
298301
invalid = {
299302
'1234': error_format, # wrong size
300303
'75293271200017': error_format, # Bad luhn on SIREN
301304
'75293271000010': error_format, # Bad luhn on whole
305+
'35600000014596' : error_format # Special case La Poste
302306
}
303307
self.assertFieldOutput(FRSIRETField, valid, invalid)
304308

0 commit comments

Comments
 (0)