From 2ca0f0e5d1ea2c4a894bc72c99ae7dea8053597a Mon Sep 17 00:00:00 2001 From: qdp-odoo Date: Fri, 16 Jan 2015 18:38:18 +0100 Subject: [PATCH] [FIX] account_bank_statement_import: ofx and qif don't save the uploaded file on the server anymore + data_file is now mandatory in _parse_file() --- .../account_bank_statement_import.py | 2 +- .../account_bank_statement_import_ofx.py | 17 +++-------------- .../account_bank_statement_import_qif.py | 12 ++++-------- .../l10n_be_coda/wizard/account_coda_import.py | 2 +- 4 files changed, 9 insertions(+), 24 deletions(-) diff --git a/addons/account_bank_statement_import/account_bank_statement_import.py b/addons/account_bank_statement_import/account_bank_statement_import.py index a175f5d0e96..6a39a6a2920 100644 --- a/addons/account_bank_statement_import/account_bank_statement_import.py +++ b/addons/account_bank_statement_import/account_bank_statement_import.py @@ -70,7 +70,7 @@ class account_bank_statement_import(osv.TransientModel): 'type': 'ir.actions.client', } - def _parse_file(self, cr, uid, data_file=None, context=None): + def _parse_file(self, cr, uid, data_file, context=None): """ Each module adding a file support must extends this method. It processes the file if it can, returns super otherwise, resulting in a chain of responsability. This method parses the given file and returns the data required by the bank statement import process, as specified below. rtype: triplet (if a value can't be retrieved, use None) diff --git a/addons/account_bank_statement_import_ofx/account_bank_statement_import_ofx.py b/addons/account_bank_statement_import_ofx/account_bank_statement_import_ofx.py index 7f197d9ea7a..6aa840917e9 100644 --- a/addons/account_bank_statement_import_ofx/account_bank_statement_import_ofx.py +++ b/addons/account_bank_statement_import_ofx/account_bank_statement_import_ofx.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- import logging -import os +import StringIO from openerp.osv import osv from openerp.tools.translate import _ @@ -28,17 +28,8 @@ class account_bank_statement_import(osv.TransientModel): return False return ofx - def _parse_file(self, cr, uid, data_file=None, context=None): - try: - tempfile = open("temp.ofx", "w+") - tempfile.write(data_file) - tempfile.read() - pathname = os.path.dirname('temp.ofx') - path = os.path.join(os.path.abspath(pathname), 'temp.ofx') - except: - raise UserError(_('File handling error.')) - - ofx = self._check_ofx(cr, uid, file(path), context=context) + def _parse_file(self, cr, uid, data_file, context=None): + ofx = self._check_ofx(cr, uid, StringIO.StringIO(data_file), context=context) if not ofx: return super(account_bank_statement_import, self)._parse_file(cr, uid, data_file, context=context) @@ -65,7 +56,6 @@ class account_bank_statement_import(osv.TransientModel): total_amt += float(transaction.amount) transactions.append(vals_line) except Exception, e: - os.remove(path) raise UserError(_("The following problem occurred during import. The file might not be valid.\n\n %s" % e.message)) vals_bank_statement = { @@ -74,5 +64,4 @@ class account_bank_statement_import(osv.TransientModel): 'balance_start': ofx.account.statement.balance, 'balance_end_real': float(ofx.account.statement.balance) + total_amt, } - os.remove(path) return ofx.account.statement.currency, ofx.account.number, [vals_bank_statement] diff --git a/addons/account_bank_statement_import_qif/account_bank_statement_import_qif.py b/addons/account_bank_statement_import_qif/account_bank_statement_import_qif.py index 95c2eb28e1a..65f7241ec20 100644 --- a/addons/account_bank_statement_import_qif/account_bank_statement_import_qif.py +++ b/addons/account_bank_statement_import_qif/account_bank_statement_import_qif.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- import dateutil.parser -from tempfile import TemporaryFile +import StringIO from openerp.tools.translate import _ from openerp.osv import osv, fields @@ -23,7 +23,7 @@ class account_bank_statement_import(osv.TransientModel): } def _get_journal(self, cr, uid, currency_id, bank_account_id, account_number, context=None): - """ As .QIF format does not allow us to detect the journal, we need to let the user choose it. + """ As .QIF format does not allow us to detect the journal, we need to let the user choose it. We set it in context before to call super so it's the same as calling the widget from a journal """ if context is None: context = {} @@ -36,18 +36,14 @@ class account_bank_statement_import(osv.TransientModel): def _check_qif(self, cr, uid, data_file, context=None): return data_file.strip().startswith('!Type:') - def _parse_file(self, cr, uid, data_file=None, context=None): + def _parse_file(self, cr, uid, data_file, context=None): if not self._check_qif(cr, uid, data_file, context=context): return super(account_bank_statement_import, self)._parse_file(cr, uid, data_file, context=context) try: - fileobj = TemporaryFile('wb+') - fileobj.write(data_file) - fileobj.seek(0) file_data = "" - for line in fileobj.readlines(): + for line in StringIO.StringIO(data_file).readlines(): file_data += line - fileobj.close() if '\r' in file_data: data_list = file_data.split('\r') else: diff --git a/addons/l10n_be_coda/wizard/account_coda_import.py b/addons/l10n_be_coda/wizard/account_coda_import.py index 7fc3aaddd6a..bca1f989853 100644 --- a/addons/l10n_be_coda/wizard/account_coda_import.py +++ b/addons/l10n_be_coda/wizard/account_coda_import.py @@ -38,7 +38,7 @@ class account_bank_statement_import(osv.TransientModel): # Matches the first 24 characters of a CODA file, as defined by the febelfin specifications return re.match('0{5}\d{9}05[ D] {7}', data_file) != None - def _parse_file(self, cr, uid, data_file=None, context=None): + def _parse_file(self, cr, uid, data_file, context=None): if not self._check_coda(cr, uid, data_file, context=context): return super(account_bank_statement_import, self)._parse_file(cr, uid, data_file, context=context)