Ticket #3961: ticket_3961__rev_6223.diff
File ticket_3961__rev_6223.diff, 4.6 KB (added by , 17 years ago) |
---|
-
django/contrib/localflavor/za/za_provinces.py
1 from django.utils.translation import gettext_lazy as _ 2 3 PROVINCE_CHOICES = ( 4 ('EC', _('Eastern Cape')), 5 ('FS', _('Free State')), 6 ('GP', _('Gauteng')), 7 ('KN', _('KwaZulu-Natal')), 8 ('LP', _('Limpopo')), 9 ('MP', _('Mpumalanga')), 10 ('NC', _('Northern Cape')), 11 ('NW', _('North West')), 12 ('WC', _('Western Cape')), 13 ) -
django/contrib/localflavor/za/forms.py
1 """ 2 South Africa-specific Form helpers 3 """ 4 5 from django.newforms import ValidationError 6 from django.newforms.fields import Field, RegexField, EMPTY_VALUES 7 from django.utils.checksums import luhn 8 from django.utils.translation import gettext as _ 9 import re 10 from datetime import date 11 12 id_re = re.compile(r'^(?P<yy>\d\d)(?P<mm>\d\d)(?P<dd>\d\d)(?P<mid>\d{4})(?P<end>\d{3})') 13 14 class ZAIDField(Field): 15 """A form field for South African ID numbers -- the checksum is validated 16 using the Luhn checksum, and uses a simlistic (read: not entirely accurate) 17 check for the birthdate 18 """ 19 20 def __init__(self, *args, **kwargs): 21 super(ZAIDField, self).__init__() 22 self.error_message = _(u'Enter a valid South African ID number') 23 24 def clean(self, value): 25 # strip spaces and dashes 26 value = value.strip().replace(' ', '').replace('-', '') 27 28 super(ZAIDField, self).clean(value) 29 30 if value in EMPTY_VALUES: 31 return u'' 32 33 match = re.match(id_re, value) 34 35 if not match: 36 raise ValidationError(self.error_message) 37 38 g = match.groupdict() 39 40 try: 41 # The year 2000 is conveniently a leapyear. 42 # This algorithm will break in xx00 years which aren't leap years 43 # There is no way to guess the century of a ZA ID number 44 d = date(int(g['yy']) + 2000, int(g['mm']), int(g['dd'])) 45 except ValueError: 46 raise ValidationError(self.error_message) 47 48 if not luhn(value): 49 raise ValidationError(self.error_message) 50 51 return value 52 53 class ZAPostCodeField(RegexField): 54 def __init__(self, *args, **kwargs): 55 super(ZAPostCodeField, self).__init__(r'^\d{4}$', 56 max_length=None, min_length=None, 57 error_message=_(u'Enter a valid South African postal code')) -
tests/regressiontests/forms/localflavor.py
2036 2036 ... 2037 2037 ValidationError: [u'Enter a valid Canadian Social Insurance number in XXX-XXX-XXXX format.'] 2038 2038 2039 # ZAIDField ################################################################# 2040 2041 ZAIDField validates that the date is a valid birthdate and that the value 2042 has a valid checksum. It allows spaces and dashes, and returns a plain 2043 string of digits. 2044 >>> from django.contrib.localflavor.za.forms import ZAIDField 2045 >>> f = ZAIDField() 2046 >>> f.clean('0002290001003') 2047 '0002290001003' 2048 >>> f.clean('000229 0001 003') 2049 '0002290001003' 2050 >>> f.clean('0102290001001') 2051 Traceback (most recent call last): 2052 ... 2053 ValidationError: [u'Enter a valid South African ID number'] 2054 >>> f.clean('811208') 2055 Traceback (most recent call last): 2056 ... 2057 ValidationError: [u'Enter a valid South African ID number'] 2058 >>> f.clean('0002290001004') 2059 Traceback (most recent call last): 2060 ... 2061 ValidationError: [u'Enter a valid South African ID number'] 2062 2063 # ZAPostCodeField ########################################################### 2064 >>> from django.contrib.localflavor.za.forms import ZAPostCodeField 2065 >>> f = ZAPostCodeField() 2066 >>> f.clean('abcd') 2067 Traceback (most recent call last): 2068 ... 2069 ValidationError: [u'Enter a valid South African postal code'] 2070 >>> f.clean('0000') 2071 u'0000' 2072 >>> f.clean(' 7530') 2073 Traceback (most recent call last): 2074 ... 2075 ValidationError: [u'Enter a valid South African postal code'] 2076 2039 2077 ## Generic DateField ########################################################## 2040 2078 2041 2079 >>> from django.contrib.localflavor.generic.forms import *