Skip to content

Commit

Permalink
Merge branch 'django:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
BESoft33 authored Aug 27, 2024
2 parents 189964b + 085fb2b commit 7ac0c22
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 21 deletions.
3 changes: 3 additions & 0 deletions docs/authors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Authors
=======

* Aaron Boman
* Abdellah El Youssfi Alaoui
* Adam Taylor
* Adnane Belmadiaf
* Adonys Alea Boffill
Expand Down Expand Up @@ -32,6 +33,7 @@ Authors
* Ben Konrath
* Bruno M. Custódio
* Burhan Khalid
* Célia Prat
* Claude Paroz
* Daniel Ampuero
* Daniela Ponader
Expand All @@ -42,6 +44,7 @@ Authors
* Diederik van der Boor
* d.merc
* Dmitry Dygalo
* Dmytro Litvinov
* Dominick Rivard
* Douglas Miranda
* Elliott Fawcett
Expand Down
5 changes: 4 additions & 1 deletion docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ New flavors:

New fields for existing flavors:

- None
- Added CIN Number field in Morocco flavor (`gh-705 <https://github.com/django/django-localflavor/pull/507>`_).

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
23 changes: 23 additions & 0 deletions localflavor/ma/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,26 @@ class MARegionField(CharField):
def __init__(self, **kwargs):
kwargs.setdefault('label', _('Select Region'))
super().__init__(**kwargs)


class MACinNumberField(RegexField):
"""
CIN number: (Numéro de la Carte D'Identité Nationale) The CIN represents the ID of a Moroccan citizen.
- It is an 8-max-length string that starts with one or two Latin letters followed by digits,
with the first digit not being zero.
- as implemented in the official government site "https://www.cnie.ma/"
.. versionadded:: 4.1
"""

default_error_messages = {
'invalid': _('Enter a valid Moroccan CIN number.'),
}
cin_pattern = r'^[A-Za-z]{1,2}[1-9][0-9]{0,6}$'

def __init__(self, **kwargs):
kwargs.setdefault('label', _('CIN Number'))
kwargs['max_length'] = 8
kwargs['min_length'] = 2
super().__init__(self.cin_pattern, **kwargs)
4 changes: 2 additions & 2 deletions localflavor/ua/ua_regions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
('UA-65', _('Kherson Oblast')),
('UA-68', _('Khmelnytskyi Oblast')),
('UA-35', _('Kirovohrad Oblast')),
('UA-32', _('Kiev Oblast')),
('UA-32', _('Kyiv Oblast')),
('UA-09', _('Luhansk Oblast')),
('UA-46', _('Lviv Oblast')),
('UA-48', _('Mykolaiv Oblast')),
Expand All @@ -28,6 +28,6 @@
('UA-23', _('Zaporizhia Oblast')),
('UA-18', _('Zhytomyr Oblast')),
('UA-43', _('Autonomous Republic of Crimea')),
('UA-30', _('Kiev')),
('UA-30', _('Kyiv')),
('UA-40', _('Sevastopol'))
)
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
33 changes: 32 additions & 1 deletion tests/test_ma.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.test import SimpleTestCase

from localflavor.ma.forms import MAPostalCodeField, MAProvinceField, MAProvinceSelect, MARegionField, MARegionSelect
from localflavor.ma.forms import MAPostalCodeField, MAProvinceField, MAProvinceSelect, MARegionField, MARegionSelect, \
MACinNumberField

PROVINCE_SELECT_OUTPUT = '''
<select name="province">
Expand Down Expand Up @@ -99,6 +100,7 @@
</select>
'''


class MALocalFlavorTests(SimpleTestCase):
def test_MAPostalCodeField(self):
error_format = ['Enter a postal code in the format XXXXX.']
Expand Down Expand Up @@ -128,3 +130,32 @@ def test_MAProvinceSelect(self):
def test_MARegionSelect(self):
f = MARegionSelect()
self.assertHTMLEqual(f.render('region', '04'), REGION_SELECT_OUTPUT)

def test_MACinNumberField(self):
error_format = ['Enter a valid Moroccan CIN number.']
valid = {
'D1': 'D1',
'DZ1': 'DZ1',
'D23': 'D23',
'DR23': 'DR23',
'D345': 'D345',
'DR345': 'DR345',
'D3454': 'D3454',
'DT3454': 'DT3454',
'D34546': 'D34546',
'DG34546': 'DG34546',
'D345467': 'D345467',
'DH345467': 'DH345467',
'D3454673': 'D3454673',

}
invalid = {
'9': ['Ensure this value has at least 2 characters (it has 1).'] + error_format,
'T': ['Ensure this value has at least 2 characters (it has 1).'] + error_format,
'903': error_format,
'D034': error_format,
'DR034': error_format,
'RER45': error_format,
'T23456786': ['Ensure this value has at most 8 characters (it has 9).'] + error_format,
}
self.assertFieldOutput(MACinNumberField, valid, invalid)

0 comments on commit 7ac0c22

Please sign in to comment.