From 287f87a9975907f9d571d350f8f7f57ee55c4020 Mon Sep 17 00:00:00 2001 From: Stefan Rijnhart Date: Tue, 28 Feb 2017 11:04:17 +0100 Subject: [PATCH] [RFR] Don't unzip xlsx files --- .../models/account_bank_statement_import.py | 28 ++++++++++++------- .../tests/test_import_file.py | 1 + .../account_bank_statement_import_view.xml | 3 +- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/account_bank_statement_import/models/account_bank_statement_import.py b/account_bank_statement_import/models/account_bank_statement_import.py index 3fbfbfd..b9a1c2d 100644 --- a/account_bank_statement_import/models/account_bank_statement_import.py +++ b/account_bank_statement_import/models/account_bank_statement_import.py @@ -53,6 +53,7 @@ class AccountBankStatementImport(models.TransientModel): 'Bank Statement File', required=True, help='Get you bank statements in electronic format from your bank ' 'and select them here.') + filename = fields.Char() @api.multi def import_file(self): @@ -62,7 +63,8 @@ class AccountBankStatementImport(models.TransientModel): data_file = base64.b64decode(self.data_file) # pylint: disable=protected-access statement_ids, notifications = self.with_context( - active_id=self.id # pylint: disable=no-member + active_id=self.id, # pylint: disable=no-member + filename=self.filename )._import_file(data_file) # dispatch to reconciliation interface action = self.env.ref( @@ -77,6 +79,20 @@ class AccountBankStatementImport(models.TransientModel): 'type': 'ir.actions.client', } + @api.model + def unzip(self, data_file): + filename = self.env.context.get('filename') + if filename and filename.lower().endswith('.xlsx'): + return [data_file] + try: + with ZipFile(StringIO(data_file), 'r') as archive: + return [ + archive.read(name) for name in archive.namelist() + if not name.endswith('/') + ] + except BadZipfile: + return [data_file] + @api.model def _parse_all_files(self, data_file): """Parse one file or multiple files from zip-file. @@ -84,15 +100,7 @@ class AccountBankStatementImport(models.TransientModel): Return array of statements for further processing. """ statements = [] - files = [data_file] - try: - with ZipFile(StringIO(data_file), 'r') as archive: - files = [ - archive.read(filename) for filename in archive.namelist() - if not filename.endswith('/') - ] - except BadZipfile: - pass + files = self.unzip(data_file) # Parse the file(s) for import_file in files: # The appropriate implementation module(s) returns the statements. diff --git a/account_bank_statement_import/tests/test_import_file.py b/account_bank_statement_import/tests/test_import_file.py index 71ed1e0..3047cac 100644 --- a/account_bank_statement_import/tests/test_import_file.py +++ b/account_bank_statement_import/tests/test_import_file.py @@ -95,6 +95,7 @@ class TestStatementFile(TransactionCase): bank_statement_id = import_model.create( dict( data_file=statement_file, + filename=file_name, ) ) bank_statement_id.import_file() diff --git a/account_bank_statement_import/views/account_bank_statement_import_view.xml b/account_bank_statement_import/views/account_bank_statement_import_view.xml index 6aaa888..f7ce5c0 100644 --- a/account_bank_statement_import/views/account_bank_statement_import_view.xml +++ b/account_bank_statement_import/views/account_bank_statement_import_view.xml @@ -8,7 +8,8 @@ 1
- + +