~ubuntu-branches/debian/sid/python-django/sid

« back to all changes in this revision

Viewing changes to django/contrib/localflavor/nl/forms.py

  • Committer: Package Import Robot
  • Author(s): Luke Faraone
  • Date: 2013-11-07 15:33:49 UTC
  • mfrom: (1.3.12)
  • Revision ID: package-import@ubuntu.com-20131107153349-e31sc149l2szs3jb
Tags: 1.6-1
* New upstream version. Closes: #557474, #724637.
* python-django now also suggests the installation of ipython,
  bpython, python-django-doc, and libgdal1.
  Closes: #636511, #686333, #704203
* Set package maintainer to Debian Python Modules Team.
* Bump standards version to 3.9.5, no changes needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""
2
 
NL-specific Form helpers
3
 
"""
4
 
 
5
 
from __future__ import absolute_import, unicode_literals
6
 
 
7
 
import re
8
 
 
9
 
from django.contrib.localflavor.nl.nl_provinces import PROVINCE_CHOICES
10
 
from django.core.validators import EMPTY_VALUES
11
 
from django.forms import ValidationError
12
 
from django.forms.fields import Field, Select
13
 
from django.utils.encoding import smart_text
14
 
from django.utils.translation import ugettext_lazy as _
15
 
 
16
 
 
17
 
pc_re = re.compile('^\d{4}[A-Z]{2}$')
18
 
sofi_re = re.compile('^\d{9}$')
19
 
numeric_re = re.compile('^\d+$')
20
 
 
21
 
class NLZipCodeField(Field):
22
 
    """
23
 
    A Dutch postal code field.
24
 
    """
25
 
    default_error_messages = {
26
 
        'invalid': _('Enter a valid postal code'),
27
 
    }
28
 
 
29
 
    def clean(self, value):
30
 
        super(NLZipCodeField, self).clean(value)
31
 
        if value in EMPTY_VALUES:
32
 
            return ''
33
 
 
34
 
        value = value.strip().upper().replace(' ', '')
35
 
        if not pc_re.search(value):
36
 
            raise ValidationError(self.error_messages['invalid'])
37
 
 
38
 
        if int(value[:4]) < 1000:
39
 
            raise ValidationError(self.error_messages['invalid'])
40
 
 
41
 
        return '%s %s' % (value[:4], value[4:])
42
 
 
43
 
class NLProvinceSelect(Select):
44
 
    """
45
 
    A Select widget that uses a list of provinces of the Netherlands as its
46
 
    choices.
47
 
    """
48
 
    def __init__(self, attrs=None):
49
 
        super(NLProvinceSelect, self).__init__(attrs, choices=PROVINCE_CHOICES)
50
 
 
51
 
class NLPhoneNumberField(Field):
52
 
    """
53
 
    A Dutch telephone number field.
54
 
    """
55
 
    default_error_messages = {
56
 
        'invalid': _('Enter a valid phone number'),
57
 
    }
58
 
 
59
 
    def clean(self, value):
60
 
        super(NLPhoneNumberField, self).clean(value)
61
 
        if value in EMPTY_VALUES:
62
 
            return ''
63
 
 
64
 
        phone_nr = re.sub('[\-\s\(\)]', '', smart_text(value))
65
 
 
66
 
        if len(phone_nr) == 10 and numeric_re.search(phone_nr):
67
 
            return value
68
 
 
69
 
        if phone_nr[:3] == '+31' and len(phone_nr) == 12 and \
70
 
           numeric_re.search(phone_nr[3:]):
71
 
            return value
72
 
 
73
 
        raise ValidationError(self.error_messages['invalid'])
74
 
 
75
 
class NLSoFiNumberField(Field):
76
 
    """
77
 
    A Dutch social security number (SoFi/BSN) field.
78
 
 
79
 
    http://nl.wikipedia.org/wiki/Sofinummer
80
 
    """
81
 
    default_error_messages = {
82
 
        'invalid': _('Enter a valid SoFi number'),
83
 
    }
84
 
 
85
 
    def clean(self, value):
86
 
        super(NLSoFiNumberField, self).clean(value)
87
 
        if value in EMPTY_VALUES:
88
 
            return ''
89
 
 
90
 
        if not sofi_re.search(value):
91
 
            raise ValidationError(self.error_messages['invalid'])
92
 
 
93
 
        if int(value) == 0:
94
 
            raise ValidationError(self.error_messages['invalid'])
95
 
 
96
 
        checksum = 0
97
 
        for i in range(9, 1, -1):
98
 
            checksum += int(value[9-i]) * i
99
 
        checksum -= int(value[-1])
100
 
 
101
 
        if checksum % 11 != 0:
102
 
            raise ValidationError(self.error_messages['invalid'])
103
 
 
104
 
        return value