2007-03-31 17:14:37 +08:00
|
|
|
"""
|
|
|
|
Norwegian-specific Form helpers
|
|
|
|
"""
|
|
|
|
|
2011-10-18 08:47:49 +08:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2011-07-13 17:35:51 +08:00
|
|
|
import re
|
|
|
|
import datetime
|
2011-10-18 08:47:49 +08:00
|
|
|
|
|
|
|
from django.contrib.localflavor.no.no_municipalities import MUNICIPALITY_CHOICES
|
2010-01-05 11:56:19 +08:00
|
|
|
from django.core.validators import EMPTY_VALUES
|
2008-07-19 09:22:26 +08:00
|
|
|
from django.forms import ValidationError
|
2010-01-05 11:56:19 +08:00
|
|
|
from django.forms.fields import Field, RegexField, Select
|
2008-06-18 21:10:05 +08:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2007-03-31 17:14:37 +08:00
|
|
|
|
2011-10-18 08:47:49 +08:00
|
|
|
|
2007-03-31 17:14:37 +08:00
|
|
|
class NOZipCodeField(RegexField):
|
2007-12-17 16:05:27 +08:00
|
|
|
default_error_messages = {
|
2008-06-18 21:10:05 +08:00
|
|
|
'invalid': _('Enter a zip code in the format XXXX.'),
|
2007-12-17 16:05:27 +08:00
|
|
|
}
|
|
|
|
|
2011-05-03 19:52:04 +08:00
|
|
|
def __init__(self, max_length=None, min_length=None, *args, **kwargs):
|
2007-03-31 17:14:37 +08:00
|
|
|
super(NOZipCodeField, self).__init__(r'^\d{4}$',
|
2011-05-03 19:52:04 +08:00
|
|
|
max_length, min_length, *args, **kwargs)
|
2007-03-31 17:14:37 +08:00
|
|
|
|
|
|
|
class NOMunicipalitySelect(Select):
|
|
|
|
"""
|
|
|
|
A Select widget that uses a list of Norwegian municipalities (fylker)
|
|
|
|
as its choices.
|
|
|
|
"""
|
|
|
|
def __init__(self, attrs=None):
|
|
|
|
super(NOMunicipalitySelect, self).__init__(attrs, choices=MUNICIPALITY_CHOICES)
|
|
|
|
|
|
|
|
class NOSocialSecurityNumber(Field):
|
|
|
|
"""
|
|
|
|
Algorithm is documented at http://no.wikipedia.org/wiki/Personnummer
|
|
|
|
"""
|
2007-12-17 16:05:27 +08:00
|
|
|
default_error_messages = {
|
2008-06-18 21:10:05 +08:00
|
|
|
'invalid': _(u'Enter a valid Norwegian social security number.'),
|
2007-12-17 16:05:27 +08:00
|
|
|
}
|
|
|
|
|
2007-03-31 17:14:37 +08:00
|
|
|
def clean(self, value):
|
|
|
|
super(NOSocialSecurityNumber, self).clean(value)
|
|
|
|
if value in EMPTY_VALUES:
|
|
|
|
return u''
|
|
|
|
|
|
|
|
if not re.match(r'^\d{11}$', value):
|
2007-12-17 16:05:27 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid'])
|
2007-03-31 17:14:37 +08:00
|
|
|
|
|
|
|
day = int(value[:2])
|
|
|
|
month = int(value[2:4])
|
|
|
|
year2 = int(value[4:6])
|
|
|
|
|
|
|
|
inum = int(value[6:9])
|
|
|
|
self.birthday = None
|
|
|
|
try:
|
|
|
|
if 000 <= inum < 500:
|
|
|
|
self.birthday = datetime.date(1900+year2, month, day)
|
2007-04-01 13:00:44 +08:00
|
|
|
if 500 <= inum < 750 and year2 > 54:
|
2007-03-31 17:14:37 +08:00
|
|
|
self.birthday = datetime.date(1800+year2, month, day)
|
2007-04-01 13:00:44 +08:00
|
|
|
if 500 <= inum < 1000 and year2 < 40:
|
2007-03-31 17:14:37 +08:00
|
|
|
self.birthday = datetime.date(2000+year2, month, day)
|
2007-04-01 13:00:44 +08:00
|
|
|
if 900 <= inum < 1000 and year2 > 39:
|
|
|
|
self.birthday = datetime.date(1900+year2, month, day)
|
2007-03-31 17:14:37 +08:00
|
|
|
except ValueError:
|
2007-12-17 16:05:27 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid'])
|
2007-03-31 17:14:37 +08:00
|
|
|
|
|
|
|
sexnum = int(value[8])
|
|
|
|
if sexnum % 2 == 0:
|
|
|
|
self.gender = 'F'
|
|
|
|
else:
|
|
|
|
self.gender = 'M'
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
2007-03-31 17:14:37 +08:00
|
|
|
digits = map(int, list(value))
|
|
|
|
weight_1 = [3, 7, 6, 1, 8, 9, 4, 5, 2, 1, 0]
|
|
|
|
weight_2 = [5, 4, 3, 2, 7, 6, 5, 4, 3, 2, 1]
|
|
|
|
|
|
|
|
def multiply_reduce(aval, bval):
|
2007-07-13 21:15:35 +08:00
|
|
|
return sum([(a * b) for (a, b) in zip(aval, bval)])
|
2007-03-31 17:14:37 +08:00
|
|
|
|
|
|
|
if multiply_reduce(digits, weight_1) % 11 != 0:
|
2007-12-17 16:05:27 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid'])
|
2007-03-31 17:14:37 +08:00
|
|
|
if multiply_reduce(digits, weight_2) % 11 != 0:
|
2007-12-17 16:05:27 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid'])
|
2007-03-31 17:14:37 +08:00
|
|
|
|
|
|
|
return value
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|