From 7ad314d53ec0c849b884b949d9d7f8b3ce933dda Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Sat, 16 May 2020 01:28:57 +0200 Subject: [PATCH] ofx: black and others... --- .../__manifest__.py | 38 ++--- .../models/__init__.py | 2 +- .../models/account_journal.py | 2 +- .../tests/test_import_bank_statement.py | 142 +++++++++--------- .../view_account_bank_statement_import.xml | 7 +- .../wizard/account_bank_statement_import.py | 54 ++++--- 6 files changed, 127 insertions(+), 118 deletions(-) diff --git a/account_bank_statement_import_ofx/__manifest__.py b/account_bank_statement_import_ofx/__manifest__.py index 96b0dfd8..3ac95a12 100644 --- a/account_bank_statement_import_ofx/__manifest__.py +++ b/account_bank_statement_import_ofx/__manifest__.py @@ -1,24 +1,18 @@ { - 'name': 'Import OFX Bank Statement', - 'category': 'Banking addons', - 'version': '13.0.1.0.0', - 'license': 'AGPL-3', - 'author': 'Odoo SA,' - 'Akretion,' - 'La Louve,' - 'GRAP,' - 'Nicolas JEUDY,' - 'Le Filament,' - 'Odoo Community Association (OCA)', - 'website': 'https://github.com/OCA/bank-statement-import', - 'depends': [ - 'account_bank_statement_import', - ], - 'data': [ - 'views/view_account_bank_statement_import.xml', - ], - 'external_dependencies': { - 'python': ['ofxparse'], - }, - 'installable': True, + "name": "Import OFX Bank Statement", + "category": "Banking addons", + "version": "13.0.1.0.0", + "license": "AGPL-3", + "author": "Odoo SA," + "Akretion," + "La Louve," + "GRAP," + "Nicolas JEUDY," + "Le Filament," + "Odoo Community Association (OCA)", + "website": "https://github.com/OCA/bank-statement-import", + "depends": ["account_bank_statement_import",], + "data": ["views/view_account_bank_statement_import.xml",], + "external_dependencies": {"python": ["ofxparse"],}, + "installable": True, } diff --git a/account_bank_statement_import_ofx/models/__init__.py b/account_bank_statement_import_ofx/models/__init__.py index e18569e6..2388e119 100644 --- a/account_bank_statement_import_ofx/models/__init__.py +++ b/account_bank_statement_import_ofx/models/__init__.py @@ -1 +1 @@ -from . import account_journal \ No newline at end of file +from . import account_journal diff --git a/account_bank_statement_import_ofx/models/account_journal.py b/account_bank_statement_import_ofx/models/account_journal.py index be290b94..fb6c480e 100644 --- a/account_bank_statement_import_ofx/models/account_journal.py +++ b/account_bank_statement_import_ofx/models/account_journal.py @@ -8,5 +8,5 @@ class AccountJournal(models.Model): """ Adds ofx to supported import formats. """ rslt = super()._get_bank_statements_available_import_formats() - rslt.append('ofx') + rslt.append("ofx") return rslt diff --git a/account_bank_statement_import_ofx/tests/test_import_bank_statement.py b/account_bank_statement_import_ofx/tests/test_import_bank_statement.py index fd27cd65..752e0614 100644 --- a/account_bank_statement_import_ofx/tests/test_import_bank_statement.py +++ b/account_bank_statement_import_ofx/tests/test_import_bank_statement.py @@ -1,8 +1,9 @@ -from odoo.tests.common import TransactionCase -from odoo.modules.module import get_module_resource import base64 import datetime +from odoo.modules.module import get_module_resource +from odoo.tests.common import TransactionCase + class TestOfxFile(TransactionCase): """Tests for import bank statement ofx file format @@ -11,90 +12,95 @@ class TestOfxFile(TransactionCase): def setUp(self): super(TestOfxFile, self).setUp() - self.absi_model = self.env['account.bank.statement.import'] - self.abs_model = self.env['account.bank.statement'] - self.j_model = self.env['account.journal'] - self.absl_model = self.env['account.bank.statement.line'] - self.ia_model = self.env['ir.attachment'] - cur = self.env.ref('base.USD') - self.env.ref('base.main_company').currency_id = cur.id - bank = self.env['res.partner.bank'].create({ - 'acc_number': '123456', - 'partner_id': self.env.ref('base.main_partner').id, - 'company_id': self.env.ref('base.main_company').id, - 'bank_id': self.env.ref('base.res_bank_1').id, - }) - self.env['account.journal'].create({ - 'name': 'Bank Journal TEST OFX', - 'code': 'BNK12', - 'type': 'bank', - 'bank_account_id': bank.id, - }) + self.absi_model = self.env["account.bank.statement.import"] + self.abs_model = self.env["account.bank.statement"] + self.j_model = self.env["account.journal"] + self.absl_model = self.env["account.bank.statement.line"] + self.ia_model = self.env["ir.attachment"] + cur = self.env.ref("base.USD") + self.env.ref("base.main_company").currency_id = cur.id + bank = self.env["res.partner.bank"].create( + { + "acc_number": "123456", + "partner_id": self.env.ref("base.main_partner").id, + "company_id": self.env.ref("base.main_company").id, + "bank_id": self.env.ref("base.res_bank_1").id, + } + ) + self.env["account.journal"].create( + { + "name": "Bank Journal TEST OFX", + "code": "BNK12", + "type": "bank", + "bank_account_id": bank.id, + } + ) - bank_iban_ofx = self.env['res.partner.bank'].create({ - 'acc_number': 'FR7630001007941234567890185', - 'partner_id': self.env.ref('base.main_partner').id, - 'company_id': self.env.ref('base.main_company').id, - 'bank_id': self.env.ref('base.res_bank_1').id, - }) + bank_iban_ofx = self.env["res.partner.bank"].create( + { + "acc_number": "FR7630001007941234567890185", + "partner_id": self.env.ref("base.main_partner").id, + "company_id": self.env.ref("base.main_company").id, + "bank_id": self.env.ref("base.res_bank_1").id, + } + ) - self.env['account.journal'].create({ - 'name': 'FR7630001007941234567890185', - 'code': 'BNK13', - 'type': 'bank', - 'bank_account_id': bank_iban_ofx.id, - }) + self.env["account.journal"].create( + { + "name": "FR7630001007941234567890185", + "code": "BNK13", + "type": "bank", + "bank_account_id": bank_iban_ofx.id, + } + ) def test_wrong_ofx_file_import(self): ofx_file_path = get_module_resource( - 'account_bank_statement_import_ofx', - 'tests/test_ofx_file/', 'test_ofx_wrong.ofx') - ofx_file_wrong = base64.b64encode(open(ofx_file_path, 'rb').read()) - attach = self.ia_model.create({ - 'name': 'test_ofx_wrong.ofx', - 'datas': ofx_file_wrong, - }) + "account_bank_statement_import_ofx", + "tests/test_ofx_file/", + "test_ofx_wrong.ofx", + ) + ofx_file_wrong = base64.b64encode(open(ofx_file_path, "rb").read()) + attach = self.ia_model.create( + {"name": "test_ofx_wrong.ofx", "datas": ofx_file_wrong,} + ) bank_statement = self.absi_model.create( - dict(attachment_ids=[(6, 0, [attach.id])])) + dict(attachment_ids=[(6, 0, [attach.id])]) + ) self.assertFalse(bank_statement._check_ofx(data_file=ofx_file_wrong)) def test_ofx_file_import(self): ofx_file_path = get_module_resource( - 'account_bank_statement_import_ofx', - 'tests/test_ofx_file/', 'test_ofx.ofx') - ofx_file = base64.b64encode(open(ofx_file_path, 'rb').read()) - attach = self.ia_model.create({ - 'name': 'test_ofx.ofx', - 'datas': ofx_file, - }) + "account_bank_statement_import_ofx", "tests/test_ofx_file/", "test_ofx.ofx" + ) + ofx_file = base64.b64encode(open(ofx_file_path, "rb").read()) + attach = self.ia_model.create({"name": "test_ofx.ofx", "datas": ofx_file,}) bank_statement = self.absi_model.create( - dict(attachment_ids=[(6, 0, [attach.id])])) + dict(attachment_ids=[(6, 0, [attach.id])]) + ) bank_statement.import_file() - bank_st_record = self.abs_model.search( - [('name', 'like', '123456')])[0] + bank_st_record = self.abs_model.search([("name", "like", "123456")])[0] self.assertEqual(bank_st_record.balance_start, 2516.56) self.assertEqual(bank_st_record.balance_end_real, 2156.56) - line = self.absl_model.search([ - ('name', '=', 'Agrolait'), - ('statement_id', '=', bank_st_record.id)])[0] - self.assertEqual(line.ref, '219378') + line = self.absl_model.search( + [("name", "=", "Agrolait"), ("statement_id", "=", bank_st_record.id)] + )[0] + self.assertEqual(line.ref, "219378") self.assertEqual(line.date, datetime.date(2013, 8, 24)) def test_check_journal_bank_account(self): ofx_file_path = get_module_resource( - 'account_bank_statement_import_ofx', - 'tests/test_ofx_file/', 'test_ofx_iban.ofx') - ofx_file = base64.b64encode(open(ofx_file_path, 'rb').read()) - attach = self.ia_model.create({ - 'name': 'test_ofx.ofx', - 'datas': ofx_file, - }) - bank_st = self.absi_model.create( - dict(attachment_ids=[(6, 0, [attach.id])])) - journal_iban_ofx = self.j_model.search([ - ('name', '=', 'FR7630001007941234567890185')]) - res = bank_st._check_journal_bank_account(journal_iban_ofx, - '12345678901') + "account_bank_statement_import_ofx", + "tests/test_ofx_file/", + "test_ofx_iban.ofx", + ) + ofx_file = base64.b64encode(open(ofx_file_path, "rb").read()) + attach = self.ia_model.create({"name": "test_ofx.ofx", "datas": ofx_file,}) + bank_st = self.absi_model.create(dict(attachment_ids=[(6, 0, [attach.id])])) + journal_iban_ofx = self.j_model.search( + [("name", "=", "FR7630001007941234567890185")] + ) + res = bank_st._check_journal_bank_account(journal_iban_ofx, "12345678901") self.assertTrue(res) bank_st.with_context(journal_id=journal_iban_ofx.id).import_file() diff --git a/account_bank_statement_import_ofx/views/view_account_bank_statement_import.xml b/account_bank_statement_import_ofx/views/view_account_bank_statement_import.xml index 046ed1e4..c8cd372b 100644 --- a/account_bank_statement_import_ofx/views/view_account_bank_statement_import.xml +++ b/account_bank_statement_import_ofx/views/view_account_bank_statement_import.xml @@ -1,8 +1,11 @@ - + account.bank.statement.import - +
  • Open Financial Exchange (.OFX Money)
  • diff --git a/account_bank_statement_import_ofx/wizard/account_bank_statement_import.py b/account_bank_statement_import_ofx/wizard/account_bank_statement_import.py index 775f76be..9618f43c 100644 --- a/account_bank_statement_import_ofx/wizard/account_bank_statement_import.py +++ b/account_bank_statement_import_ofx/wizard/account_bank_statement_import.py @@ -1,10 +1,13 @@ -import logging import io +import logging -from odoo import api, models, _ +from odoo import _, api, models from odoo.exceptions import UserError -from odoo.addons.base_iban.models.res_partner_bank import _map_iban_template -from odoo.addons.base_iban.models.res_partner_bank import validate_iban + +from odoo.addons.base_iban.models.res_partner_bank import ( + _map_iban_template, + validate_iban, +) _logger = logging.getLogger(__name__) @@ -16,7 +19,7 @@ except ImportError: class AccountBankStatementImport(models.TransientModel): - _inherit = 'account.bank.statement.import' + _inherit = "account.bank.statement.import" def _check_journal_bank_account(self, journal, account_number): res = super()._check_journal_bank_account(journal, account_number) @@ -25,11 +28,11 @@ class AccountBankStatementImport(models.TransientModel): e_acc_num = e_acc_num.replace(" ", "") validate_iban(e_acc_num) country_code = e_acc_num[:2].lower() - iban_template = _map_iban_template[country_code].replace( - " ", "") + iban_template = _map_iban_template[country_code].replace(" ", "") e_acc_num = "".join( - [c for c, t in zip(e_acc_num, iban_template) if t == "C"]) - res = (e_acc_num == account_number) + [c for c, t in zip(e_acc_num, iban_template) if t == "C"] + ) + res = e_acc_num == account_number return res @api.model @@ -57,11 +60,11 @@ class AccountBankStatementImport(models.TransientModel): if transaction.memo: name += " : " + transaction.memo vals = { - 'date': transaction.date, - 'name': name, - 'ref': transaction.id, - 'amount': float(transaction.amount), - 'unique_import_id': transaction.id, + "date": transaction.date, + "name": name, + "ref": transaction.id, + "amount": float(transaction.amount), + "unique_import_id": transaction.id, } return vals @@ -77,18 +80,21 @@ class AccountBankStatementImport(models.TransientModel): vals = self._prepare_ofx_transaction_line(transaction) if vals: transactions.append(vals) - total_amt += vals['amount'] + total_amt += vals["amount"] except Exception as e: - raise UserError(_( - "The following problem occurred during import. " - "The file might not be valid.\n\n %s") % e.message) + raise UserError( + _( + "The following problem occurred during import. " + "The file might not be valid.\n\n %s" + ) + % e.message + ) balance = float(ofx.account.statement.balance) vals_bank_statement = { - 'name': ofx.account.number, - 'transactions': transactions, - 'balance_start': balance - total_amt, - 'balance_end_real': balance, + "name": ofx.account.number, + "transactions": transactions, + "balance_start": balance - total_amt, + "balance_end_real": balance, } - return ofx.account.statement.currency, ofx.account.number, [ - vals_bank_statement] + return ofx.account.statement.currency, ofx.account.number, [vals_bank_statement]