From 2d9260a40610962a42ee85ac0e0d8a24b72dee7a Mon Sep 17 00:00:00 2001 From: Rad0van Date: Wed, 25 May 2022 22:03:35 +0200 Subject: [PATCH 1/2] [15.0][FIX][IMP]base_import_async --- .pre-commit-config.yaml | 1 - base_import_async/__manifest__.py | 11 +++-- .../models/base_import_import.py | 42 ++++++++++++------- base_import_async/models/queue_job.py | 2 +- base_import_async/static/src/xml/import.xml | 13 ++++-- base_import_async/views/base_import_async.xml | 15 ------- .../odoo/addons/base_import_async | 1 + setup/base_import_async/setup.py | 6 +++ 8 files changed, 51 insertions(+), 40 deletions(-) delete mode 100644 base_import_async/views/base_import_async.xml create mode 120000 setup/base_import_async/odoo/addons/base_import_async create mode 100644 setup/base_import_async/setup.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 19c47388d4..28a1777142 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -2,7 +2,6 @@ exclude: | (?x) # NOT INSTALLABLE ADDONS ^base_export_async/| - ^base_import_async/| ^queue_job_subscribe/| ^test_base_import_async/| # END NOT INSTALLABLE ADDONS diff --git a/base_import_async/__manifest__.py b/base_import_async/__manifest__.py index 205960ff84..0807aa91dd 100644 --- a/base_import_async/__manifest__.py +++ b/base_import_async/__manifest__.py @@ -5,14 +5,17 @@ { "name": "Asynchronous Import", "summary": "Import CSV files in the background", - "version": "14.0.1.0.1", + "version": "15.0.1.0.1", "author": "Akretion, ACSONE SA/NV, Odoo Community Association (OCA)", "license": "AGPL-3", "website": "https://github.com/OCA/queue", "category": "Generic Modules", "depends": ["base_import", "queue_job"], - "data": ["data/queue_job_function_data.xml", "views/base_import_async.xml"], - "qweb": ["static/src/xml/import.xml"], - "installable": False, + "data": ["data/queue_job_function_data.xml"], + "assets": { + "web.assets_qweb": ["/base_import_async/static/src/xml/import.xml"], + "web.assets_backend": ["/base_import_async/static/src/js/import.js"], + }, + "installable": True, "development_status": "Production/Stable", } diff --git a/base_import_async/models/base_import_import.py b/base_import_async/models/base_import_import.py index de08c5efb1..0f2a1c5f20 100644 --- a/base_import_async/models/base_import_import.py +++ b/base_import_async/models/base_import_import.py @@ -32,10 +32,10 @@ class BaseImportImport(models.TransientModel): _inherit = "base_import.import" - def do(self, fields, columns, options, dryrun=False): + def execute_import(self, fields, columns, options, dryrun=False): if dryrun or not options.get(OPT_USE_QUEUE): # normal import - return super().do(fields, columns, options, dryrun=dryrun) + return super().execute_import(fields, columns, options, dryrun=dryrun) # asynchronous import try: @@ -52,19 +52,23 @@ def do(self, fields, columns, options, dryrun=False): translated_model_name = search_result[0][1] else: translated_model_name = self._description - description = _("Import %s from file %s") % ( - translated_model_name, - self.file_name, + description = _( + "Import %(translated_model_name)s from file %(file_name)s", + translated_model_name=translated_model_name, + file_name=self.file_name, ) + file_name = self.file_name + if not file_name.endswith(".csv"): + file_name += ".csv" attachment = self._create_csv_attachment( - import_fields, data, options, self.file_name + import_fields, data, options, file_name ) delayed_job = self.with_delay(description=description)._split_file( model_name=self.res_model, translated_model_name=translated_model_name, attachment=attachment, options=options, - file_name=self.file_name, + file_name=file_name, ) self._link_attachment_to_job(delayed_job, attachment) return [] @@ -91,7 +95,12 @@ def _create_csv_attachment(self, fields, data, options, file_name): # create attachment datas = base64.encodebytes(f.getvalue().encode(encoding)) attachment = self.env["ir.attachment"].create( - {"name": file_name, "datas": datas} + { + "name": file_name, + "datas": datas, + "type": "binary", + "mimetype": "text/csv", + } ) return attachment @@ -130,7 +139,7 @@ def _split_file( options, file_name="file.csv", ): - """ Split a CSV attachment in smaller import jobs """ + """Split a CSV attachment in smaller import jobs""" model_obj = self.env[model_name] fields, data = self._read_csv_attachment(attachment, options) padding = len(str(len(data))) @@ -144,13 +153,14 @@ def _split_file( model_obj, fields, data, chunk_size ): chunk = str(priority - INIT_PRIORITY).zfill(padding) - description = _("Import %s from file %s - #%s - lines %s to %s") - description = description % ( - translated_model_name, - file_name, - chunk, - row_from + 1 + header_offset, - row_to + 1 + header_offset, + description = _( + "Import %(translated_model_name)s from file %(file_name)s" + " - #%(chunk)s - lines %(row_from)s to %(row_to)s", + translated_model_name=translated_model_name, + file_name=file_name, + chunk=chunk, + row_from=row_from + 1 + header_offset, + row_to=row_to + 1 + header_offset, ) # create a CSV attachment and enqueue the job root, ext = splitext(file_name) diff --git a/base_import_async/models/queue_job.py b/base_import_async/models/queue_job.py index fd1faec28e..e3594f3568 100644 --- a/base_import_async/models/queue_job.py +++ b/base_import_async/models/queue_job.py @@ -5,7 +5,7 @@ class QueueJob(models.Model): - """ Job status and result """ + """Job status and result""" _inherit = "queue.job" diff --git a/base_import_async/static/src/xml/import.xml b/base_import_async/static/src/xml/import.xml index d401157a14..87a7469790 100644 --- a/base_import_async/static/src/xml/import.xml +++ b/base_import_async/static/src/xml/import.xml @@ -1,14 +1,21 @@ - +
- - + +
diff --git a/base_import_async/views/base_import_async.xml b/base_import_async/views/base_import_async.xml deleted file mode 100644 index fadc5d69c3..0000000000 --- a/base_import_async/views/base_import_async.xml +++ /dev/null @@ -1,15 +0,0 @@ - - -