Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[15.0][MIG] base_import_async #434

Open
wants to merge 2 commits into
base: 15.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 7 additions & 4 deletions base_import_async/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
59 changes: 41 additions & 18 deletions base_import_async/models/base_import_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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 []
Expand All @@ -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

Expand Down Expand Up @@ -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)))
Expand All @@ -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)
Expand All @@ -169,9 +179,22 @@ def _split_file(
priority += 1

def _import_one_chunk(self, model_name, attachment, options):
model_obj = self.env[model_name]
fields, data = self._read_csv_attachment(attachment, options)
result = model_obj.load(fields, data)
import_fields, merged_data = self._handle_multi_mapping(fields, data)
if options.get("fallback_values"):
merged_data = self._handle_fallback_values(
import_fields, merged_data, options["fallback_values"]
)
name_create_enabled_fields = options.pop("name_create_enabled_fields", {})
import_limit = options.pop("limit", None)
model = self.env[model_name].with_context(
import_file=True,
name_create_enabled_fields=name_create_enabled_fields,
import_set_empty_fields=options.get("import_set_empty_fields", []),
import_skip_records=options.get("import_skip_records", []),
_import_limit=import_limit,
)
result = model.load(import_fields, merged_data)
error_message = [
message["message"]
for message in result["messages"]
Expand Down
2 changes: 1 addition & 1 deletion base_import_async/models/queue_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


class QueueJob(models.Model):
""" Job status and result """
"""Job status and result"""

_inherit = "queue.job"

Expand Down
13 changes: 10 additions & 3 deletions base_import_async/static/src/xml/import.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
<?xml version="1.0" encoding="utf-8" ?>
<templates>
<t t-extend="ImportView">
<t t-extend="ImportView.side_panel">
<t t-jquery="#oe_import_has_header" t-operation="before">
<div
title="When checked, the import will be executed as a background job,
after splitting your file in small chunks that will be processed independently.
Use this to import very large files."
>
<input type="checkbox" class="oe_import_queue" id="oe_import_queue" />
<label for="oe_import_queue">Import in the background</label>
<input
type="checkbox"
class="oe_import_queue custom-control-input"
id="oe_import_queue"
/>
<label
for="oe_import_queue"
class="custom-control-label"
>Import in the background</label>
</div>
</t>
</t>
Expand Down
15 changes: 0 additions & 15 deletions base_import_async/views/base_import_async.xml

This file was deleted.

1 change: 1 addition & 0 deletions setup/base_import_async/odoo/addons/base_import_async
6 changes: 6 additions & 0 deletions setup/base_import_async/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)