Skip to content

Commit

Permalink
[IMP] product_import: import products by batch
Browse files Browse the repository at this point in the history
  • Loading branch information
florentx committed Dec 9, 2024
1 parent 4a22ef2 commit c246786
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
4 changes: 2 additions & 2 deletions product_import/data/job_function.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<field name="parent_id" ref="queue_job.channel_root" />
</record>

<record id="job_create_update_product" model="queue.job.function">
<record id="job_create_update_products" model="queue.job.function">
<field name="model_id" ref="model_product_import" />
<field name="method">_create_update_product</field>
<field name="method">_create_update_products</field>
<field name="channel_id" ref="channel_product_import" />
</record>

Expand Down
47 changes: 39 additions & 8 deletions product_import/wizard/product_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
import logging
from base64 import b64decode, b64encode
from datetime import date, timedelta
from itertools import zip_longest

from lxml import etree

from odoo import _, api, fields, models
from odoo.exceptions import UserError

CHUNK_SIZE = 40

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -197,19 +200,44 @@ def _prepare_product(self, parsed_product, chatter_msg, seller=None):
return product_vals

@api.model
def _create_update_product(self, parsed_product, seller_id):
def _create_update_products(self, products, seller_id):
"""Create / Update a product.
This method is called from a queue job.
"""
chatter_msg = []

seller = self.env["res.partner"].browse(seller_id)
product_vals = self._prepare_product(parsed_product, chatter_msg, seller=seller)
if not product_vals:
return False

log_msgs = []
for parsed_product in products:
product_vals = self._prepare_product(
parsed_product, log_msgs, seller=seller
)
if product_vals:
msg = self._create_update_product(product_vals)
log_msgs.append(msg)

return "\n".join(log_msgs)

@api.model
def _create_update_product(self, product_vals):
"""Create / Update a product.
This method is called from a queue job.
"""
chatter_msg = []

product = product_vals.pop("recordset", None)
if product:
supplierinfo = product_vals.pop("seller_ids", ())
supplierinfo_obj = self.env["product.supplierinfo"]
for (command, line_id, values) in supplierinfo:
if command == 1:
supplierinfo_obj.browse(line_id).write(values)
elif command == 0:
supplierinfo_obj.create({**values, "product_id": product.id})
else:
raise RuntimeError(f"Command {command} not supported")
product.write(product_vals)
logger.debug("Product %s updated", product.default_code)
else:
Expand All @@ -235,9 +263,12 @@ def import_button(self):
seller = self._get_seller(catalogue)
wiz = self.with_context(product_company_id=company_id)
# Create products asynchronously
for product_vals in catalogue["products"]:
# One job per product
wiz.with_delay()._create_update_product(product_vals, seller.id)
iterators = [iter(catalogue["products"])] * chunk_size
for products in zip_longest(*iterators):
if products[-1] is None:
products = [product for product in products if product]
# One job for x products (chunk of 40)
wiz.with_delay()._create_update_products(products, seller.id)
# Save imported file as attachment
self._bdimport.post_create_or_update(
catalogue, seller, doc_filename=self.product_filename
Expand Down

0 comments on commit c246786

Please sign in to comment.