diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index cdbd3cf757..10a2e7f371 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,7 +1,6 @@ exclude: | (?x) # NOT INSTALLABLE ADDONS - ^base_import_async/| ^queue_job_batch/| ^queue_job_cron/| ^queue_job_cron_jobrunner/| diff --git a/base_import_async/README.rst b/base_import_async/README.rst index 030c371758..2d6c79f705 100644 --- a/base_import_async/README.rst +++ b/base_import_async/README.rst @@ -1,3 +1,7 @@ +.. image:: https://odoo-community.org/readme-banner-image + :target: https://odoo-community.org/get-involved?utm_source=readme + :alt: Odoo Community Association + =================== Asynchronous Import =================== @@ -13,17 +17,17 @@ Asynchronous Import .. |badge1| image:: https://img.shields.io/badge/maturity-Production%2FStable-green.png :target: https://odoo-community.org/page/development-status :alt: Production/Stable -.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png +.. |badge2| image:: https://img.shields.io/badge/license-AGPL--3-blue.png :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html :alt: License: AGPL-3 .. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fqueue-lightgray.png?logo=github - :target: https://github.com/OCA/queue/tree/18.0/base_import_async + :target: https://github.com/OCA/queue/tree/20.0/base_import_async :alt: OCA/queue .. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png - :target: https://translation.odoo-community.org/projects/queue-18-0/queue-18-0-base_import_async + :target: https://translation.odoo-community.org/projects/queue-20-0/queue-20-0-base_import_async :alt: Translate me on Weblate .. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png - :target: https://runboat.odoo-community.org/builds?repo=OCA/queue&target_branch=18.0 + :target: https://runboat.odoo-community.org/builds?repo=OCA/queue&target_branch=20.0 :alt: Try me on Runboat |badge1| |badge2| |badge3| |badge4| |badge5| @@ -87,7 +91,7 @@ Bug Tracker Bugs are tracked on `GitHub Issues `_. In case of trouble, please check there if your issue has already been reported. If you spotted it first, help us to smash it by providing a detailed and welcomed -`feedback `_. +`feedback `_. Do not contact contributors directly about support or help with technical issues. @@ -130,12 +134,6 @@ Other contributors include: - Daniel Duque (FactorLibre) -Other credits -------------- - -The migration of this module from 17.0 to 18.0 was financially supported -by Camptocamp - Maintainers ----------- @@ -149,6 +147,6 @@ OCA, or the Odoo Community Association, is a nonprofit organization whose mission is to support the collaborative development of Odoo features and promote its widespread use. -This module is part of the `OCA/queue `_ project on GitHub. +This module is part of the `OCA/queue `_ project on GitHub. You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/base_import_async/__manifest__.py b/base_import_async/__manifest__.py index 5432d7c5ca..239de060d0 100644 --- a/base_import_async/__manifest__.py +++ b/base_import_async/__manifest__.py @@ -5,7 +5,7 @@ { "name": "Asynchronous Import", "summary": "Import CSV files in the background", - "version": "18.0.1.0.0", + "version": "20.0.1.0.0", "author": "Akretion, ACSONE SA/NV, Odoo Community Association (OCA)", "license": "AGPL-3", "website": "https://github.com/OCA/queue", @@ -20,6 +20,6 @@ "base_import_async/static/src/xml/import_data_sidepanel.xml", ], }, - "installable": False, + "installable": True, "development_status": "Production/Stable", } diff --git a/base_import_async/data/queue_job_function_data.xml b/base_import_async/data/queue_job_function_data.xml index 22cc8dbab0..0437b18545 100644 --- a/base_import_async/data/queue_job_function_data.xml +++ b/base_import_async/data/queue_job_function_data.xml @@ -1,3 +1,4 @@ + diff --git a/base_import_async/models/base_import_import.py b/base_import_async/models/base_import_import.py index 856828a593..073c2b26f2 100644 --- a/base_import_async/models/base_import_import.py +++ b/base_import_async/models/base_import_import.py @@ -4,12 +4,11 @@ # @author Sébastien BEAU # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -import base64 import csv -from io import BytesIO, StringIO, TextIOWrapper +from io import StringIO, TextIOWrapper from os.path import splitext -from odoo import _, api, models +from odoo import models from odoo.models import fix_import_export_id_paths from odoo.addons.base_import.models.base_import import ImportValidationError @@ -30,7 +29,8 @@ DEFAULT_CHUNK_SIZE = 100 -class BaseImportImport(models.TransientModel): +# Upstream base_import.import lives in models/; keep path for backwards compatibility. +class BaseImportImport(models.TransientModel): # pylint: disable=no-wizard-in-models _inherit = "base_import.import" def execute_import(self, fields, columns, options, dryrun=False): @@ -55,10 +55,11 @@ def execute_import(self, fields, columns, options, dryrun=False): translated_model_name = search_result[0][1] else: translated_model_name = self._description - description = _("Import %(model)s from file %(from_file)s") % { - "model": translated_model_name, - "from_file": self.file_name, - } + description = self.env._( + "Import %(model)s from file %(from_file)s", + model=translated_model_name, + from_file=self.file_name, + ) attachment = self._create_csv_attachment( import_fields, data, options, self.file_name ) @@ -67,7 +68,7 @@ def execute_import(self, fields, columns, options, dryrun=False): translated_model_name=translated_model_name, attachment=attachment, options=options, - file_name=self.file_name, + file_name=self.file_name or "file.csv", ) self._link_attachment_to_job(delayed_job, attachment) return [] @@ -78,7 +79,6 @@ def _link_attachment_to_job(self, delayed_job, attachment): ) attachment.write({"res_model": "queue.job", "res_id": queue_job.id}) - @api.returns("ir.attachment") def _create_csv_attachment(self, fields, data, options, file_name): # write csv f = StringIO() @@ -94,30 +94,29 @@ def _create_csv_attachment(self, fields, data, options, file_name): # create attachment. Remove default values from context context = self.env.context context_copy = {} - for key in context.keys(): + for key in context: if not key.startswith("default_"): context_copy[key] = context[key] - datas = base64.encodebytes(f.getvalue().encode(encoding)) attachment = ( self.env["ir.attachment"] .with_context(**context_copy) - .create({"name": file_name, "datas": datas}) + .create({"name": file_name, "raw": f.getvalue().encode(encoding)}) ) return attachment @staticmethod def _read_csv_attachment(attachment, options): - decoded_datas = base64.decodebytes(attachment.datas) encoding = options.get(OPT_ENCODING) or "utf-8" - f = TextIOWrapper(BytesIO(decoded_datas), encoding=encoding) - reader = csv.reader( - f, - delimiter=str(options.get(OPT_SEPARATOR)) or ",", - quotechar=str(options.get(OPT_QUOTING)), - ) + with attachment.raw.open() as raw_file: + f = TextIOWrapper(raw_file, encoding=encoding) + reader = csv.reader( + f, + delimiter=str(options.get(OPT_SEPARATOR)) or ",", + quotechar=str(options.get(OPT_QUOTING)), + ) - fields = next(reader) - data = [row for row in reader] + fields = next(reader) + data = [row for row in reader] return fields, data @staticmethod @@ -155,16 +154,17 @@ def _split_file( model_obj, fields, data, chunk_size ): chunk = str(priority - INIT_PRIORITY).zfill(padding) - description = _( + description = self.env._( "Import %(model)s from file %(file_name)s - " - "#%(chunk)s - lines %(from)s to %(to)s" - ) % { - "model": translated_model_name, - "file_name": file_name, - "chunk": chunk, - "from": row_from + 1 + header_offset, - "to": row_to + 1 + header_offset, - } + "#%(chunk)s - lines %(from)s to %(to)s", + **{ + "model": translated_model_name, + "file_name": file_name, + "chunk": chunk, + "from": row_from + 1 + header_offset, + "to": row_to + 1 + header_offset, + }, + ) # create a CSV attachment and enqueue the job root, ext = splitext(file_name) attachment = self._create_csv_attachment( diff --git a/base_import_async/models/queue_job.py b/base_import_async/models/queue_job.py index b7313505f3..f3034e9d54 100644 --- a/base_import_async/models/queue_job.py +++ b/base_import_async/models/queue_job.py @@ -1,7 +1,7 @@ # Copyright 2017 ACSONE SA/NV # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -from odoo import _, models +from odoo import models class QueueJob(models.Model): @@ -11,7 +11,7 @@ class QueueJob(models.Model): def _related_action_attachment(self): return { - "name": _("Attachment"), + "name": self.env._("Attachment"), "type": "ir.actions.act_window", "res_model": "ir.attachment", "view_mode": "form", diff --git a/base_import_async/readme/CREDITS.md b/base_import_async/readme/CREDITS.md deleted file mode 100644 index 76edf63081..0000000000 --- a/base_import_async/readme/CREDITS.md +++ /dev/null @@ -1,2 +0,0 @@ -The migration of this module from 17.0 to 18.0 was financially supported -by Camptocamp diff --git a/base_import_async/static/description/index.html b/base_import_async/static/description/index.html index 2994cf9efa..665e4177d9 100644 --- a/base_import_async/static/description/index.html +++ b/base_import_async/static/description/index.html @@ -360,16 +360,21 @@ -
-

Asynchronous Import

+
+ + +Odoo Community Association + +
+

Asynchronous Import

-

Production/Stable License: AGPL-3 OCA/queue Translate me on Weblate Try me on Runboat

+

Production/Stable License: AGPL-3 OCA/queue Translate me on Weblate Try me on Runboat

This module extends the standard CSV import functionality to import files in the background using the OCA/queue framework.

Table of contents

@@ -385,14 +390,13 @@

Asynchronous Import

  • Credits
  • -

    Usage

    +

    Usage

    The user is presented with a new checkbox in the import screen. When selected, the import is delayed in a background job.

    This job in turn splits the CSV file in chunks of minimum 100 lines (or @@ -416,7 +420,7 @@

    Usage

    -

    Known issues / Roadmap

    +

    Known issues / Roadmap

    • There is currently no user interface to control the chunk size, which is currently 100 by default. Should this proves to be an issue, it is @@ -425,33 +429,33 @@

      Known issues / Roadmap

    -

    Changelog

    +

    Changelog

    -

    13.0.1.0.0 (2019-12-20)

    +

    13.0.1.0.0 (2019-12-20)

    • [MIGRATION] from 12.0 branched at rev. a7f8031
    -

    Bug Tracker

    +

    Bug Tracker

    Bugs are tracked on GitHub Issues. In case of trouble, please check there if your issue has already been reported. If you spotted it first, help us to smash it by providing a detailed and welcomed -feedback.

    +feedback.

    Do not contact contributors directly about support or help with technical issues.

    -

    Credits

    +

    Credits

    -

    Authors

    +

    Authors

    • Akretion
    • ACSONE SA/NV
    -

    Contributors

    +

    Contributors

    Sébastien Beau (Akretion) authored the initial prototype.

    Stéphane Bidoul (ACSONE) extended it to version 1.0 to support multi-line records, store data to import as attachments and let the user @@ -472,13 +476,8 @@

    Contributors

  • Daniel Duque (FactorLibre)
  • -
    -

    Other credits

    -

    The migration of this module from 17.0 to 18.0 was financially supported -by Camptocamp

    -
    -

    Maintainers

    +

    Maintainers

    This module is maintained by the OCA.

    Odoo Community Association @@ -486,10 +485,11 @@

    Maintainers

    OCA, or the Odoo Community Association, is a nonprofit organization whose mission is to support the collaborative development of Odoo features and promote its widespread use.

    -

    This module is part of the OCA/queue project on GitHub.

    +

    This module is part of the OCA/queue project on GitHub.

    You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

    +
    diff --git a/base_import_async/static/src/xml/import_data_sidepanel.xml b/base_import_async/static/src/xml/import_data_sidepanel.xml index 8c3ef0f5c8..7db570eb10 100644 --- a/base_import_async/static/src/xml/import_data_sidepanel.xml +++ b/base_import_async/static/src/xml/import_data_sidepanel.xml @@ -1,4 +1,4 @@ - + diff --git a/base_import_async/tests/test_base_import_import.py b/base_import_async/tests/test_base_import_import.py index 4a815d6707..a1e2d9f034 100644 --- a/base_import_async/tests/test_base_import_import.py +++ b/base_import_async/tests/test_base_import_import.py @@ -2,6 +2,9 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from odoo.tests.common import RecordCapturer, TransactionCase +from odoo.tools import BinaryBytes + +from odoo.addons.queue_job.job import Job from ..models.base_import_import import OPT_USE_QUEUE @@ -18,22 +21,22 @@ def test_normal_import_res_partners(self): [ "name", "email", - "is_company", + "city", ], [ "partner 1", "partner1@example.com", - "1", + "Paris", ], [ "partner 2", "partner2@example.com", - "0", + "Lyon", ], ] import_vals = { "res_model": self.res_partners._name, - "file": "\n".join([";".join(values) for values in values]), + "file": BinaryBytes("\n".join([";".join(row) for row in values]).encode()), "file_type": "text/csv", } self.import_wizard |= self.import_wizard.create(import_vals) @@ -44,7 +47,7 @@ def test_normal_import_res_partners(self): { 0: ["name"], 1: ["email"], - 2: ["is_company"], + 2: ["city"], }, ) with RecordCapturer(self.res_partners, []) as capture: @@ -64,36 +67,82 @@ def test_wrong_import_res_partners(self): [ "name", "email", - "date", # Adding date field to trigger parsing error + # res.partner.date removed in Odoo 19; use float field for parse error + "partner_latitude", ], [ "partner 1", "partner1@example.com", - "21-13-2024", + "invalid_float", ], [ "partner 2", "partner2@example.com", - "2024-13-45", + "bad_value", ], ] opts = { "quoting": '"', "separator": ";", "has_headers": True, - "date_format": "%Y-%m-%d", # Set specific date format OPT_USE_QUEUE: True, } import_vals = { "res_model": self.res_partners._name, - "file": "\n".join([";".join(row) for row in values]), + "file": BinaryBytes("\n".join([";".join(row) for row in values]).encode()), "file_type": "text/csv", } import_wizard = self.import_wizard.create(import_vals) - preview = import_wizard.parse_preview(opts) results = import_wizard.execute_import( - [field[0] for field in preview["matches"].values()], - ["name", "email", "date"], # Include date in fields to import + ["name", "email", "partner_latitude"], + ["name", "email", "partner_latitude"], opts, ) self.assertTrue(any(msg["type"] == "error" for msg in results["messages"])) + + def test_async_import_res_partners(self): + values = [ + ["name", "email", "city"], + ["async partner 1", "async1@example.com", "Paris"], + ["async partner 2", "async2@example.com", "Lyon"], + ] + import_wizard = self.import_wizard.create( + { + "res_model": self.res_partners._name, + "file": BinaryBytes( + "\n".join(";".join(row) for row in values).encode() + ), + "file_name": "partners.csv", + "file_type": "text/csv", + } + ) + opts = { + "quoting": '"', + "separator": ";", + "has_headers": True, + OPT_USE_QUEUE: True, + } + preview = import_wizard.parse_preview(opts) + with RecordCapturer(self.res_partners, []) as capture: + result = import_wizard.execute_import( + [fnames[0] for fnames in preview["matches"].values()], + [], + opts, + ) + self.assertEqual(result, []) + split_job = self.env["queue.job"].search( + [("method_name", "=", "_split_file")], order="id desc", limit=1 + ) + self.assertTrue(split_job) + Job.load(self.env, split_job.uuid).perform() + chunk_jobs = self.env["queue.job"].search( + [("method_name", "=", "_import_one_chunk")], order="id desc" + ) + self.assertTrue(chunk_jobs) + for chunk_job in chunk_jobs: + Job.load(self.env, chunk_job.uuid).perform() + self.assertEqual(len(capture.records), 2) + self.assertEqual( + set(capture.records.mapped("email")), + {"async1@example.com", "async2@example.com"}, + )