Ticket #7994: DutchBankAccountNumberField.diff
File DutchBankAccountNumberField.diff, 5.8 KB (added by , 16 years ago) |
---|
-
django/contrib/localflavor/nl/forms.py
11 11 12 12 pc_re = re.compile('^\d{4}[A-Z]{2}$') 13 13 sofi_re = re.compile('^\d{9}$') 14 bank_re = re.compile('^\d{9}$') 14 15 numeric_re = re.compile('^\d+$') 15 16 16 17 class NLZipCodeField(Field): … … 98 99 raise ValidationError(self.error_messages['invalid']) 99 100 100 101 return value 102 103 class NLBankAccountNumberField(Field): 104 """ 105 A Dutch back account number field (this excludes Postbank accounts, they 106 have no 11-check and these numbers will probably be converted on 107 01-01-2009). 108 109 People tend to use space or point seperators in Dutch bank account numbers 110 like this: 12.34.56.789. This field strips these seperators and leaves this 111 formatting to the "presentation layer". 112 113 http://nl.wikipedia.org/wiki/Bankrekeningnummer 114 """ 115 default_error_messages = { 116 'invalid': _('Enter a valid Dutch bank account number'), 117 } 118 119 def clean(self, value): 120 super(NLBankAccountNumberField, self).clean(value) 121 if value in EMPTY_VALUES: 122 return u'' 123 124 value = value.replace(' ', '').replace('.', '') 125 126 if not bank_re.search(value): 127 raise ValidationError(self.error_messages['invalid']) 128 129 if int(value) == 0: 130 raise ValidationError(self.error_messages['invalid']) 131 132 checksum = 0 133 for i in range(1, 10): 134 checksum += int(value[9-i]) * i 135 136 if checksum % 11 != 0: 137 raise ValidationError(self.error_messages['invalid']) 138 139 return value -
tests/regressiontests/forms/localflavor/nl.py
63 63 ... 64 64 ValidationError: [u'Enter a valid SoFi number'] 65 65 66 # NLBankAccountNumberField ######################################################### 67 68 >>> from django.contrib.localflavor.nl.forms import NLBankAccountNumberField 69 >>> f = NLBankAccountNumberField(required=False) 70 >>> f.clean('') 71 u'' 72 >>> f.clean('12.34.56.789') 73 '123456789' 74 >>> f.clean('12 34 56 789') 75 '123456789' 76 >>> f.clean('123456789') 77 '123456789' 78 >>> f.clean('000000000') 79 Traceback (most recent call last): 80 ... 81 ValidationError: [u'Enter a valid Dutch bank account number'] 82 >>> f.clean('123456788') 83 Traceback (most recent call last): 84 ... 85 ValidationError: [u'Enter a valid Dutch bank account number'] 86 >>> f.clean('foo') 87 Traceback (most recent call last): 88 ... 89 ValidationError: [u'Enter a valid Dutch bank account number'] 90 66 91 # NLProvinceSelect ########################################################## 67 92 68 93 >>> from django.contrib.localflavor.nl.forms import NLProvinceSelect -
django/contrib/localflavor/nl/forms.py
11 11 12 12 pc_re = re.compile('^\d{4}[A-Z]{2}$') 13 13 sofi_re = re.compile('^\d{9}$') 14 bank_re = re.compile('^\d{9}$') 14 15 numeric_re = re.compile('^\d+$') 15 16 16 17 class NLZipCodeField(Field): … … 98 99 raise ValidationError(self.error_messages['invalid']) 99 100 100 101 return value 102 103 class NLBankAccountNumberField(Field): 104 """ 105 A Dutch bank account number field (this excludes Postbank accounts, they 106 have no 11-check and use a different format). 107 108 People tend to use space or point seperators in Dutch bank account numbers 109 like this: 12.34.56.789. This field strips these seperators and leaves this 110 formatting to the "presentation layer". 111 112 http://nl.wikipedia.org/wiki/Bankrekeningnummer 113 """ 114 default_error_messages = { 115 'invalid': _('Enter a valid Dutch bank account number'), 116 } 117 118 def clean(self, value): 119 super(NLBankAccountNumberField, self).clean(value) 120 if value in EMPTY_VALUES: 121 return u'' 122 123 value = value.replace(' ', '').replace('.', '') 124 125 if not bank_re.search(value): 126 raise ValidationError(self.error_messages['invalid']) 127 128 if int(value) == 0: 129 raise ValidationError(self.error_messages['invalid']) 130 131 checksum = 0 132 for i in range(1, 10): 133 checksum += int(value[9-i]) * i 134 135 if checksum % 11 != 0: 136 raise ValidationError(self.error_messages['invalid']) 137 138 return value -
tests/regressiontests/forms/localflavor/nl.py
63 63 ... 64 64 ValidationError: [u'Enter a valid SoFi number'] 65 65 66 # NLBankAccountNumberField ######################################################### 67 68 >>> from django.contrib.localflavor.nl.forms import NLBankAccountNumberField 69 >>> f = NLBankAccountNumberField(required=False) 70 >>> f.clean('') 71 u'' 72 >>> f.clean('12.34.56.789') 73 '123456789' 74 >>> f.clean('12 34 56 789') 75 '123456789' 76 >>> f.clean('123456789') 77 '123456789' 78 >>> f.clean('000000000') 79 Traceback (most recent call last): 80 ... 81 ValidationError: [u'Enter a valid Dutch bank account number'] 82 >>> f.clean('123456788') 83 Traceback (most recent call last): 84 ... 85 ValidationError: [u'Enter a valid Dutch bank account number'] 86 >>> f.clean('foo') 87 Traceback (most recent call last): 88 ... 89 ValidationError: [u'Enter a valid Dutch bank account number'] 90 66 91 # NLProvinceSelect ########################################################## 67 92 68 93 >>> from django.contrib.localflavor.nl.forms import NLProvinceSelect