Ticket #5403: nl-localflavor.patch
File nl-localflavor.patch, 6.4 KB (added by , 17 years ago) |
---|
-
django/contrib/localflavor/nl/nl_provinces.py
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 ) -
django/contrib/localflavor/nl/forms.py
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 -
tests/regressiontests/forms/localflavor.py
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 """