Skip to content

Commit be0475e

Browse files
Localflavor Sri Lanka: Support for Sri Lanka added in django-localflavor (#493)
1 parent cf484b4 commit be0475e

13 files changed

Lines changed: 367 additions & 1 deletion

File tree

docs/authors.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,4 @@ Authors
137137
* Xabi Bello
138138
* Abhineet Tamrakar
139139
* Tudor Amariei
140+
* Dishan Sachin

docs/changelog.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ instructions on how to suppress this warning once the migration has been complet
3131

3232
New flavors:
3333

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

3637
New fields for existing flavors:
3738

docs/localflavor/lk.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Sri Lanka (``lk``)
2+
==================
3+
4+
Forms
5+
-----
6+
7+
.. automodule:: localflavor.lk.forms
8+
:members:
9+
10+
Models
11+
------
12+
13+
.. automodule:: localflavor.lk.models
14+
:members:
15+
16+
Data
17+
----
18+
.. autodata:: localflavor.lk.lk_provinces.PROVINCES
19+
.. autodata:: localflavor.lk.lk_districts.DISTRICTS

localflavor/lk/__init__.py

Whitespace-only changes.

localflavor/lk/forms.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""Sri Lanka specific Form helpers."""
2+
3+
from django.forms.fields import RegexField, Select
4+
from django.utils.translation import gettext_lazy as _
5+
6+
from .lk_provinces import PROVINCES
7+
from .lk_districts import DISTRICTS
8+
9+
10+
class LKPostalCodeFormField(RegexField):
11+
"""
12+
A form field that accepts Sri Lanka postal code.
13+
Format : NNNNN
14+
15+
Postal codes: https://en.wikipedia.org/wiki/Postal_codes_in_Sri_Lanka
16+
17+
.. versionadded:: 5.0
18+
"""
19+
20+
default_error_messages = {
21+
'invalid': _('Enter a postal code in format NNNNN'),
22+
}
23+
24+
def __init__(self, **kwargs):
25+
super().__init__(r'^[0-9]{5}$', **kwargs)
26+
27+
28+
class LKProvinceSelect(Select):
29+
"""
30+
A Select widget with option to select a provinces from
31+
list of all provinces of Sri Lanka.
32+
33+
.. versionadded:: 5.0
34+
"""
35+
36+
def __init__(self, attrs=None):
37+
super().__init__(attrs, choices=PROVINCES)
38+
39+
40+
class LKDistrictSelect(Select):
41+
"""
42+
A Select widget with option to select a districts from
43+
list of all districts of Sri Lanka.
44+
45+
.. versionadded:: 5.0
46+
"""
47+
48+
def __init__(self, attrs=None):
49+
super().__init__(attrs, choices=DISTRICTS)

localflavor/lk/lk_districts.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
"""
2+
List of Districts of Sri Lanka.
3+
4+
Source: https://en.wikipedia.org/wiki/Districts_of_Sri_Lanka
5+
6+
Sri Lanka districts list choices are in this format:
7+
8+
('name_of_districts',_('Name of districts')),
9+
10+
eg.
11+
('name_of_district', _('Name of district')),
12+
"""
13+
14+
from django.utils.translation import gettext_lazy as _
15+
16+
# list of districts in Central
17+
CENTRAL_DISTRICTS = [
18+
('kandy', _('Kandy')),
19+
('matale', _('Matale')),
20+
('nuwara_eliya', _('Nuwara Eliya')),
21+
]
22+
23+
# list of districts in North Central
24+
NORTH_CENTRAL_DISTRICTS = [
25+
('anuradhapura', _('Anuradhapura')),
26+
('polonnaruwa', _('Polonnaruwa')),
27+
]
28+
29+
# list of districts in Northern
30+
NORTHERN_DISTRICTS = [
31+
('jaffna', _('Jaffna')),
32+
('kilinochchi', _('Kilinochchi')),
33+
('mannar', _('Mannar')),
34+
('vavuniya', _('Vavuniya')),
35+
('mullativu', _('Mullativu')),
36+
('alambil', _('Alambil')),
37+
]
38+
39+
# list of districts in Eastern
40+
EASTERN_DISTRICTS = [
41+
('ampara', _('Ampara')),
42+
('batticaloa', _('Batticaloa')),
43+
('trincomalee', _('Trincomalee')),
44+
]
45+
46+
# list of districts in North Western
47+
NORTH_WESTERN_DISTRICTS = [
48+
('kurunagala', _('Kurunagala')),
49+
('puttalam', _('Puttalam')),
50+
]
51+
52+
# list of districts in Southern
53+
SOUTHERN_DISTRICTS = [
54+
('galle', _('Galle')),
55+
('hambanthota', _('Hambanthota')),
56+
('mathara', _('Mathara')),
57+
]
58+
59+
# list of districts in Uva
60+
UVA_DISTRICTS = [
61+
('badulla', _('Badulla')),
62+
('monaragala', _('Monaragala')),
63+
]
64+
65+
# list of districts in Sabaragamuwa
66+
SABARAGAMUWA_DISTRICTS = [
67+
('kegalle', _('Kegalle')),
68+
('rathnapura', _('Rathnapura')),
69+
]
70+
71+
# list of districts in Western
72+
WESTERN_DISTRICTS = [
73+
('colombo', _('Colombo')),
74+
('gampaha', _('Gampaha')),
75+
('kaluthara', _('Kaluthara')),
76+
]
77+
78+
# Combining all the district lists from different provinces into a single list
79+
DISTRICTS = CENTRAL_DISTRICTS + NORTH_CENTRAL_DISTRICTS + NORTHERN_DISTRICTS + EASTERN_DISTRICTS + NORTH_WESTERN_DISTRICTS + SOUTHERN_DISTRICTS + UVA_DISTRICTS + SABARAGAMUWA_DISTRICTS + WESTERN_DISTRICTS
80+
81+
# Alphabetically sorting the list of all districts based on their names (first element of each tuple)
82+
DISTRICTS.sort(key=lambda district: district[0])

localflavor/lk/lk_provinces.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
List of Provinces of Sri Lanka.
3+
4+
Source: https://en.wikipedia.org/wiki/Provinces_of_Sri_Lanka
5+
6+
Sri Lanka province list choices are in this format:
7+
8+
('name_of_province',_('Name of province')),
9+
10+
e.g.
11+
('central', _('Central')),
12+
"""
13+
14+
from django.utils.translation import gettext_lazy as _
15+
16+
PROVINCES = [
17+
('central', _('Central')),
18+
('eastern', _('Eastern')),
19+
('north_central', _('North Central')),
20+
('north_western', _('North Western')),
21+
('northern', _('Northern')),
22+
('sabaragamuwa', _('Sabaragamuwa')),
23+
('southern', _('Southern')),
24+
('uva', _('Uva')),
25+
('western', _('Western')),
26+
]

localflavor/lk/models.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
"""Sri Lanka specific Model fields"""
2+
3+
import re
4+
5+
from django.core.validators import RegexValidator
6+
from django.db import models
7+
from django.utils.translation import gettext_lazy as _
8+
9+
from .forms import LKPostalCodeFormField
10+
from .lk_districts import DISTRICTS
11+
from .lk_provinces import PROVINCES
12+
13+
14+
class LKPostalCodeValidator(RegexValidator):
15+
"""
16+
A validator for Sri Lanka Postal Codes.
17+
"""
18+
default_error_messages = {
19+
'invalid': _('Enter a postal code in format NNNNN'),
20+
}
21+
22+
def __init__(self, *args, **kwargs):
23+
super().__init__(re.compile(r'^[0-9]{5}$'), *args, **kwargs)
24+
25+
26+
class LKPostalCodeField(models.CharField):
27+
"""
28+
A model field that accepts Sri Lanka postal codes.
29+
Format: NNNNN
30+
Source: https://en.wikipedia.org/wiki/Postal_codes_in_Sri_Lanka
31+
32+
.. versionadded:: 5.0
33+
"""
34+
description = _("Postal Code")
35+
36+
def __init__(self, *args, **kwargs):
37+
kwargs['max_length'] = 5
38+
super().__init__(*args, **kwargs)
39+
self.validators.append(LKPostalCodeValidator())
40+
41+
def formfield(self, **kwargs):
42+
defaults = {'form_class': LKPostalCodeFormField}
43+
defaults.update(kwargs)
44+
return super().formfield(**defaults)
45+
46+
47+
class LKDistrictField(models.CharField):
48+
"""
49+
A model field that provides an option to select
50+
a district from the list of all Sri Lanka districts.
51+
52+
.. versionadded:: 5.0
53+
"""
54+
55+
def __init__(self, *args, **kwargs):
56+
kwargs['choices'] = DISTRICTS
57+
kwargs['max_length'] = 15
58+
super().__init__(*args, **kwargs)
59+
60+
def deconstruct(self):
61+
name, path, args, kwargs = super().deconstruct()
62+
del kwargs['choices']
63+
return name, path, args, kwargs
64+
65+
66+
class LKProvinceField(models.CharField):
67+
"""
68+
A model field that provides an option to select
69+
a province from the list of all Sri Lanka provinces.
70+
71+
.. versionadded:: 5.0
72+
"""
73+
74+
def __init__(self, *args, **kwargs):
75+
kwargs['choices'] = PROVINCES
76+
kwargs['max_length'] = 15
77+
super().__init__(*args, **kwargs)
78+
79+
def deconstruct(self):
80+
name, path, args, kwargs = super().deconstruct()
81+
del kwargs['choices']
82+
return name, path, args, kwargs

tests/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
'tests.test_fr',
1717
'tests.test_generic',
1818
'tests.test_gh',
19+
'tests.test_lk',
1920
'tests.test_md',
2021
'tests.test_mk',
2122
'tests.test_mx',

tests/test_lk/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)