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

[14.0][IMP] product_import: reorganize for inheritance to import by batch #1076

Open
wants to merge 3 commits into
base: 14.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
4 changes: 2 additions & 2 deletions product_import/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ def setUpClass(cls):
cls.wiz_model = cls.env["product.import"]
cls.supplier = cls.env["res.partner"].create({"name": "Catalogue Vendor"})

def _mock(self, method_name):
return mock.patch.object(type(self.wiz_model), method_name)
def _mock(self, method_name, **kw):
return mock.patch.object(type(self.wiz_model), method_name, **kw)

@property
def wiz_form(self):
Expand Down
13 changes: 10 additions & 3 deletions product_import/tests/test_product_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
},
],
"ref": "1387",
"company": {"name": "Customer ABC"},
"seller": {
"contact": False,
"email": False,
Expand Down Expand Up @@ -88,9 +89,15 @@ def test_get_company_id(self):

def test_product_import(self):
# product.product
products = self.wiz_model._create_products(
self.parsed_catalog, seller=self.supplier
product_obj = self.env["product.product"].with_context(active_test=False)
existing = product_obj.search([], order="id")

wiz = self.wiz_model.create(
{"product_file": b"====", "product_filename": "test_import.xml"}
)
with self._mock("parse_product_catalogue", return_value=self.parsed_catalog):
wiz.import_button()
products = product_obj.search([], order="id") - existing
self.assertEqual(len(products), 3)
for product, parsed in zip(products, PARSED_CATALOG["products"]):

Expand Down Expand Up @@ -140,7 +147,7 @@ def test_product_import(self):
self.assertEqual(product.seller_ids, product_tmpl.seller_ids)
self.assertEqual(
product.seller_ids.mapped("delay")[0], parsed.get("sale_delay", 0)
),
)

def test_import_button(self):
form = self.wiz_form
Expand Down
71 changes: 48 additions & 23 deletions product_import/wizard/product_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,10 @@
return result

@api.model
def _prepare_product(self, parsed_product, chatter_msg, seller=None):
def _prepare_product(self, parsed_product, seller, company_id, chatter_msg):
# Important: barcode is unique key of product.template model
# So records product.product are created with company_id=False.
# Only the pricelist (product.supplierinfo) is company-specific.
product_company_id = self.env.context.get("product_company_id", False)
if not parsed_product["barcode"]:
chatter_msg.append(
_("Cannot import product without barcode: %s") % (parsed_product,)
Expand Down Expand Up @@ -187,7 +186,7 @@
"price": parsed_product["price"],
"currency_id": currency.id,
"min_qty": parsed_product["min_qty"],
"company_id": product_company_id,
"company_id": company_id,
"delay": parsed_product.get("sale_delay", 0),
}
product_vals["seller_ids"] = self._prepare_supplierinfo(seller_info, product)
Expand All @@ -197,14 +196,12 @@
return product_vals

@api.model
def create_product(self, parsed_product, chatter_msg, seller=None):
product_vals = self._prepare_product(parsed_product, chatter_msg, seller=seller)
if not product_vals:
return False
def _save_product(self, product_vals, chatter_msg):
"""Create / Update a product."""
product = product_vals.pop("recordset", None)
if product:
product.write(product_vals)
logger.info("Product %d updated", product.id)
product.update(product_vals)
logger.debug("Product %s updated", product.default_code)

Check warning on line 204 in product_import/wizard/product_import.py

View check run for this annotation

Codecov / codecov/patch

product_import/wizard/product_import.py#L203-L204

Added lines #L203 - L204 were not covered by tests
else:
product_active = product_vals.pop("active")
product = self.env["product.product"].create(product_vals)
Expand All @@ -213,33 +210,61 @@
# 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"],
def _create_update_products(self, products, seller_id, company_id, chatter_msg):
"""Create / Update all products."""
seller = self.env["res.partner"].browse(seller_id)

for parsed_product in products:
product_vals = self._prepare_product(
parsed_product,
seller=seller,
company_id=company_id,
chatter_msg=chatter_msg,
)
if record:
products |= record
self._bdimport.post_create_or_update(catalogue, seller, doc_filename=filename)
logger.info("Products updated for vendor %d", seller.id)
return products
if product_vals:
product = self._save_product(product_vals, chatter_msg=chatter_msg)
chatter_msg.append(
f"Product created/updated {product.default_code} ({product.id})"
)
return True

@api.model
def create_update_products(self, products, seller_id, company_id, chatter_msg):
"""Create / Update a product.

This method can be overriden, for example to import asynchronously with queue_job.
"""
return self._create_update_products(
products, seller_id, company_id, chatter_msg=chatter_msg
)

def import_button(self):
self.ensure_one()
file_content = b64decode(self.product_file)
# 1st step: Parse the (UBL) document --> get a "catalogue" dictionary
catalogue = self.parse_product_catalogue(file_content, self.product_filename)
if not catalogue.get("products"):
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(
catalogue, seller, filename=self.product_filename
# 2nd step: Prepare values and create the "product.product" records in Odoo
self.create_update_products(
catalogue["products"],
seller.id,
company_id,
chatter_msg=catalogue["chatter_msg"],
)
# Save imported file as attachment
self._bdimport.post_create_or_update(
catalogue, seller, doc_filename=self.product_filename
)
logger.info(
"Update for vendor %s: %d products", seller.name, len(catalogue["products"])
)

return {"type": "ir.actions.act_window_close"}