LOANWOLF_BANK_ACCOUNTS_LENGTH = {
'001': 7, # Banque de Montréal
'002': 7, # Scotia
'003': 7, # RBC
'004': 7 or 11, # TD (7 or 11)
'006': 7, # BNC
'010': 7, # CIBC
'016': 9, # HSBC
'039': 9, # Banque Laurentienne
'614': 10, # Tangerine
'815': 7, # Desjardins
'829': 7, # Desjardins Ontario
}
和
def clean_bank_account(self):
bank_account = self.form.cleaned_data.get('bank_account')
bank_transit = self.form.cleaned_data.get('bank_transit')
if bank_account not in (None, ''):
bank = self.form.cleaned_data.get('bank')
if bank not in (None, ''):
# Check bank account format for specific banks
length = settings.LOANWOLF_BANK_ACCOUNTS_LENGTH.get(bank)
if length:
if bank_transit not in (None, ''):
if not bank_account.isnumeric() or length != len(bank_account):
if bank == '004':
raise ValidationError(
_('Bank account number must contain 7 or 11 digits') % length # noqa
)
else:
raise ValidationError(
_('Bank account number must contain %d digits') % length # noqa
)
else:
raise ValidationError(
_('Cannot validate bank account without a valid bank transit') # noqa
)
return bank_account
我想给004
提供多个参数,定义,以便length
不同于7或11,那么我想显示Bank account number must contain 7 or 11 digits
。我怎样才能将多个参数传递给关键字?
答案 0 :(得分:0)
我建议您的词典存储<bankId : set>
的映射,其中每个集合包含可能的长度:
LOANWOLF_BANK_ACCOUNTS_LENGTH = {
'001': {7}, # Banque de Montréal
'002': {7}, # Scotia
'003': {7}, # RBC
'004': {7, 11}, # TD (7 or 11)
'006': {7}, # BNC
'010': {7}, # CIBC
'016': {9}, # HSBC
'039': {9}, # Banque Laurentienne
'614': {10}, # Tangerine
'815': {7}, # Desjardins
'829': {7}, # Desjardins Ontario
}
您的代码现在需要测试集合成员资格,如下所示:
( len(bank_account) not in length )
您还可以使用str.format
简化错误处理。
...
if length:
if bank_transit not in (None, ''):
if not bank_account.isnumeric() or (len(bank_account) not in length): # this is the important part
raise ValidationError(
_('Bank account number must contain {} digits').format(' or '.join(map(str, length))) # noqa
)
else:
raise ValidationError(
_('Cannot validate bank account without a valid bank transit') # noqa
)
...