Ticket #5403: nl-localflavor.patch

File nl-localflavor.patch, 6.4 KB (added by Jan Rademaker <j.rademaker@…>, 17 years ago)
  • django/contrib/localflavor/nl/nl_provinces.py

     
     1from django.utils.translation import ugettext_lazy as _
     2
     3PROVINCE_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)
  • django/contrib/localflavor/nl/forms.py

     
     1"""
     2NL-specific Form helpers
     3"""
     4
     5import re
     6
     7from django.newforms import ValidationError
     8from django.newforms.fields import Field, Select, EMPTY_VALUES
     9from django.utils.translation import ugettext as _
     10from django.utils.encoding import smart_unicode
     11
     12pc_re = re.compile('^\d{4}[A-Z]{2}$')
     13sofi_re = re.compile('^\d{9}$')
     14numeric_re = re.compile('^\d+$')
     15
     16class 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
     35class 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
     44class 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
     65class 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
  • tests/regressiontests/forms/localflavor.py

     
    14461446Traceback (most recent call last):
    14471447...
    14481448ValidationError: [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('')
     1455u''
     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')
     1465Traceback (most recent call last):
     1466    ...
     1467ValidationError: [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('')
     1474u''
     1475>>> f.clean('1234ab')
     1476u'1234 AB'
     1477>>> f.clean('1234 ab')
     1478u'1234 AB'
     1479>>> f.clean('1234 AB')
     1480u'1234 AB'
     1481>>> f.clean('0123AB')
     1482Traceback (most recent call last):
     1483    ...
     1484ValidationError: [u'Enter a valid postal code']
     1485>>> f.clean('foo')
     1486Traceback (most recent call last):
     1487    ...
     1488ValidationError: [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('')
     1495u''
     1496>>> f.clean('123456782')
     1497'123456782'
     1498>>> f.clean('000000000')
     1499Traceback (most recent call last):
     1500    ...
     1501ValidationError: [u'Enter a valid SoFi number']
     1502>>> f.clean('123456789')
     1503Traceback (most recent call last):
     1504    ...
     1505ValidationError: [u'Enter a valid SoFi number']
     1506>>> f.clean('foo')
     1507Traceback (most recent call last):
     1508    ...
     1509ValidationError: [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')
     1516u'<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>'
    14491517"""
Back to Top