2007-04-26 16:43:25 +08:00
|
|
|
"""
|
|
|
|
Iceland specific form helpers.
|
|
|
|
"""
|
2007-04-26 22:55:18 +08:00
|
|
|
|
2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import absolute_import, unicode_literals
|
2011-10-18 08:47:49 +08:00
|
|
|
|
|
|
|
from django.contrib.localflavor.is_.is_postalcodes import IS_POSTALCODES
|
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 RegexField
|
2008-07-19 09:22:26 +08:00
|
|
|
from django.forms.widgets import Select
|
2012-07-21 16:00:10 +08:00
|
|
|
from django.utils.encoding import smart_text
|
2011-10-18 08:47:49 +08:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
2007-04-26 16:43:25 +08:00
|
|
|
|
|
|
|
class ISIdNumberField(RegexField):
|
|
|
|
"""
|
|
|
|
Icelandic identification number (kennitala). This is a number every citizen
|
|
|
|
of Iceland has.
|
|
|
|
"""
|
2007-12-17 16:05:27 +08:00
|
|
|
default_error_messages = {
|
2008-06-18 21:10:05 +08:00
|
|
|
'invalid': _('Enter a valid Icelandic identification number. The format is XXXXXX-XXXX.'),
|
2012-06-08 00:08:47 +08:00
|
|
|
'checksum': _('The Icelandic identification number is not valid.'),
|
2007-12-17 16:05:27 +08:00
|
|
|
}
|
|
|
|
|
2011-05-03 19:52:04 +08:00
|
|
|
def __init__(self, max_length=11, min_length=10, *args, **kwargs):
|
|
|
|
super(ISIdNumberField, self).__init__(r'^\d{6}(-| )?\d{4}$',
|
|
|
|
max_length, min_length, *args, **kwargs)
|
2007-04-26 16:43:25 +08:00
|
|
|
|
|
|
|
def clean(self, value):
|
|
|
|
value = super(ISIdNumberField, self).clean(value)
|
|
|
|
|
|
|
|
if value in EMPTY_VALUES:
|
2012-06-08 00:08:47 +08:00
|
|
|
return ''
|
2007-04-26 16:43:25 +08:00
|
|
|
|
|
|
|
value = self._canonify(value)
|
|
|
|
if self._validate(value):
|
|
|
|
return self._format(value)
|
|
|
|
else:
|
2007-12-17 16:05:27 +08:00
|
|
|
raise ValidationError(self.error_messages['checksum'])
|
2007-04-26 16:43:25 +08:00
|
|
|
|
|
|
|
def _canonify(self, value):
|
|
|
|
"""
|
|
|
|
Returns the value as only digits.
|
|
|
|
"""
|
|
|
|
return value.replace('-', '').replace(' ', '')
|
|
|
|
|
|
|
|
def _validate(self, value):
|
|
|
|
"""
|
|
|
|
Takes in the value in canonical form and checks the verifier digit. The
|
|
|
|
method is modulo 11.
|
|
|
|
"""
|
|
|
|
check = [3, 2, 7, 6, 5, 4, 3, 2, 1, 0]
|
2007-05-13 00:05:51 +08:00
|
|
|
return sum([int(value[i]) * check[i] for i in range(10)]) % 11 == 0
|
2007-04-26 16:43:25 +08:00
|
|
|
|
|
|
|
def _format(self, value):
|
|
|
|
"""
|
|
|
|
Takes in the value in canonical form and returns it in the common
|
|
|
|
display format.
|
|
|
|
"""
|
2012-07-21 16:00:10 +08:00
|
|
|
return smart_text(value[:6]+'-'+value[6:])
|
2007-04-26 16:43:25 +08:00
|
|
|
|
|
|
|
class ISPhoneNumberField(RegexField):
|
|
|
|
"""
|
|
|
|
Icelandic phone number. Seven digits with an optional hyphen or space after
|
|
|
|
the first three digits.
|
|
|
|
"""
|
2011-05-03 19:52:04 +08:00
|
|
|
def __init__(self, max_length=8, min_length=7, *args, **kwargs):
|
|
|
|
super(ISPhoneNumberField, self).__init__(r'^\d{3}(-| )?\d{4}$',
|
|
|
|
max_length, min_length, *args, **kwargs)
|
2007-04-26 16:43:25 +08:00
|
|
|
|
|
|
|
def clean(self, value):
|
|
|
|
value = super(ISPhoneNumberField, self).clean(value)
|
|
|
|
|
|
|
|
if value in EMPTY_VALUES:
|
2012-06-08 00:08:47 +08:00
|
|
|
return ''
|
2007-04-26 16:43:25 +08:00
|
|
|
|
|
|
|
return value.replace('-', '').replace(' ', '')
|
|
|
|
|
|
|
|
class ISPostalCodeSelect(Select):
|
|
|
|
"""
|
|
|
|
A Select widget that uses a list of Icelandic postal codes as its choices.
|
|
|
|
"""
|
|
|
|
def __init__(self, attrs=None):
|
|
|
|
super(ISPostalCodeSelect, self).__init__(attrs, choices=IS_POSTALCODES)
|
|
|
|
|