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

Localflavor Sri Lanka: Support for Sri Lanka added in django-localflavor #493

Open
wants to merge 22 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 @@ -130,3 +130,4 @@ Authors
* Vladimir Nani
* Xabi Bello
* Abhineet Tamrakar
* Dishan Sachin
3 changes: 2 additions & 1 deletion docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ Changelog

New flavors:

- None
- Sri Lanka LocalFlavor: Support for Sri Lanka added
(`gh-493 <https://github.com/django/django-localflavor/pull/493>`_).

New fields for existing flavors:

Expand Down
19 changes: 19 additions & 0 deletions docs/localflavor/lk.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Sri Lanka (``lk``)
==============

Forms
-----

.. automodule:: localflavor.lk.forms
:members:

Models
------

.. automodule:: localflavor.lk.models
:members:

Data
----
.. autodata:: localflavor.lk.lk_provinces.PROVINCES
.. autodata:: localflavor.lk.lk_districts.DISTRICTS
Empty file added localflavor/lk/__init__.py
Empty file.
43 changes: 43 additions & 0 deletions localflavor/lk/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""Sri Lanka specific Form helpers."""

from django.forms.fields import RegexField, Select
from django.utils.translation import gettext_lazy as _

from .lk_provinces import PROVINCES
from .lk_districts import DISTRICTS


class LKPostalCodeFormField(RegexField):
"""
A form field that accepts Sri Lanka postal code.
Format : XXXXX

Postal codes: https://en.wikipedia.org/wiki/Postal_codes_in_Sri_Lanka
"""

default_error_messages = {
'invalid': _('Enter a postal code in format XXXXX'),
}

def __init__(self, **kwargs):
super().__init__(r'^\d{5}$', **kwargs)


class LKProvinceSelect(Select):
"""
A Select widget with option to select a provinces from
list of all provinces of Sri Lanka.
"""

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


class LKDistrictSelect(Select):
"""
A Select widget with option to select a districts from
list of all districts of Sri Lanka.
"""

def __init__(self, attrs=None):
super().__init__(attrs, choices=DISTRICTS)
82 changes: 82 additions & 0 deletions localflavor/lk/lk_districts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
"""
List of Districts of Sri Lanka.

Source: https://en.wikipedia.org/wiki/Districts_of_Sri_Lanka

Sri Lanka districts list choices are in this format:

('name_of_districts',_('Name of districts')),

eg.
('name_of_district', _('Name of district')),
"""

from django.utils.translation import gettext_lazy as _

# list of districts in Central
CENTRAL_DISTRICTS = [
('kandy', _('Kandy')),
('matale', _('Matale')),
('nuwara_eliya', _('Nuwara Eliya')),
]

# list of districts in North Central
NORTH_CENTRAL_DISTRICTS = [
('anuradhapura', _('Anuradhapura')),
('polonnaruwa', _('Polonnaruwa')),
]

# list of districts in Northern
NORTHERN_DISTRICTS = [
('jaffna', _('Jaffna')),
('kilinochchi', _('Kilinochchi')),
('mannar', _('Mannar')),
('vavuniya', _('Vavuniya')),
('mullativu', _('Mullativu')),
('alambil', _('Alambil')),
]

# list of districts in Eastern
EASTERN_DISTRICTS = [
('ampara', _('Ampara')),
('batticaloa', _('Batticaloa')),
('trincomalee', _('Trincomalee')),
]

# list of districts in North Western
NORTH_WESTERN_DISTRICTS = [
('kurunagala', _('Kurunagala')),
('puttalam', _('Puttalam')),
]

# list of districts in Southern
SOUTHERN_DISTRICTS = [
('galle', _('Galle')),
('hambanthota', _('Hambanthota')),
('mathara', _('Mathara')),
]

# list of districts in Uva
UVA_DISTRICTS = [
('badulla', _('Badulla')),
('monaragala', _('Monaragala')),
]

# list of districts in Sabaragamuwa
SABARAGAMUWA_DISTRICTS = [
('kegalle', _('Kegalle')),
('rathnapura', _('Rathnapura')),
]

# list of districts in Western
WESTERN_DISTRICTS = [
('colombo', _('Colombo')),
('gampaha', _('Gampaha')),
('kaluthara', _('Kaluthara')),
]

# Combining all the district lists from different provinces into a single list
DISTRICTS = CENTRAL_DISTRICTS + NORTH_CENTRAL_DISTRICTS + NORTHERN_DISTRICTS + EASTERN_DISTRICTS + NORTH_WESTERN_DISTRICTS + SOUTHERN_DISTRICTS + UVA_DISTRICTS + SABARAGAMUWA_DISTRICTS + WESTERN_DISTRICTS

# Alphabetically sorting the list of all districts based on their names (first element of each tuple)
DISTRICTS.sort(key=lambda district: district[0])
26 changes: 26 additions & 0 deletions localflavor/lk/lk_provinces.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""
List of Provinces of Sri Lanka.

Source: https://en.wikipedia.org/wiki/Provinces_of_Sri_Lanka

Sri Lanka province list choices are in this format:

('name_of_province',_('Name of province')),

eg.
('central', _('Central')),
"""

from django.utils.translation import gettext_lazy as _

PROVINCES = [
('central', _('Central')),
('north_central', _('North Central')),
('northern', _('Northern')),
('eastern', _('Eastern')),
('north_western', _('North Western')),
('southern', _('Southern')),
('uva', _('Uva')),
('sabaragamuwa', _('Sabaragamuwa')),
('western', _('Western')),
]
79 changes: 79 additions & 0 deletions localflavor/lk/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
"""Sri Lanka specific Model fields"""

import re

from django.core.validators import RegexValidator
from django.db import models
from django.utils.translation import gettext_lazy as _

from .forms import LKPostalCodeFormField
from .lk_districts import DISTRICTS
from .lk_provinces import PROVINCES


class LKPostalCodeValidator(RegexValidator):
"""
A validator for Sri Lanka Postal Codes.
"""
default_error_messages = {
'invalid': _('Enter a postal code in format XXXXX'),
}

def __init__(self, *args, **kwargs):
super().__init__(re.compile(r'^\d{5}$'), *args, **kwargs)


class LKPostalCodeField(models.CharField):
"""
A model field that accepts Sri Lanka postal codes.
Format: XXXXX
Source: https://en.wikipedia.org/wiki/Postal_codes_in_Sri_Lanka
.. versionadded:: 4.0
"""
description = _("Postal Code")

def __init__(self, *args, **kwargs):
kwargs['max_length'] = 5
super().__init__(*args, **kwargs)
self.validators.append(LKPostalCodeValidator())

def formfield(self, **kwargs):
defaults = {'form_class': LKPostalCodeFormField}
defaults.update(kwargs)
return super().formfield(**defaults)


class LKDistrictField(models.CharField):
"""
A model field that provides an option to select
a district from the list of all Sri Lanka districts.
.. versionadded:: 4.0
"""

def __init__(self, *args, **kwargs):
kwargs['choices'] = DISTRICTS
kwargs['max_length'] = 15
super().__init__(*args, **kwargs)

def deconstruct(self):
name, path, args, kwargs = super().deconstruct()
del kwargs['choices']
return name, path, args, kwargs


class LKProvinceField(models.CharField):
"""
A model field that provides an option to select
a province from the list of all Sri Lanka provinces.
.. versionadded:: 4.0
"""

def __init__(self, *args, **kwargs):
kwargs['choices'] = PROVINCES
kwargs['max_length'] = 15
super().__init__(*args, **kwargs)

def deconstruct(self):
name, path, args, kwargs = super().deconstruct()
del kwargs['choices']
return name, path, args, kwargs
1 change: 1 addition & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
'tests.test_gh',
'tests.test_np',
'tests.test_ca',
'tests.test_lk',
'tests.test_generic',
]

Expand Down
Empty file added tests/test_lk/__init__.py
Empty file.
8 changes: 8 additions & 0 deletions tests/test_lk/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.db import models

from localflavor.lk.models import (LKDistrictField, LKProvinceField)


class LKPlace(models.Model):
district = LKDistrictField(blank=True)
province = LKProvinceField(blank=True)
46 changes: 46 additions & 0 deletions tests/test_lk/selectfields_html.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""Option for all select box fields"""

districts_select = """
<select name="district">
<option value="kandy" selected>Kandy</option>
<option value="matale">Matale</option>
<option value="nuwara_eliya">Nuwara Eliya</option>
<option value="anuradhapura">Anuradhapura</option>
<option value="polonnaruwa">Polonnaruwa</option>
<option value="jaffna">Jaffna</option>
<option value="kilinochchi">Kilinochchi</option>
<option value="mannar">Mannar</option>
<option value="vavuniya">Vavuniya</option>
<option value="mullativu">Mullativu</option>
<option value="alambil">Alambil</option>
<option value="ampara">Ampara</option>
<option value="batticaloa">Batticaloa</option>
<option value="trincomalee">Trincomalee</option>
<option value="kurunagala">Kurunagala</option>
<option value="puttalam">Puttalam</option>
<option value="galle">Galle</option>
<option value="hambanthota">Hambanthota</option>
<option value="mathara">Mathara</option>
<option value="badulla">Badulla</option>
<option value="monaragala">Monaragala</option>
<option value="kegalle">Kegalle</option>
<option value="rathnapura">Rathnapura</option>
<option value="colombo">Colombo</option>
<option value="gampaha">Gampaha</option>
<option value="kaluthara">Kaluthara</option>
</select>
"""

provinces_select = """
<select name="province">
<option value="central" selected>Central</option>
<option value="north_central">North Central</option>
<option value="northern">Northern</option>
<option value="eastern">Eastern</option>
<option value="north_western">North Western</option>
<option value="southern">Southern</option>
<option value="uva">Uva</option>
<option value="sabaragamuwa">Sabaragamuwa</option>
<option value="western">Western</option>
</select>
"""
50 changes: 50 additions & 0 deletions tests/test_lk/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from django.test import TransactionTestCase
from localflavor.lk.forms import LKDistrictSelect, LKPostalCodeFormField, LKProvinceSelect

from .selectfields_html import districts_select, provinces_select
from .models import LKPlace


class SriLanakDetailsTests(TransactionTestCase):
"""
This Test class tests all the selectbox
fields and Postal Code Fields.
"""

def test_LKDistrictSelect(self):
field = LKDistrictSelect()
self.assertHTMLEqual(field.render('district', 'kandy'), districts_select)

def test_LKProvinceSelect(self):
field = LKProvinceSelect()
self.assertHTMLEqual(field.render('province', 'central'), provinces_select)

def test_LKPostalCodeFieldTest(self):
error_format = ['Enter a postal code in format XXXXX']
valid = {
'12345': '12345',
'00000': '00000',
'11111': '11111'
}
invalid = {
'12345_123': error_format,
'1234-123': error_format,
'-232': error_format,
'abc_den': error_format,
'2345-': error_format,
}
self.assertFieldOutput(LKPostalCodeFormField, valid, invalid)

def test_LKDistrictField(self):
place = LKPlace()
place.district = 'kandy'
place.clean_fields()
place.save()
self.assertEqual(place.get_district_display(), 'Kandy')

def test_LKProvinceField(self):
place = LKPlace()
place.province = 'central'
place.clean_fields()
place.save()
self.assertEqual(place.get_province_display(), 'Central')
Loading