Ticket #7994: DutchBankAccountNumberField.diff

File DutchBankAccountNumberField.diff, 5.8 KB (added by Rudolph Froger, 16 years ago)

Removed a typo and altered the comments.

  • django/contrib/localflavor/nl/forms.py

     
    1111
    1212pc_re = re.compile('^\d{4}[A-Z]{2}$')
    1313sofi_re = re.compile('^\d{9}$')
     14bank_re = re.compile('^\d{9}$')
    1415numeric_re = re.compile('^\d+$')
    1516
    1617class NLZipCodeField(Field):
     
    9899            raise ValidationError(self.error_messages['invalid'])
    99100
    100101        return value
     102
     103class 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

     
    6363    ...
    6464ValidationError: [u'Enter a valid SoFi number']
    6565
     66# NLBankAccountNumberField #########################################################
     67
     68>>> from django.contrib.localflavor.nl.forms import NLBankAccountNumberField
     69>>> f = NLBankAccountNumberField(required=False)
     70>>> f.clean('')
     71u''
     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')
     79Traceback (most recent call last):
     80    ...
     81ValidationError: [u'Enter a valid Dutch bank account number']
     82>>> f.clean('123456788')
     83Traceback (most recent call last):
     84    ...
     85ValidationError: [u'Enter a valid Dutch bank account number']
     86>>> f.clean('foo')
     87Traceback (most recent call last):
     88    ...
     89ValidationError: [u'Enter a valid Dutch bank account number']
     90
    6691# NLProvinceSelect ##########################################################
    6792
    6893>>> from django.contrib.localflavor.nl.forms import NLProvinceSelect
  • django/contrib/localflavor/nl/forms.py

     
    1111
    1212pc_re = re.compile('^\d{4}[A-Z]{2}$')
    1313sofi_re = re.compile('^\d{9}$')
     14bank_re = re.compile('^\d{9}$')
    1415numeric_re = re.compile('^\d+$')
    1516
    1617class NLZipCodeField(Field):
     
    9899            raise ValidationError(self.error_messages['invalid'])
    99100
    100101        return value
     102
     103class 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

     
    6363    ...
    6464ValidationError: [u'Enter a valid SoFi number']
    6565
     66# NLBankAccountNumberField #########################################################
     67
     68>>> from django.contrib.localflavor.nl.forms import NLBankAccountNumberField
     69>>> f = NLBankAccountNumberField(required=False)
     70>>> f.clean('')
     71u''
     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')
     79Traceback (most recent call last):
     80    ...
     81ValidationError: [u'Enter a valid Dutch bank account number']
     82>>> f.clean('123456788')
     83Traceback (most recent call last):
     84    ...
     85ValidationError: [u'Enter a valid Dutch bank account number']
     86>>> f.clean('foo')
     87Traceback (most recent call last):
     88    ...
     89ValidationError: [u'Enter a valid Dutch bank account number']
     90
    6691# NLProvinceSelect ##########################################################
    6792
    6893>>> from django.contrib.localflavor.nl.forms import NLProvinceSelect
Back to Top