| 1 |
""" |
|---|
| 2 |
Iceland specific form helpers. |
|---|
| 3 |
""" |
|---|
| 4 |
|
|---|
| 5 |
from django.newforms import ValidationError |
|---|
| 6 |
from django.newforms.fields import RegexField, EMPTY_VALUES |
|---|
| 7 |
from django.newforms.widgets import Select |
|---|
| 8 |
from django.utils.translation import ugettext_lazy as _ |
|---|
| 9 |
from django.utils.encoding import smart_unicode |
|---|
| 10 |
|
|---|
| 11 |
class ISIdNumberField(RegexField): |
|---|
| 12 |
""" |
|---|
| 13 |
Icelandic identification number (kennitala). This is a number every citizen |
|---|
| 14 |
of Iceland has. |
|---|
| 15 |
""" |
|---|
| 16 |
default_error_messages = { |
|---|
| 17 |
'invalid': _('Enter a valid Icelandic identification number. The format is XXXXXX-XXXX.'), |
|---|
| 18 |
'checksum': _(u'The Icelandic identification number is not valid.'), |
|---|
| 19 |
} |
|---|
| 20 |
|
|---|
| 21 |
def __init__(self, *args, **kwargs): |
|---|
| 22 |
kwargs['min_length'],kwargs['max_length'] = 10,11 |
|---|
| 23 |
super(ISIdNumberField, self).__init__(r'^\d{6}(-| )?\d{4}$', *args, **kwargs) |
|---|
| 24 |
|
|---|
| 25 |
def clean(self, value): |
|---|
| 26 |
value = super(ISIdNumberField, self).clean(value) |
|---|
| 27 |
|
|---|
| 28 |
if value in EMPTY_VALUES: |
|---|
| 29 |
return u'' |
|---|
| 30 |
|
|---|
| 31 |
value = self._canonify(value) |
|---|
| 32 |
if self._validate(value): |
|---|
| 33 |
return self._format(value) |
|---|
| 34 |
else: |
|---|
| 35 |
raise ValidationError(self.error_messages['checksum']) |
|---|
| 36 |
|
|---|
| 37 |
def _canonify(self, value): |
|---|
| 38 |
""" |
|---|
| 39 |
Returns the value as only digits. |
|---|
| 40 |
""" |
|---|
| 41 |
return value.replace('-', '').replace(' ', '') |
|---|
| 42 |
|
|---|
| 43 |
def _validate(self, value): |
|---|
| 44 |
""" |
|---|
| 45 |
Takes in the value in canonical form and checks the verifier digit. The |
|---|
| 46 |
method is modulo 11. |
|---|
| 47 |
""" |
|---|
| 48 |
check = [3, 2, 7, 6, 5, 4, 3, 2, 1, 0] |
|---|
| 49 |
return sum([int(value[i]) * check[i] for i in range(10)]) % 11 == 0 |
|---|
| 50 |
|
|---|
| 51 |
def _format(self, value): |
|---|
| 52 |
""" |
|---|
| 53 |
Takes in the value in canonical form and returns it in the common |
|---|
| 54 |
display format. |
|---|
| 55 |
""" |
|---|
| 56 |
return smart_unicode(value[:6]+'-'+value[6:]) |
|---|
| 57 |
|
|---|
| 58 |
class ISPhoneNumberField(RegexField): |
|---|
| 59 |
""" |
|---|
| 60 |
Icelandic phone number. Seven digits with an optional hyphen or space after |
|---|
| 61 |
the first three digits. |
|---|
| 62 |
""" |
|---|
| 63 |
def __init__(self, *args, **kwargs): |
|---|
| 64 |
kwargs['min_length'], kwargs['max_length'] = 7,8 |
|---|
| 65 |
super(ISPhoneNumberField, self).__init__(r'^\d{3}(-| )?\d{4}$', *args, **kwargs) |
|---|
| 66 |
|
|---|
| 67 |
def clean(self, value): |
|---|
| 68 |
value = super(ISPhoneNumberField, self).clean(value) |
|---|
| 69 |
|
|---|
| 70 |
if value in EMPTY_VALUES: |
|---|
| 71 |
return u'' |
|---|
| 72 |
|
|---|
| 73 |
return value.replace('-', '').replace(' ', '') |
|---|
| 74 |
|
|---|
| 75 |
class ISPostalCodeSelect(Select): |
|---|
| 76 |
""" |
|---|
| 77 |
A Select widget that uses a list of Icelandic postal codes as its choices. |
|---|
| 78 |
""" |
|---|
| 79 |
def __init__(self, attrs=None): |
|---|
| 80 |
from is_postalcodes import IS_POSTALCODES |
|---|
| 81 |
super(ISPostalCodeSelect, self).__init__(attrs, choices=IS_POSTALCODES) |
|---|