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
63 changes: 63 additions & 0 deletions localflavor/se/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# -*- coding: utf-8 -*-
from django.db import models
from django.utils.translation import ugettext_lazy as _
from .se_counties import COUNTY_CHOICES, NUMERICAL_COUNTY_CODE_CHOICES, FULL_COUNTY_NAME_CHOICES


class SECountyField(models.CharField):
"""
A standard `CharField` with `choices` defaulting to `localflavor.se.se_counties.COUNTY_CHOICES`.

As the Swedish counties can be named in at least two ways (e.g. "Stockholm" vs. "Stockholm County"),
and there has historically been at least two numeric systems used for counties, this field adds the
following accessors to let you access all of this data:

* `get_county_numerical_code()` - Numiercal code (01-25)
* `get_county_full_name()` - Full Name (e.g. Stockholm County)
* `get_county_short_name()`- Short name (e.g. Stockholm)

Example:

.. code-block:: python

from django.db import models
from localflavor.se.models import SECountyField
from .se_counties import COUNTY_CHOICES, NUMERICAL_COUNTY_CODE_CHOICES, FULL_COUNTY_NAME_CHOICES

class MyModel(models.Model):
county = SECountyField()

obj = MyModel(county='AB')

obj.get_county_numerical_code() # '01'
obj.get_county_full_name() # 'Stockholm County'
obj.get_county_short_name() # 'Stockholm'
obj.get_county_display() # 'Stockholm'
obj.county # 'AB'

https://sv.wikipedia.org/wiki/Sveriges_län
https://en.wikipedia.org/wiki/Counties_of_Sweden
"""
description = _('A Swedish County')

def __init__(self, *args, **kwargs):

defaults = {
'max_length': 2,
'choices': COUNTY_CHOICES,
}
defaults.update(kwargs)

super(SECountyField, self).__init__(*args, **defaults)

def contribute_to_class(self, cls, name):
if not cls._meta.abstract:
_get_numerical_code = lambda self: dict(NUMERICAL_COUNTY_CODE_CHOICES).get(getattr(self, name), None)
_get_full_name = lambda self: dict(FULL_COUNTY_NAME_CHOICES).get(getattr(self, name), None)
_get_short_name = lambda self: dict(COUNTY_CHOICES).get(getattr(self, name), None)

cls.add_to_class("get_" + name + "_numerical_code", _get_numerical_code)
cls.add_to_class("get_" + name + "_full_name", _get_full_name)
cls.add_to_class("get_" + name + "_short_name", _get_short_name)

super(SECountyField, self).contribute_to_class(cls, name)
59 changes: 59 additions & 0 deletions localflavor/se/se_counties.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,62 @@
('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_CODE_CHOICES = (
('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_NAME_CHOICES = (
('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'),),
)