Skip to content

Commit

Permalink
[IMP] product_import: Import products asynchronously
Browse files Browse the repository at this point in the history
  • Loading branch information
florentx committed Nov 29, 2024
1 parent 56e83cf commit 96ad6db
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 19 deletions.
3 changes: 3 additions & 0 deletions product_import/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
"stock",
# OCA/edi
"base_business_document_import",
# OCA/queue
"queue_job",
],
"data": [
"security/ir.model.access.csv",
"wizard/product_import_view.xml",
"data/job_function.xml",
],
}
9 changes: 9 additions & 0 deletions product_import/data/job_function.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<odoo noupdate="1">

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

</odoo>
3 changes: 2 additions & 1 deletion product_import/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class TestCommon(SavepointCase):
def setUpClass(cls):
super().setUpClass()
cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True))
cls.wiz_model = cls.env["product.import"]
# Execute directly, no job
cls.wiz_model = cls.env["product.import"].with_context(queue_job__no_delay=True)
cls.supplier = cls.env["res.partner"].create({"name": "Catalogue Vendor"})

def _mock(self, method_name):
Expand Down
3 changes: 2 additions & 1 deletion product_import/tests/test_product_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,10 @@ def test_get_company_id(self):

def test_product_import(self):
# product.product
products = self.wiz_model._create_products(
self.wiz_model._import_products(
self.parsed_catalog, seller=self.supplier
)
products = self.env["product.product"].search([], order="id")[-3:]
self.assertEqual(len(products), 3)
for product, parsed in zip(products, PARSED_CATALOG["products"]):

Expand Down
39 changes: 23 additions & 16 deletions product_import/wizard/product_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,15 @@ def _prepare_product(self, parsed_product, chatter_msg, seller=None):
return product_vals

@api.model
def create_product(self, parsed_product, chatter_msg, seller=None):
def create_update_product(self, parsed_product, chatter_msg, seller_id):
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
product = product_vals.pop("recordset", None)
if product:
product.write(product_vals)
logger.info("Product %d updated", product.id)
logger.debug("Product %s updated", product.default_code)
else:
product_active = product_vals.pop("active")
product = self.env["product.product"].create(product_vals)
Expand All @@ -211,23 +212,29 @@ def create_product(self, parsed_product, chatter_msg, seller=None):
# all characteristics into product.template
product.flush()
product.action_archive()
logger.info("Product %d created", product.id)
logger.debug("Product %s created", product.default_code)
return product

@api.model
def _create_products(self, catalogue, seller, filename=None):
products = self.env["product.product"].browse()
for product in catalogue.get("products"):
record = self.create_product(
product,
catalogue["chatter_msg"],
seller=seller,
)
if record:
products |= record
def _create_update_product(self, parsed_product, seller_id):
"""Create / Update a product.
This method is called from a queue job.
"""
messgs = []
product = self.create_update_product(parsed_product, messgs, seller_id)
log_msg = f"Product created/updated {product.id}\n" + "\n".join(messgs)
return log_msg

@api.model
def _import_products(self, catalogue, seller, filename=None):
for product_vals in catalogue["products"]:
# One job per product
self.with_delay()._create_update_product(product_vals, seller.id)
self._bdimport.post_create_or_update(catalogue, seller, doc_filename=filename)
logger.info("Products updated for vendor %d", seller.id)
return products
logger.info(
"Update for vendor %s: %d products", seller.name, len(catalogue["products"])
)

def import_button(self):
self.ensure_one()
Expand All @@ -237,7 +244,7 @@ def import_button(self):
raise UserError(_("This catalogue doesn't have any product!"))
company_id = self._get_company_id(catalogue)
seller = self._get_seller(catalogue)
self.with_context(product_company_id=company_id)._create_products(
self.with_context(product_company_id=company_id)._import_products(
catalogue, seller, filename=self.product_filename
)
return {"type": "ir.actions.act_window_close"}
8 changes: 7 additions & 1 deletion product_import_ubl/tests/test_ubl_catalogue_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ class TestUblOrderImport(SavepointCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True))
cls.env = cls.env(
context={
**cls.env.context,
"tracking_disable": True,
"queue_job__no_delay": True,
}
)
cls.supplier = cls.env["res.partner"].create(
{"name": "Medical", "ref": "78456123"}
)
Expand Down

0 comments on commit 96ad6db

Please sign in to comment.