Ticket #3879: no_localflavor.diff

File no_localflavor.diff, 4.0 KB (added by thebjorn <bp@…>, 8 years ago)
  • no/no_municipalities.py

     
     1# -*- coding: iso-8859-1 -*-
     2"""
     3An alphabetical list of Norwegian municipalities (fylker) fro use as `choices`
     4in a formfield.
     5
     6This exists in this standalone file so that it's on ly imported into memory
     7when explicitly needed.
     8"""
     9
     10MUNICIPALITY_CHOICES = (
     11    ('akershus', u'Akershus'),
     12    ('austagder', u'Aust-Agder'),
     13    ('buskerud', u'Buskerud'),
     14    ('finnmark', u'Finnmark'),
     15    ('hedmark', u'Hedmark'),
     16    ('hordaland', u'Hordaland'),
     17    ('janmayen', u'Jan Mayen'),
     18    ('moreogromsdal', u'Møre og Romsdal'),
     19    ('nordtrondelag', u'Nord-Trøndelag'),
     20    ('nordland', u'Nordland'),
     21    ('oppland', u'Oppland'),
     22    ('oslo', u'Oslo'),
     23    ('rogaland', u'Rogaland'),
     24    ('sognogfjordane', u'Sogn og Fjordane'),
     25    ('svalbard', u'Svalbard'),
     26    ('sortrondelag', u'Sør-Trøndelag'),
     27    ('telemark', u'Telemark'),
     28    ('troms', u'Troms'),
     29    ('vestagder', u'Vest-Agder'),
     30    ('vestfold', u'Vestfold'),
     31    ('ostfold', u'Østfold')
     32)
  • no/forms.py

     
     1# -*- coding: iso-8859-1 -*-
     2"""
     3NO-specific Form helpers
     4"""
     5
     6import re, datetime
     7from django.newforms import ValidationError
     8from django.newforms.fields import Field, RegexField, Select, EMPTY_VALUES
     9from django.utils.translation import gettext
     10
     11class NOZipCodeField(RegexField):
     12    def __init__(self, *args, **kwargs):
     13        super(NOZipCodeField, self).__init__(r'^\d{4}$',
     14            max_length=None, min_length=None,
     15            error_message=gettext(u'Enter a zip code in the format XXXX.'),
     16            *args, **kwargs)
     17
     18class NOMunicipalitySelect(Select):
     19    """
     20    A Select widget that uses a list of Norwegian municipalities (fylker)
     21    as its choices.
     22    """
     23    def __init__(self, attrs=None):
     24        from no_municipalities import MUNICIPALITY_CHOICES # relative import
     25        super(NOMunicipalitySelect, self).__init__(attrs, choices=MUNICIPALITY_CHOICES)
     26
     27class NOSocialSecurityNumber(Field):
     28    """
     29    Algorithm is documented at http://no.wikipedia.org/wiki/Personnummer
     30    """
     31    def clean(self, value):
     32        super(NOSocialSecurityNumber, self).clean(value)
     33        if value in EMPTY_VALUES:
     34            return u''
     35
     36        msg = gettext(u'Enter a valid Norwegian social security number.')
     37        if not re.match(r'^\d{11}$', value):
     38            raise ValidationError(msg)
     39
     40        day = int(value[:2])
     41        month = int(value[2:4])
     42        year2 = int(value[4:6])
     43
     44        inum = int(value[6:9])
     45        self.birthday = None
     46        try:
     47            if 000 <= inum < 500:
     48                self.birthday = datetime.date(1900+year2, month, day)
     49            if 500 <= inum < 750:
     50                self.birthday = datetime.date(1800+year2, month, day)
     51            if 500 <= inum < 1000:
     52                self.birthday = datetime.date(2000+year2, month, day)
     53        except ValueError:
     54            raise ValidationError(msg)
     55
     56        sexnum = int(value[8])
     57        if sexnum % 2 == 0:
     58            self.gender = 'F'
     59        else:
     60            self.gender = 'M'
     61       
     62        digits = map(int, list(value))
     63        weight_1 = [3, 7, 6, 1, 8, 9, 4, 5, 2, 1, 0]
     64        weight_2 = [5, 4, 3, 2, 7, 6, 5, 4, 3, 2, 1]
     65
     66        def multiply_reduce(aval, bval):
     67            return sum((a * b) for (a, b) in zip(aval, bval))
     68
     69        if multiply_reduce(digits, weight_1) % 11 != 0:
     70            raise ValidationError(msg)
     71        if multiply_reduce(digits, weight_2) % 11 != 0:
     72            raise ValidationError(msg)
     73
     74        return value
     75   
Back to Top