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

[16.0][IMP] connector_importer_product: recordhandler create tmpl xid #142

Draft
wants to merge 3 commits into
base: 16.0
Choose a base branch
from
Draft
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
30 changes: 18 additions & 12 deletions connector_importer/components/odoorecord.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,20 +157,26 @@ def odoo_create(self, values, orig_values):
self.update_translations(odoo_record, translatable)
# Set the external ID if necessary
if self.must_generate_xmlid:
xid = self._get_xmlid(values, orig_values)
if not self.env.ref(xid, raise_if_not_found=False):
module, id_ = xid.split(".", 1)
self.env["ir.model.data"].create(
{
"name": id_,
"module": module,
"model": odoo_record._name,
"res_id": odoo_record.id,
"noupdate": False,
}
)
self.odoo_create_xmlid(odoo_record, values, orig_values)
return odoo_record

def odoo_create_xmlid(self, odoo_record, values, orig_values):
xid = self._get_xmlid(values, orig_values)
return self._create_xmlid(odoo_record, xid)

def _create_xmlid(self, odoo_record, xid):
if not self.env.ref(xid, raise_if_not_found=False):
module, id_ = xid.split(".", 1)
return self.env["ir.model.data"].create(
{
"name": id_,
"module": module,
"model": odoo_record._name,
"res_id": odoo_record.id,
"noupdate": False,
}
)

def odoo_pre_write(self, odoo_record, values, orig_values):
"""Do some extra stuff before updating an existing object."""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,12 @@ def create_attribute_value_if_missing(self):
return self.work.options.record_handler.get(
"create_attribute_value_if_missing", False
)

def odoo_create_xmlid(self, odoo_record, values, orig_values):
# auto-generate xid for the template too if needed
xmlid_rec = super().odoo_create_xmlid(odoo_record, values, orig_values)
if xmlid_rec:
# new xid for the variant created -> get a xid for the template too
xid = self._get_xmlid(values, orig_values)
tmpl_xid = f"{xid}_product_template"
return self._create_xmlid(odoo_record.product_tmpl_id, tmpl_xid)
8 changes: 8 additions & 0 deletions connector_importer_product/tests/test_record_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,11 @@ def test_find_or_create_attr_value_create_missing(self):
"__setup__.product_attr_Size_value_XL", raise_if_not_found=False
)
)

def test_odoo_create_xmlid_if_missing(self):
handler = self._get_handler()
xid = "product.product_variant_test_1"
self.assertFalse(self.env.ref(xid, raise_if_not_found=False))
handler.unique_key = "xid"
handler.odoo_create_xmlid(self.prod, {"xid": xid}, {"xid": xid})
self.assertTrue(self.env.ref(xid, raise_if_not_found=False))
Loading