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

Added MACinNumberField to MA flavor #507

Merged
merged 3 commits into from
May 22, 2024
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 @@ -2,6 +2,7 @@ Authors
=======

* Aaron Boman
* Abdellah El Youssfi Alaoui
* Adam Taylor
* Adnane Belmadiaf
* Adonys Alea Boffill
Expand Down
2 changes: 1 addition & 1 deletion docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ 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:

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)
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)
Loading