diff --git a/localflavor/se/models.py b/localflavor/se/models.py new file mode 100644 index 000000000..b1d517571 --- /dev/null +++ b/localflavor/se/models.py @@ -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) diff --git a/localflavor/se/se_counties.py b/localflavor/se/se_counties.py index b6e1c5ed9..7fa346b73 100644 --- a/localflavor/se/se_counties.py +++ b/localflavor/se/se_counties.py @@ -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'),), +)