changeset 72a104f5469c in modules/bank:default
details: https://hg.tryton.org/modules/bank?cmd=changeset;node=72a104f5469c
description:
        Validate and format BIC

        issue8422
        review255561002
diffstat:

 CHANGELOG          |   2 ++
 bank.py            |  22 ++++++++++++++++++++--
 exceptions.py      |   4 ++++
 message.xml        |   3 +++
 tests/test_bank.py |  24 ++++++++++++++++++++++++
 5 files changed, 53 insertions(+), 2 deletions(-)

diffs (116 lines):

diff -r 7b3dc92ea364 -r 72a104f5469c CHANGELOG
--- a/CHANGELOG Mon May 06 15:01:01 2019 +0200
+++ b/CHANGELOG Wed Jun 26 22:10:31 2019 +0200
@@ -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 -r 7b3dc92ea364 -r 72a104f5469c bank.py
--- a/bank.py   Mon May 06 15:01:01 2019 +0200
+++ b/bank.py   Wed Jun 26 22:10:31 2019 +0200
@@ -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 @@
     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 -r 7b3dc92ea364 -r 72a104f5469c exceptions.py
--- a/exceptions.py     Mon May 06 15:01:01 2019 +0200
+++ b/exceptions.py     Wed Jun 26 22:10:31 2019 +0200
@@ -6,3 +6,7 @@
 
 class IBANValidationError(ValidationError):
     pass
+
+
+class InvalidBIC(ValidationError):
+    pass
diff -r 7b3dc92ea364 -r 72a104f5469c message.xml
--- a/message.xml       Mon May 06 15:01:01 2019 +0200
+++ b/message.xml       Wed Jun 26 22:10:31 2019 +0200
@@ -6,5 +6,8 @@
         <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>
diff -r 7b3dc92ea364 -r 72a104f5469c tests/test_bank.py
--- a/tests/test_bank.py        Mon May 06 15:01:01 2019 +0200
+++ b/tests/test_bank.py        Wed Jun 26 22:10:31 2019 +0200
@@ -5,12 +5,36 @@
 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'
         pool = Pool()

Reply via email to