Skip to content

Commit

Permalink
Adding support for Kuwait governorates (#231)
Browse files Browse the repository at this point in the history
Add support for Kuwait governorates.
  • Loading branch information
burhan authored and benkonrath committed Nov 5, 2016
1 parent edaf9d5 commit 985f757
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
3 changes: 2 additions & 1 deletion docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ New fields for existing flavors:

- Added MXCLABEField model and form fields.
(`gh-227 <https://github.com/django/django-localflavor/pull/227>`_).

- Added AUTaxFileNumberField model and form fields.
(`gh-238 <https://github.com/django/django-localflavor/pull/238>`_)
- Added KWGovernorateSelect field to easily select Kuwait governorates.
(`gh-231 <https://github.com/django/django-localflavor/pull/231>`_).

Modifications to existing flavors:

Expand Down
14 changes: 13 additions & 1 deletion localflavor/kw/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@

from django.core.validators import EMPTY_VALUES
from django.forms import ValidationError
from django.forms.fields import Field
from django.forms.fields import Field, Select
from django.utils.translation import gettext_lazy as _

from .kw_governorates import GOVERNORATE_CHOICES

id_re = re.compile(r'''^(?P<initial>\d{1})
(?P<yy>\d\d)
(?P<mm>\d\d)
Expand Down Expand Up @@ -73,3 +75,13 @@ def clean(self, value):
raise ValidationError(self.error_messages['invalid'])

return value


class KWGovernorateSelect(Select):
"""
A Select widget that uses a list of Kuwait governorates
as its choices.
"""
def __init__(self, attrs=None):
super(KWGovernorateSelect, self).__init__(attrs,
choices=GOVERNORATE_CHOICES)
18 changes: 18 additions & 0 deletions localflavor/kw/kw_governorates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.utils.translation import ugettext_lazy as _

# List of governorates as defined by ISO_3166-2
# For reference; see https://en.wikipedia.org/wiki/ISO_3166-2:KW
# Note that these are governorates and not subdivisions

# These are the English names
GOVERNORATE_CHOICES = (
('AH', _('Ahmadi')),
('FA', _('Farwaniyah')),
('JA', _('Jahra')),
('KU', _('Capital')),
('HA', _('Hawalli')),
('MU', _('Mubarak Al Kabir')),
)
14 changes: 13 additions & 1 deletion tests/test_kw.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from django.test import SimpleTestCase

from localflavor.kw.forms import KWCivilIDNumberField
from localflavor.kw.forms import KWCivilIDNumberField, KWGovernorateSelect


class KWLocalFlavorTests(SimpleTestCase):
Expand All @@ -20,3 +20,15 @@ def test_KWCivilIDNumberField(self):
'2*9332013455': error_invalid,
}
self.assertFieldOutput(KWCivilIDNumberField, valid, invalid)

def test_KWGovernorateSelect(self):
f = KWGovernorateSelect()
result = '''<select name="governorates">
<option value="AH">Ahmadi</option>
<option value="FA">Farwaniyah</option>
<option value="JA">Jahra</option>
<option value="KU" selected="selected">Capital</option>
<option value="HA">Hawalli</option>
<option value="MU">Mubarak Al Kabir</option>
</select>'''
self.assertHTMLEqual(f.render('governorates', 'KU'), result)

0 comments on commit 985f757

Please sign in to comment.