From a7b3f42449f3b1dde9d5aefcc1d3259d4093e467 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Wed, 26 Jun 2019 22:10:31 +0200 Subject: [PATCH] Validate and format BIC issue8422 review255561002 --- CHANGELOG | 2 ++ bank.py | 22 ++++++++++++++++++++-- exceptions.py | 4 ++++ message.xml | 3 +++ tests/test_bank.py | 24 ++++++++++++++++++++++++ 5 files changed, 53 insertions(+), 2 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 73b38b7575..cb1968fe85 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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 diff --git a/bank.py b/bank.py index d87bf42ba0..a6c92914f1 100644 --- a/bank.py +++ b/bank.py @@ -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' diff --git a/exceptions.py b/exceptions.py index 857162d304..15272b0f28 100644 --- a/exceptions.py +++ b/exceptions.py @@ -6,3 +6,7 @@ from trytond.model.exceptions import ValidationError class IBANValidationError(ValidationError): pass + + +class InvalidBIC(ValidationError): + pass diff --git a/message.xml b/message.xml index dc5c4d67db..af84a91d7e 100644 --- a/message.xml +++ b/message.xml @@ -6,5 +6,8 @@ this repository contains the full copyright notices and license terms. --> IBAN number "%(number)s" is not valid. + + The BIC "%(bic)s" is not valid. + diff --git a/tests/test_bank.py b/tests/test_bank.py index e81975bdf4..3d12c1dab6 100644 --- a/tests/test_bank.py +++ b/tests/test_bank.py @@ -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'