mirror of
https://github.com/OCA/bank-statement-import.git
synced 2025-01-20 12:37:43 +02:00
[RFR] Don't unzip xlsx files
This commit is contained in:
@@ -53,6 +53,7 @@ class AccountBankStatementImport(models.TransientModel):
|
|||||||
'Bank Statement File', required=True,
|
'Bank Statement File', required=True,
|
||||||
help='Get you bank statements in electronic format from your bank '
|
help='Get you bank statements in electronic format from your bank '
|
||||||
'and select them here.')
|
'and select them here.')
|
||||||
|
filename = fields.Char()
|
||||||
|
|
||||||
@api.multi
|
@api.multi
|
||||||
def import_file(self):
|
def import_file(self):
|
||||||
@@ -62,7 +63,8 @@ class AccountBankStatementImport(models.TransientModel):
|
|||||||
data_file = base64.b64decode(self.data_file)
|
data_file = base64.b64decode(self.data_file)
|
||||||
# pylint: disable=protected-access
|
# pylint: disable=protected-access
|
||||||
statement_ids, notifications = self.with_context(
|
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)
|
)._import_file(data_file)
|
||||||
# dispatch to reconciliation interface
|
# dispatch to reconciliation interface
|
||||||
action = self.env.ref(
|
action = self.env.ref(
|
||||||
@@ -77,6 +79,20 @@ class AccountBankStatementImport(models.TransientModel):
|
|||||||
'type': 'ir.actions.client',
|
'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
|
@api.model
|
||||||
def _parse_all_files(self, data_file):
|
def _parse_all_files(self, data_file):
|
||||||
"""Parse one file or multiple files from zip-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.
|
Return array of statements for further processing.
|
||||||
"""
|
"""
|
||||||
statements = []
|
statements = []
|
||||||
files = [data_file]
|
files = self.unzip(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
|
|
||||||
# Parse the file(s)
|
# Parse the file(s)
|
||||||
for import_file in files:
|
for import_file in files:
|
||||||
# The appropriate implementation module(s) returns the statements.
|
# The appropriate implementation module(s) returns the statements.
|
||||||
|
|||||||
@@ -95,6 +95,7 @@ class TestStatementFile(TransactionCase):
|
|||||||
bank_statement_id = import_model.create(
|
bank_statement_id = import_model.create(
|
||||||
dict(
|
dict(
|
||||||
data_file=statement_file,
|
data_file=statement_file,
|
||||||
|
filename=file_name,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
bank_statement_id.import_file()
|
bank_statement_id.import_file()
|
||||||
|
|||||||
@@ -8,7 +8,8 @@
|
|||||||
<field name="priority">1</field>
|
<field name="priority">1</field>
|
||||||
<field name="arch" type="xml">
|
<field name="arch" type="xml">
|
||||||
<form string="Import Bank Statements">
|
<form string="Import Bank Statements">
|
||||||
<field name="data_file"/>
|
<field name="data_file" filename="filename"/>
|
||||||
|
<field name="filename" invisible="1"/>
|
||||||
<field name="hide_journal_field" invisible="1"/>
|
<field name="hide_journal_field" invisible="1"/>
|
||||||
<label for="journal_id"/>
|
<label for="journal_id"/>
|
||||||
<field name="journal_id"
|
<field name="journal_id"
|
||||||
|
|||||||
Reference in New Issue
Block a user