From 02d4ee0047a493e004a85c3470a01882c6e75ca9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Mart=C3=ADnez?= Date: Mon, 17 Oct 2022 09:11:17 +0200 Subject: [PATCH] [IMP] account_bank_statement_import_qif: black, isort, prettier --- account_statement_import_qif/__manifest__.py | 24 +++--- .../tests/test_import_bank_statement.py | 39 +++++---- .../account_bank_statement_import_qif.py | 80 ++++++++++--------- ...account_bank_statement_import_qif_view.xml | 5 +- 4 files changed, 75 insertions(+), 73 deletions(-) diff --git a/account_statement_import_qif/__manifest__.py b/account_statement_import_qif/__manifest__.py index f5fca4db..4c73d642 100644 --- a/account_statement_import_qif/__manifest__.py +++ b/account_statement_import_qif/__manifest__.py @@ -5,19 +5,13 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). { - 'name': 'Import QIF Bank Statements', - 'category': 'Accounting', - 'version': '11.0.1.0.1', - 'author': 'OpenERP SA,' - 'Tecnativa,' - 'Odoo Community Association (OCA)', - 'website': 'https://github.com/OCA/bank-statement-import', - 'depends': [ - 'account_bank_statement_import', - ], - 'data': [ - 'wizards/account_bank_statement_import_qif_view.xml', - ], - 'installable': True, - 'license': 'AGPL-3', + "name": "Import QIF Bank Statements", + "category": "Accounting", + "version": "11.0.1.0.1", + "author": "OpenERP SA," "Tecnativa," "Odoo Community Association (OCA)", + "website": "https://github.com/OCA/bank-statement-import", + "depends": ["account_bank_statement_import",], + "data": ["wizards/account_bank_statement_import_qif_view.xml",], + "installable": True, + "license": "AGPL-3", } diff --git a/account_statement_import_qif/tests/test_import_bank_statement.py b/account_statement_import_qif/tests/test_import_bank_statement.py index b5269e2e..052fc038 100644 --- a/account_statement_import_qif/tests/test_import_bank_statement.py +++ b/account_statement_import_qif/tests/test_import_bank_statement.py @@ -4,10 +4,11 @@ # Copyright 2016-2017 Tecnativa - Pedro M. Baeza # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from odoo.tests.common import TransactionCase -from odoo.modules.module import get_module_resource import base64 +from odoo.modules.module import get_module_resource +from odoo.tests.common import TransactionCase + class TestQifFile(TransactionCase): """Tests for import bank statement qif file format @@ -16,34 +17,32 @@ class TestQifFile(TransactionCase): def setUp(self): super(TestQifFile, self).setUp() - self.statement_import_model = self.env['account.bank.statement.import'] - self.statement_line_model = self.env['account.bank.statement.line'] - self.journal = self.env['account.journal'].create({ - 'name': 'Test bank journal', - 'code': 'TEST', - 'type': 'bank', - }) - self.partner = self.env['res.partner'].create({ - # Different case for trying insensitive case search - 'name': 'EPIC Technologies', - }) + self.statement_import_model = self.env["account.bank.statement.import"] + self.statement_line_model = self.env["account.bank.statement.line"] + self.journal = self.env["account.journal"].create( + {"name": "Test bank journal", "code": "TEST", "type": "bank",} + ) + self.partner = self.env["res.partner"].create( + { + # Different case for trying insensitive case search + "name": "EPIC Technologies", + } + ) def test_qif_file_import(self): qif_file_path = get_module_resource( - 'account_bank_statement_import_qif', 'tests', 'test_qif.qif', + "account_bank_statement_import_qif", "tests", "test_qif.qif", ) - qif_file = base64.b64encode(open(qif_file_path, 'rb').read()) + qif_file = base64.b64encode(open(qif_file_path, "rb").read()) wizard = self.statement_import_model.with_context( journal_id=self.journal.id - ).create( - dict(data_file=qif_file) - ) + ).create(dict(data_file=qif_file)) wizard.import_file() statement = self.statement_line_model.search( - [('name', '=', 'YOUR LOCAL SUPERMARKET')], limit=1, + [("name", "=", "YOUR LOCAL SUPERMARKET")], limit=1, )[0].statement_id self.assertAlmostEqual(statement.balance_end_real, -1896.09, 2) line = self.statement_line_model.search( - [('name', '=', 'Epic Technologies')], limit=1, + [("name", "=", "Epic Technologies")], limit=1, ) self.assertEqual(line.partner_id, self.partner) diff --git a/account_statement_import_qif/wizards/account_bank_statement_import_qif.py b/account_statement_import_qif/wizards/account_bank_statement_import_qif.py index 7f9e6fec..39c6c7fa 100644 --- a/account_statement_import_qif/wizards/account_bank_statement_import_qif.py +++ b/account_statement_import_qif/wizards/account_bank_statement_import_qif.py @@ -6,9 +6,9 @@ import dateutil.parser -from odoo.tools.translate import _ from odoo import api, models from odoo.exceptions import UserError +from odoo.tools.translate import _ class AccountBankStatementImport(models.TransientModel): @@ -16,22 +16,21 @@ class AccountBankStatementImport(models.TransientModel): @api.model def _check_qif(self, data_file): - return data_file.strip().startswith(b'!Type:') + return data_file.strip().startswith(b"!Type:") def _parse_file(self, data_file): if not self._check_qif(data_file): - return super(AccountBankStatementImport, self)._parse_file( - data_file) + return super(AccountBankStatementImport, self)._parse_file(data_file) try: file_data = data_file.decode() - if '\r' in file_data: - data_list = file_data.split('\r') + if "\r" in file_data: + data_list = file_data.split("\r") else: - data_list = file_data.split('\n') + data_list = file_data.split("\n") header = data_list[0].strip() header = header.split(":")[1] except: - raise UserError(_('Could not decipher the QIF file.')) + raise UserError(_("Could not decipher the QIF file.")) transactions = [] vals_line = {} total = 0 @@ -41,37 +40,44 @@ class AccountBankStatementImport(models.TransientModel): line = line.strip() if not line: continue - if line[0] == 'D': # date of transaction - vals_line['date'] = dateutil.parser.parse( - line[1:], fuzzy=True).date() - elif line[0] == 'T': # Total amount - total += float(line[1:].replace(',', '')) - vals_line['amount'] = float(line[1:].replace(',', '')) - elif line[0] == 'N': # Check number - vals_line['ref'] = line[1:] - elif line[0] == 'P': # Payee - vals_line['name'] = ( - 'name' in vals_line and - line[1:] + ': ' + vals_line['name'] or line[1:] + if line[0] == "D": # date of transaction + vals_line["date"] = dateutil.parser.parse( + line[1:], fuzzy=True + ).date() + elif line[0] == "T": # Total amount + total += float(line[1:].replace(",", "")) + vals_line["amount"] = float(line[1:].replace(",", "")) + elif line[0] == "N": # Check number + vals_line["ref"] = line[1:] + elif line[0] == "P": # Payee + vals_line["name"] = ( + "name" in vals_line + and line[1:] + ": " + vals_line["name"] + or line[1:] ) - elif line[0] == 'M': # Memo - vals_line['name'] = ('name' in vals_line and - vals_line['name'] + ': ' + line[1:] or - line[1:]) - elif line[0] == '^' and vals_line: # end of item + elif line[0] == "M": # Memo + vals_line["name"] = ( + "name" in vals_line + and vals_line["name"] + ": " + line[1:] + or line[1:] + ) + elif line[0] == "^" and vals_line: # end of item transactions.append(vals_line) vals_line = {} - elif line[0] == '\n': + elif line[0] == "\n": transactions = [] else: pass else: - raise UserError(_('This file is either not a bank statement or is ' - 'not correctly formed.')) - vals_bank_statement.update({ - 'balance_end_real': total, - 'transactions': transactions - }) + raise UserError( + _( + "This file is either not a bank statement or is " + "not correctly formed." + ) + ) + vals_bank_statement.update( + {"balance_end_real": total, "transactions": transactions} + ) return None, None, [vals_bank_statement] def _complete_stmts_vals(self, stmt_vals, journal_id, account_number): @@ -82,12 +88,12 @@ class AccountBankStatementImport(models.TransientModel): # Since QIF doesn't provide account numbers (normal behaviour is to # provide 'account_number', which the generic module uses to find # the partner), we have to find res.partner through the name - partner_obj = self.env['res.partner'] + partner_obj = self.env["res.partner"] for statement in res: - for line_vals in statement['transactions']: - if not line_vals.get('partner_id') and line_vals.get('name'): + for line_vals in statement["transactions"]: + if not line_vals.get("partner_id") and line_vals.get("name"): partner = partner_obj.search( - [('name', 'ilike', line_vals['name'])], limit=1, + [("name", "ilike", line_vals["name"])], limit=1, ) - line_vals['partner_id'] = partner.id + line_vals["partner_id"] = partner.id return res diff --git a/account_statement_import_qif/wizards/account_bank_statement_import_qif_view.xml b/account_statement_import_qif/wizards/account_bank_statement_import_qif_view.xml index d6ab5b7f..d9bdc4c0 100644 --- a/account_statement_import_qif/wizards/account_bank_statement_import_qif_view.xml +++ b/account_statement_import_qif/wizards/account_bank_statement_import_qif_view.xml @@ -3,7 +3,10 @@ account.bank.statement.import - +
  • Quicken Interchange Format (.qif)