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

Iranian localflavor #283

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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 @@ -90,3 +90,4 @@ Authors
* Tom Forbes
* Venelin Stoykov
* Vladimir Nani
* Sayed Mohammad Hossein Torabi
13 changes: 13 additions & 0 deletions docs/localflavor/ir.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
IRAN (``ir``)
===============

Forms
-----

.. automodule:: localflavor.ir.forms
:members:

Data
----

.. autodata:: localflavor.ir.ir_provinces.PROVINCE_CHOICES
Empty file added localflavor/ir/__init__.py
Empty file.
54 changes: 54 additions & 0 deletions localflavor/ir/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# -*- coding: utf-8 -*-
"""AR-specific Form helpers."""

from __future__ import unicode_literals

from django.core.validators import EMPTY_VALUES
from django.forms import ValidationError
from django.forms.fields import CharField, RegexField, Select
from django.utils.translation import ugettext_lazy as _

from .ir_provinces import PROVINCE_CHOICES




class IRProvinceSelect(Select):
"""A Select widget that uses a list of Iranian provinces/autonomous cities as its choices."""

def __init__(self, attrs=None):
super(IRProvinceSelect, self).__init__(attrs, choices=PROVINCE_CHOICES)




class IRPostlCodeField(RegexField):
"""
A field that accepts a 'classic' XXXXXXXXXX Postal Code o.

See:
https://en.wikipedia.org/wiki/List_of_postal_codes
"""

default_error_messages = {
'invlid': _("Enter a postal code in the format xxxxxxxxxx")
}

def __init__(self, max_length=10, min_length=11, *args, **kwargs):
super(IRPostlCodeField,self).__init__(r'^\d{4}$|^[A-HJ-NP-Za-hj-np-z]\d{4}\D{3}$',
max_length,
min_length,
*args,
**kwargs
)

def clean(self, value):
value = super(IRPostlCodeField,self).clean(value)
if value in EMPTY_VALUES:
return ''
if len(value) != 10 :
raise ValidationError(self.error_messages['invalid'])
if len(value) == 10:
return '%s%s%s' % (value[0].upper(), value[1:5], value[5:].upper())
return value

39 changes: 39 additions & 0 deletions localflavor/ir/ir_provinces.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.utils.translation import ugettext_lazy as _

#: A list of Iranian provinces according to https://en.wikipedia.org/wiki/Provinces_of_Iran
PROVINCE_CHOICES = (
('ALB', _('Alborz')),
('ARD', _('Ardabil')),
('AZE', _('Azerbaijan, East')),
('AZW', _('Azerbaijan, West')),
('BSH', _('Bushehr')),
('CMB', _('Chahar Mahaal and Bakhtiari')),
('FAR', _('Fars')),
('GIL', _('Gilan')),
('GOL', _('Golestan')),
('HAM', _('Hamadan')),
('HOR'), _('Hormozgān'),
('ILA', _('Ilam')),
('ISF', _('Isfahan')),
('KES', _('Kerman')),
('KER', _('Kermanshah')),
('KHN', _('Khorasan, North')),
('KHR', _('Khorasan, Razavi')),
('KHS', _('Khorasan, South')),
('KHU', _('Khuzestan')),
('KBA', _('Kohgiluyeh and Boyer-Ahmad')),
('KUR', _('Kurdistan')),
('LOR', _('Lorestan')),
('MAR', _('Markazi')),
('MAZ', _('Mazandaran')),
('QAZ', _('Qazvin')),
('QOM', _('Qom')),
('SEM', _('Semnan')),
('SBA'), _('Sistan and Baluchestan'),
('TEH', _('Tehran')),
('YZD', _('Yazd')),
('ZNJ', _('Zanjan'))
)
58 changes: 58 additions & 0 deletions tests/test_ir.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from __future__ import unicode_literals

from django.test import SimpleTestCase

from localflavor.ir.forms import IRPostlCodeField,IRProvinceSelect

class IRLocalFlavourTests(SimpleTestCase):
def test_IRProvinceSelect(self):
f = IRProvinceSelect()
out ='''<select name="provinces">
<option value="ALB">Alborz</option>
<option value="ARD">Ardabil</option>
<option value="AZE">Azerbaijan, East</option>
<option value="AZW">Azerbaijan, West</option>
<option value="BSH">Bushehr</option>
<option value="CMB">Chahar Mahaal and Bakhtiari</option>
<option value="FAR">Fars</option>
<option value="GIL">Gilan</option>
<option value="GOL">Golestan</option>
<option value="HAM">Hamadan</option>
<option value="HOR">Hormozgān</option>
<option value="ILA">Iliam</option>
<option value="ISF">Isfahan</option>
<option value="KES">Kerman</option>
<option value="KHN">Khorasan, North</option>
<option value="KHR">Khorasan, Razavi</option>
<option value="KHS">Khorasan, South</option>
<option value="KHU">Khuzestan</option>
<option value="KBA">Kohgiluyeh and Boyer-Ahmad</option>
<option value="KUR">Kurdistan</option>
<option value="LOR">Lorestan</option>
<option value="MAR">Markazi</option>
<option value="MAZ">Mazandaran</option>
<option value="QAZ">Qazvin</option>
<option value="QOM">Qom</option>
<option value="SEM">Semnan</option>
<option value="SBA">Sistan and Baluchestan</option>
<option value="TEH">Tehran</option>
<option value="YZD">Yazd</option>
<option value="ZNJ">Zanjan</option>
</select>'''
self.assertHTMLEqual(f.render('provinces'),'ALB', out)
def test_IRPostalCodeField(self):
error_format = ['Enter a postal code in the format xxxxxxxxxx .']
error_atmost = ['Ensure this value has at most 10 characters (it has 11).']
error_atleast = ['Ensure this value has at least 10 characters (it has 9).']

valid = {
'5987456987' : '9874698741',
'5987456321' : '4895785787',
'c464646464' : '4848748487',
}

invalid = {
'4545' : error_atmost+error_format,
'464676467676' : error_atleast+error_format,
}
self.assertFieldOutput(IRPostlCodeField, valid, invalid)