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 full names and numerical codes for Swedish counties #117

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
24 changes: 24 additions & 0 deletions localflavor/se/decorators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from .se_counties import numerical_county_code, full_county_name


def county_decorator(field_names=()):
"""Decorator that adds get_FIELDNAME_numerical_code() and get_FIELDNAME_full_name() methods
to django models for all FIELDNAME values supplied in the field_names tuple. Ideally
this can be used on all model fields that uses COUNTY_CHOICES for the choices argument.

@county_decorator(field_names('field',))
class MyClass:
field = models.CharField(choices=se_counties=COUNTY_CHOICES)
"""

def decorate(cls):
for field_name in field_names:

_get_numerical_code = lambda self: numerical_county_code(getattr(self, field_name))
_get_full_name = lambda self: full_county_name(getattr(self, field_name))

setattr(cls, "get_" + field_name + "_numerical_code", _get_numerical_code)
setattr(cls, "get_" + field_name + "_full_name", _get_full_name)

return cls
return decorate
71 changes: 71 additions & 0 deletions localflavor/se/se_counties.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,74 @@
('Y', _('Västernorrland')),
('Z', _('Jämtland')),
)

#: A dictionary of numerical county codes, with alphabetical codes
#: as keys and the more modern numerical codes as values.
#:
#: Values taken from https://sv.wikipedia.org/wiki/Sveriges_län,
#: and code system described at https://sv.wikipedia.org/wiki/Länskod
#: and http://www.scb.se/sv_/Hitta-statistik/Regional-statistik-och-kartor/Regionala-indelningar/Lan-och-kommuner/Lan-och-kommuner-i-kodnummerordning/

NUMERICAL_COUNTY_CODES = {
'AB': '01',
'AC': '24',
'BD': '25',
'C': '03',
'D': '04',
'E': '05',
'F': '06',
'G': '07',
'H': '08',
'I': '09',
'K': '10',
'M': '12',
'N': '13',
'O': '14',
'S': '17',
'T': '18',
'U': '19',
'W': '20',
'X': '21',
'Y': '22',
'Z': '23',
}

#: A dictionary of full county names, as these are not as
#: somewhat in Swedish, e.g. "Skåne län" as opposed to
#: the more generic "Stockholms län" (ending with genitive case s)

FULL_COUNTY_NAMES = {
'AB': _('Stockholm County'),
'AC': _('Västerbotten County'),
'BD': _('Norrbotten County'),
'C': _('Uppsala County'),
'D': _('Södermanland County'),
'E': _('Östergötland County'),
'F': _('Jönköping County'),
'G': _('Kronoberg County'),
'H': _('Kalmar County'),
'I': _('Gotland County'),
'K': _('Blekinge County'),
'M': _('Skåne County'),
'N': _('Halland County'),
'O': _('Västra Götaland County'),
'S': _('Värmland County'),
'T': _('Örebro County'),
'U': _('Västmanland County'),
'W': _('Dalarna County'),
'X': _('Gävleborg County'),
'Y': _('Västernorrland County'),
'Z': _('Jämtland County'),
}


def numerical_county_code(county_code):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both of those helpers won't be necessary as they just wrap dict functionality, which seems moot. Please remove.

"""Returns a numerical county code for the supplied alphabetical county code."""

return NUMERICAL_COUNTY_CODES.get(county_code, None)


def full_county_name(county_code):
"""Returns a full county name for the supplied alphabetical county code."""

return FULL_COUNTY_NAMES.get(county_code, None)