Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #5403 -- Added Dutch localflavor. Thanks, Jan Rademaker.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@6139 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit a9fd677cb3167afbe0b27bf1a8fe5a16eaad5908 1 parent da7495c
Malcolm Tredinnick authored September 13, 2007
2  AUTHORS
@@ -237,7 +237,7 @@ answer newbie questions, and generally made Django that much better:
237 237
     polpak@yahoo.com
238 238
     Jyrki Pulliainen <jyrki.pulliainen@gmail.com>
239 239
     Johann Queuniet <johann.queuniet@adh.naellia.eu>
240  
-    J. Rademaker
  240
+    Jan Rademaker
241 241
     Michael Radziej <mir@noris.de>
242 242
     Amit Ramon <amit.ramon@gmail.com>
243 243
     Massimiliano Ravelli <massimiliano.ravelli@gmail.com>
92  django/contrib/localflavor/nl/forms.py
... ...
@@ -0,0 +1,92 @@
  1
+"""
  2
+NL-specific Form helpers
  3
+"""
  4
+
  5
+import re
  6
+
  7
+from django.newforms import ValidationError
  8
+from django.newforms.fields import Field, Select, EMPTY_VALUES
  9
+from django.utils.translation import ugettext as _
  10
+from django.utils.encoding import smart_unicode
  11
+
  12
+pc_re = re.compile('^\d{4}[A-Z]{2}$')
  13
+sofi_re = re.compile('^\d{9}$')
  14
+numeric_re = re.compile('^\d+$')
  15
+
  16
+class NLZipCodeField(Field):
  17
+    """
  18
+    A Dutch postal code field.
  19
+    """
  20
+    def clean(self, value):
  21
+        super(NLZipCodeField, self).clean(value)
  22
+        if value in EMPTY_VALUES:
  23
+            return u''
  24
+        
  25
+        msg = _('Enter a valid postal code')
  26
+        value = value.strip().upper().replace(' ', '')
  27
+        if not pc_re.search(value):
  28
+            raise ValidationError(msg)
  29
+        
  30
+        if int(value[:4]) < 1000:
  31
+            raise ValidationError(msg)
  32
+        
  33
+        return u'%s %s' % (value[:4], value[4:])
  34
+
  35
+class NLProvinceSelect(Select):
  36
+    """
  37
+    A Select widget that uses a list of provinces of the Netherlands as its 
  38
+    choices.
  39
+    """
  40
+    def __init__(self, attrs=None):
  41
+        from nl_provinces import PROVINCE_CHOICES
  42
+        super(NLProvinceSelect, self).__init__(attrs, choices=PROVINCE_CHOICES)
  43
+
  44
+class NLPhoneNumberField(Field):
  45
+    """
  46
+    A Dutch telephone number field.
  47
+    """
  48
+    def clean(self, value):
  49
+        super(NLPhoneNumberField, self).clean(value)
  50
+        if value in EMPTY_VALUES:
  51
+            return u''
  52
+        
  53
+        msg = _('Enter a valid phone number')
  54
+        phone_nr = re.sub('[\-\s\(\)]', '', smart_unicode(value))
  55
+        
  56
+        if len(phone_nr) == 10 and numeric_re.search(phone_nr):
  57
+            return value
  58
+        
  59
+        if phone_nr[:3] == '+31' and len(phone_nr) == 12 and \
  60
+           numeric_re.search(phone_nr[3:]):
  61
+            return value
  62
+        
  63
+        raise ValidationError(msg)
  64
+
  65
+class NLSoFiNumberField(Field):
  66
+    """
  67
+    A Dutch social security number (SoFi/BSN) field.
  68
+ 
  69
+    http://nl.wikipedia.org/wiki/Sofinummer
  70
+    """
  71
+    def clean(self, value):
  72
+        super(NLSoFiNumberField, self).clean(value)
  73
+        if value in EMPTY_VALUES:
  74
+            return u''
  75
+        
  76
+        msg = _('Enter a valid SoFi number')
  77
+        
  78
+        if not sofi_re.search(value):
  79
+            raise ValidationError(msg)
  80
+        
  81
+        if int(value) == 0:
  82
+            raise ValidationError(msg)
  83
+        
  84
+        checksum = 0
  85
+        for i in range(9, 1, -1):
  86
+            checksum += int(value[9-i]) * i
  87
+        checksum -= int(value[-1])
  88
+        
  89
+        if checksum % 11 != 0:
  90
+            raise ValidationError(msg)
  91
+        
  92
+        return value
16  django/contrib/localflavor/nl/nl_provinces.py
... ...
@@ -0,0 +1,16 @@
  1
+from django.utils.translation import ugettext_lazy as _
  2
+
  3
+PROVINCE_CHOICES = (
  4
+    ('DR', _('Drente')),
  5
+    ('FL', _('Flevoland')),
  6
+    ('FR', _('Friesland')),
  7
+    ('GL', _('Gelderland')),
  8
+    ('GR', _('Groningen')),
  9
+    ('LB', _('Limburg')),
  10
+    ('NB', _('Noord-Brabant')),
  11
+    ('NH', _('Noord-Holland')),
  12
+    ('OV', _('Overijssel')),
  13
+    ('UT', _('Utrecht')),
  14
+    ('ZE', _('Zeeland')),
  15
+    ('ZH', _('Zuid-Holland')),
  16
+)
0  localflavor/nl/__init__.py b/django/contrib/localflavor/nl/__init__.py
No changes.
68  tests/regressiontests/forms/localflavor.py
@@ -1446,4 +1446,72 @@
1446 1446
 Traceback (most recent call last):
1447 1447
 ...
1448 1448
 ValidationError: [u'National Identification Number consists of 11 digits.']
  1449
+
  1450
+# NLPhoneNumberField ########################################################
  1451
+
  1452
+>>> from django.contrib.localflavor.nl.forms import NLPhoneNumberField
  1453
+>>> f = NLPhoneNumberField(required=False)
  1454
+>>> f.clean('')
  1455
+u''
  1456
+>>> f.clean('012-3456789')
  1457
+'012-3456789'
  1458
+>>> f.clean('0123456789')
  1459
+'0123456789'
  1460
+>>> f.clean('+31-12-3456789')
  1461
+'+31-12-3456789'
  1462
+>>> f.clean('(0123) 456789')
  1463
+'(0123) 456789'
  1464
+>>> f.clean('foo')
  1465
+Traceback (most recent call last):
  1466
+    ...
  1467
+ValidationError: [u'Enter a valid phone number']
  1468
+
  1469
+# NLZipCodeField ############################################################
  1470
+
  1471
+>>> from django.contrib.localflavor.nl.forms import NLZipCodeField
  1472
+>>> f = NLZipCodeField(required=False)
  1473
+>>> f.clean('')
  1474
+u''
  1475
+>>> f.clean('1234ab')
  1476
+u'1234 AB'
  1477
+>>> f.clean('1234 ab')
  1478
+u'1234 AB'
  1479
+>>> f.clean('1234 AB')
  1480
+u'1234 AB'
  1481
+>>> f.clean('0123AB')
  1482
+Traceback (most recent call last):
  1483
+    ...
  1484
+ValidationError: [u'Enter a valid postal code']
  1485
+>>> f.clean('foo')
  1486
+Traceback (most recent call last):
  1487
+    ...
  1488
+ValidationError: [u'Enter a valid postal code']
  1489
+
  1490
+# NLSoFiNumberField #########################################################
  1491
+
  1492
+>>> from django.contrib.localflavor.nl.forms import NLSoFiNumberField
  1493
+>>> f = NLSoFiNumberField(required=False)
  1494
+>>> f.clean('')
  1495
+u''
  1496
+>>> f.clean('123456782')
  1497
+'123456782'
  1498
+>>> f.clean('000000000')
  1499
+Traceback (most recent call last):
  1500
+    ...
  1501
+ValidationError: [u'Enter a valid SoFi number']
  1502
+>>> f.clean('123456789')
  1503
+Traceback (most recent call last):
  1504
+    ...
  1505
+ValidationError: [u'Enter a valid SoFi number']
  1506
+>>> f.clean('foo')
  1507
+Traceback (most recent call last):
  1508
+    ...
  1509
+ValidationError: [u'Enter a valid SoFi number']
  1510
+
  1511
+# NLProvinceSelect ##########################################################
  1512
+
  1513
+>>> from django.contrib.localflavor.nl.forms import NLProvinceSelect
  1514
+>>> s = NLProvinceSelect()
  1515
+>>> s.render('provinces', 'OV')
  1516
+u'<select name="provinces">\n<option value="DR">Drente</option>\n<option value="FL">Flevoland</option>\n<option value="FR">Friesland</option>\n<option value="GL">Gelderland</option>\n<option value="GR">Groningen</option>\n<option value="LB">Limburg</option>\n<option value="NB">Noord-Brabant</option>\n<option value="NH">Noord-Holland</option>\n<option value="OV" selected="selected">Overijssel</option>\n<option value="UT">Utrecht</option>\n<option value="ZE">Zeeland</option>\n<option value="ZH">Zuid-Holland</option>\n</select>'
1449 1517
 """

0 notes on commit a9fd677

Please sign in to comment.
Something went wrong with that request. Please try again.