Validate and format BIC
issue8422 review255561002
This commit is contained in:
1 parent
3b24bb7060
commit
a7b3f42449
5 files changed
+53
-2
No files matched your search
@@ -1,3 +1,5 @@
|
||||
* Validate and format BIC
|
||||
|
||||
Version 5.2.0 - 2019-05-06
|
||||
* Bug fixes (see mercurial logs for details)
|
||||
* Remove starting wildcard when searching on codes and numbers
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
# This file is part of Tryton. The COPYRIGHT file at the top level of
|
||||
# this repository contains the full copyright notices and license terms.
|
||||
from stdnum import iban
|
||||
from stdnum import iban, bic
|
||||
import stdnum.exceptions
|
||||
from sql import operators, Literal
|
||||
|
||||
from trytond.i18n import gettext
|
||||
from trytond.model import (
|
||||
ModelView, ModelSQL, DeactivableMixin, fields, sequence_ordered)
|
||||
|
||||
from .exceptions import IBANValidationError
|
||||
from .exceptions import IBANValidationError, InvalidBIC
|
||||
|
||||
|
||||
__all__ = ['Bank', 'BankAccount', 'BankAccountNumber', 'BankAccountParty']
|
||||
@@ -27,6 +28,23 @@ class Bank(ModelSQL, ModelView):
|
||||
def search_rec_name(cls, name, clause):
|
||||
return [('party',) + tuple(clause[1:])]
|
||||
|
||||
@fields.depends('bic')
|
||||
def on_change_with_bic(self):
|
||||
try:
|
||||
return bic.compact(self.bic)
|
||||
except stdnum.exceptions.ValidationError:
|
||||
pass
|
||||
return self.bic
|
||||
|
||||
def pre_validate(self):
|
||||
super().pre_validate()
|
||||
self.check_bic()
|
||||
|
||||
@fields.depends('bic')
|
||||
def check_bic(self):
|
||||
if self.bic and not bic.is_valid(self.bic):
|
||||
raise InvalidBIC(gettext('bank.msg_invalid_bic', bic=self.bic))
|
||||
|
||||
|
||||
class BankAccount(DeactivableMixin, ModelSQL, ModelView):
|
||||
'Bank Account'
|
||||
|
||||
@@ -6,3 +6,7 @@ from trytond.model.exceptions import ValidationError
|
||||
|
||||
class IBANValidationError(ValidationError):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidBIC(ValidationError):
|
||||
pass
|
||||
@@ -6,5 +6,8 @@ this repository contains the full copyright notices and license terms. -->
|
||||
<record model="ir.message" id="msg_invalid_iban">
|
||||
<field name="text">IBAN number "%(number)s" is not valid.</field>
|
||||
</record>
|
||||
<record model="ir.message" id="msg_invalid_bic">
|
||||
<field name="text">The BIC "%(bic)s" is not valid.</field>
|
||||
</record>
|
||||
</data>
|
||||
</tryton>
|
||||
@@ -5,11 +5,35 @@ import trytond.tests.test_tryton
|
||||
from trytond.tests.test_tryton import ModuleTestCase, with_transaction
|
||||
from trytond.pool import Pool
|
||||
|
||||
from ..exceptions import InvalidBIC
|
||||
|
||||
|
||||
class BankTestCase(ModuleTestCase):
|
||||
'Test Bank module'
|
||||
module = 'bank'
|
||||
|
||||
@with_transaction()
|
||||
def test_bic_validation(self):
|
||||
"Test BIC validation"
|
||||
pool = Pool()
|
||||
Party = pool.get('party.party')
|
||||
Bank = pool.get('bank')
|
||||
|
||||
party = Party(name='Test')
|
||||
party.save()
|
||||
bank = Bank(party=party)
|
||||
bank.save()
|
||||
|
||||
bank.bic = 'ABNA BE 2A'
|
||||
bank.bic = bank.on_change_with_bic()
|
||||
self.assertEqual(bank.bic, 'ABNABE2A')
|
||||
|
||||
bank.save()
|
||||
|
||||
with self.assertRaises(InvalidBIC):
|
||||
bank.bic = 'foo'
|
||||
bank.save()
|
||||
|
||||
@with_transaction()
|
||||
def test_iban_format(self):
|
||||
'Test IBAN format'
|
||||
|
||||
Reference in new issue
Block a user