From dca15a8f09789452d432f5c000e868dbcbd6d3e1 Mon Sep 17 00:00:00 2001 From: "Laurent Mignon (ACSONE)" Date: Thu, 21 May 2015 14:03:51 +0200 Subject: [PATCH] [IMP] use new API Conflicts: account_bank_statement_import/account_bank_statement_import.py --- account_bank_statement_import_ofx/__init__.py | 2 +- .../account_bank_statement_import_ofx.py | 48 +++++++++++-------- .../tests/test_import_bank_statement.py | 27 ++++++----- 3 files changed, 45 insertions(+), 32 deletions(-) diff --git a/account_bank_statement_import_ofx/__init__.py b/account_bank_statement_import_ofx/__init__.py index 9955438..fb5e0c3 100644 --- a/account_bank_statement_import_ofx/__init__.py +++ b/account_bank_statement_import_ofx/__init__.py @@ -1,3 +1,3 @@ # -*- encoding: utf-8 -*- -import account_bank_statement_import_ofx +from . import account_bank_statement_import_ofx diff --git a/account_bank_statement_import_ofx/account_bank_statement_import_ofx.py b/account_bank_statement_import_ofx/account_bank_statement_import_ofx.py index 0442f03..93c8931 100644 --- a/account_bank_statement_import_ofx/account_bank_statement_import_ofx.py +++ b/account_bank_statement_import_ofx/account_bank_statement_import_ofx.py @@ -1,11 +1,9 @@ # -*- coding: utf-8 -*- -# noqa: This is a backport from Odoo. OCA has no control over style here. -# flake8: noqa import logging import StringIO -from openerp.osv import osv +from openerp import api, models from openerp.tools.translate import _ from openerp.exceptions import Warning @@ -17,37 +15,46 @@ except ImportError: _logger.warn("ofxparse not found, OFX parsing disabled.") ofxparser = None -class account_bank_statement_import(osv.TransientModel): + +class account_bank_statement_import(models.TransientModel): _inherit = 'account.bank.statement.import' - def _check_ofx(self, cr, uid, file, context=None): + @api.model + def _check_ofx(self, data_file): if ofxparser is None: return False try: - ofx = ofxparser.parse(file) + ofx = ofxparser.parse(StringIO.StringIO(data_file)) except: return False return ofx - def _parse_file(self, cr, uid, data_file, context=None): - ofx = self._check_ofx(cr, uid, StringIO.StringIO(data_file), context=context) + @api.model + def _parse_file(self, data_file): + ofx = self._check_ofx(data_file) if not ofx: - return super(account_bank_statement_import, self)._parse_file(cr, uid, data_file, context=context) + return super(account_bank_statement_import, self)._parse_file( + data_file) transactions = [] total_amt = 0.00 try: for transaction in ofx.account.statement.transactions: - # Since ofxparse doesn't provide account numbers, we'll have to find res.partner and res.partner.bank here - # (normal behavious is to provide 'account_number', which the generic module uses to find partner/bank) + # Since ofxparse doesn't provide account numbers, we'll have + # to find res.partner and res.partner.bank here + # (normal behavious is to provide 'account_number', which the + # generic module uses to find partner/bank) bank_account_id = partner_id = False - ids = self.pool.get('res.partner.bank').search(cr, uid, [('owner_name', '=', transaction.payee)], context=context) - if ids: - bank_account_id = bank_account_id = ids[0] - partner_id = self.pool.get('res.partner.bank').browse(cr, uid, bank_account_id, context=context).partner_id.id + banks = self.env['res.partner.bank'].search( + [('owner_name', '=', transaction.payee)], limit=1) + if banks: + bank_account = banks[0] + bank_account_id = bank_account.id + partner_id = bank_account.partner_id.id vals_line = { 'date': transaction.date, - 'name': transaction.payee + (transaction.memo and ': ' + transaction.memo or ''), + 'name': transaction.payee + ( + transaction.memo and ': ' + transaction.memo or ''), 'ref': transaction.id, 'amount': transaction.amount, 'unique_import_id': transaction.id, @@ -57,12 +64,15 @@ class account_bank_statement_import(osv.TransientModel): total_amt += float(transaction.amount) transactions.append(vals_line) except Exception, e: - raise Warning(_("The following problem occurred during import. The file might not be valid.\n\n %s" % e.message)) + raise Warning(_("The following problem occurred during import. " + "The file might not be valid.\n\n %s" % e.message)) vals_bank_statement = { 'name': ofx.account.routing_number, 'transactions': transactions, 'balance_start': ofx.account.statement.balance, - 'balance_end_real': float(ofx.account.statement.balance) + total_amt, + 'balance_end_real': + float(ofx.account.statement.balance) + total_amt, } - return ofx.account.statement.currency, ofx.account.number, [vals_bank_statement] + return ofx.account.statement.currency, ofx.account.number, [ + vals_bank_statement] 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 2391fd1..578fe57 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 @@ -4,29 +4,32 @@ from openerp.tests.common import TransactionCase from openerp.modules.module import get_module_resource + class TestOfxFile(TransactionCase): - """Tests for import bank statement ofx file format (account.bank.statement.import) + """Tests for import bank statement ofx file format + (account.bank.statement.import) """ def setUp(self): super(TestOfxFile, self).setUp() - self.statement_import_model = self.registry('account.bank.statement.import') - self.bank_statement_model = self.registry('account.bank.statement') + self.statement_import_model = self.env['account.bank.statement.import'] + self.bank_statement_model = self.env['account.bank.statement'] def test_ofx_file_import(self): try: from ofxparse import OfxParser as ofxparser except ImportError: - #the Python library isn't installed on the server, the OFX import is unavailable and the test cannot be run + # the Python library isn't installed on the server, the OFX import + # is unavailable and the test cannot be run return True - cr, uid = self.cr, self.uid - ofx_file_path = get_module_resource('account_bank_statement_import_ofx', 'test_ofx_file', 'test_ofx.ofx') + ofx_file_path = get_module_resource( + 'account_bank_statement_import_ofx', + 'test_ofx_file', 'test_ofx.ofx') ofx_file = open(ofx_file_path, 'rb').read().encode('base64') - bank_statement_id = self.statement_import_model.create(cr, uid, dict( - data_file=ofx_file, - )) - self.statement_import_model.import_file(cr, uid, [bank_statement_id]) - statement_id = self.bank_statement_model.search(cr, uid, [('name', '=', '000000123')])[0] - bank_st_record = self.bank_statement_model.browse(cr, uid, statement_id) + bank_statement = self.statement_import_model.create( + dict(data_file=ofx_file)) + bank_statement.import_file() + bank_st_record = self.bank_statement_model.search( + [('name', '=', '000000123')])[0] self.assertEquals(bank_st_record.balance_start, 2156.56) self.assertEquals(bank_st_record.balance_end_real, 1796.56)