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][FIX] shopinvader_search_engine: Fix categories into products #1480

Merged
Merged
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: 3 additions & 1 deletion shopinvader_product/schemas/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ def from_product_product(cls, odoo_rec):
variant_count=odoo_rec.product_variant_count,
categories=[
ShortProductCategory.from_product_category(shopinvader_category)
for shopinvader_category in odoo_rec.shopinvader_categ_ids
for shopinvader_category in odoo_rec.shopinvader_categ_ids.sorted(
"level"
)
],
sku=odoo_rec.default_code or None,
variant_attributes=odoo_rec.variant_attributes,
Expand Down
8 changes: 6 additions & 2 deletions shopinvader_search_engine/models/product_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@ class ProductCategory(models.Model):
@api.depends("parent_id", "parent_id.se_binding_ids")
def _compute_parent_category(self):
for record in self:
record.shopinvader_parent_id = record.parent_id._filter_by_index()
record.shopinvader_parent_id = (
record.parent_id._filter_by_bound_in_same_lang()
)

@api.depends_context("index_id")
@api.depends("child_id", "child_id.se_binding_ids")
def _compute_child_category(self):
for record in self:
record.shopinvader_child_ids = record.child_id._filter_by_index()
record.shopinvader_child_ids = (
record.child_id._filter_by_bound_in_same_lang()
)

def _get_parent(self):
self.ensure_one()
Expand Down
4 changes: 2 additions & 2 deletions shopinvader_search_engine/models/product_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ class ProductTemplate(models.Model):
def _get_categories(self):
self.ensure_one()
categories = super()._get_categories()
categories = categories._filter_by_index()
categories = categories._filter_by_bound_in_same_lang()
return categories

@api.model
def _get_parent_categories(self, categ_ids):
categories = super()._get_parent_categories(categ_ids)
categories = categories._filter_by_index()
categories = categories._filter_by_bound_in_same_lang()
return categories

@api.depends_context("index_id")
Expand Down
22 changes: 22 additions & 0 deletions shopinvader_search_engine/models/se_indexable_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,25 @@ def _filter_by_index(self):
in rec.se_binding_ids.mapped("index_id").ids
)
return records

def _filter_by_bound_in_same_lang(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please explain why, while working on a particular index, you would need to return records linked to another index (with the same backend and lang) ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when you serialize a product, you also serialize categories linked to the product. In this case, you're working with the product's index not the one from the category. If you add tests to check that the categories field is filled into the product information, you'll see that before this change it was always empty.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was a test, but it was wrong. I've corrected it (+ added one for the lang case).

I have also opened OCA/search-engine#177 to avoid such mistakes in the future.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lmignon ping

"""
This methods will fiter the records to only keep the ones that are bound
to the same lang as the current one for the current index backend.
:return: recordset
"""
records = self
index_id = self.env.context.get("index_id", False)
if index_id:
index = self.env["se.index"].browse(index_id)
lang_id = index.lang_id
all_index_same_lang = index.backend_id.index_ids.filtered(
lambda idx, lang=lang_id: idx.lang_id == lang
)
valid_indexes = set(all_index_same_lang.ids)
records = records.filtered(
lambda rec, valid_indexes=valid_indexes: valid_indexes.intersection(
set(rec.se_binding_ids.mapped("index_id").ids)
)
)
return records
12 changes: 8 additions & 4 deletions shopinvader_search_engine/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ def _prepare_index_values(cls, backend=None):
def setup_records(cls, backend=None):
backend = backend or cls.backend
# create an index for category model
cls.se_index = cls.se_index_model.create(cls._prepare_index_values(backend))
cls.se_categ_index = cls.se_index_model.create(
cls._prepare_index_values(backend)
)
# create a binding + category alltogether
cls.category = cls.env["product.category"].create({"name": "Test category"})
cls.category_binding = cls.category._add_to_index(cls.se_index)
cls.category_binding = cls.category._add_to_index(cls.se_categ_index)


class TestProductBindingMixin:
Expand All @@ -81,14 +83,16 @@ def _prepare_index_values(cls, tst_cls, backend=None):
def setup_records(cls, tst_cls, backend=None):
backend = backend or tst_cls.backend
# create an index for product model
tst_cls.se_index = tst_cls.env["se.index"].create(
tst_cls.se_product_index = tst_cls.env["se.index"].create(
cls._prepare_index_values(tst_cls, backend)
)
# create a binding + product alltogether
tst_cls.product = tst_cls.env.ref(
"shopinvader_product.product_product_chair_vortex_white"
)
tst_cls.product_binding = tst_cls.product._add_to_index(tst_cls.se_index)
tst_cls.product_binding = tst_cls.product._add_to_index(
tst_cls.se_product_index
)
tst_cls.product_expected = {
"id": tst_cls.product.id,
"name": tst_cls.product.name,
Expand Down
2 changes: 1 addition & 1 deletion shopinvader_search_engine/tests/test_category_binding.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_serialize_hierarchy_parent_not_in_index(self):
def test_serialize_hierarchy_parent_in_index(self):
parent_category = self._create_parent_category()
self.category.invalidate_model()
parent_binding = parent_category._add_to_index(self.se_index)
parent_binding = parent_category._add_to_index(self.se_categ_index)
self.category_binding.recompute_json()
parent_data = self.category_binding.data["parent"]
self.assertEqual(parent_data["id"], parent_category.id)
Expand Down
48 changes: 29 additions & 19 deletions shopinvader_search_engine/tests/test_product_binding.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright 2023 ACSONE SA/NV
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from .common import TestProductBindingBase
from .common import TestCategoryBindingBase, TestProductBindingBase


class TestProductBinding(TestProductBindingBase):
Expand All @@ -17,35 +17,45 @@ def test_serialize(self):
self.assertEqual(data["variant_attributes"], {"color": "blue"})
self.assertEqual(data["price"], {})

def test_serialize_categories_not_in_index(self):
self.product_binding.recompute_json()
self.assertFalse(self.product_binding.data["categories"])

def test_serialize_categories_in_index(self):
self.product.categ_id._add_to_index(self.se_index)
self.product_binding.recompute_json()
self.assertEqual(len(self.product_binding.data["categories"]), 1)
category_data = self.product_binding.data["categories"][0]
self.assertEqual(category_data["id"], self.product.categ_id.id)
self.assertEqual(category_data["name"], self.product.categ_id.name)
self.assertEqual(category_data["level"], 0)

def test_main_variant_index(self):
variants = self.product.product_tmpl_id.product_variant_ids
for variant in variants:
variant._add_to_index(self.se_index)
main_variant = variants.with_context(index_id=self.se_index.id).filtered("main")
variant._add_to_index(self.se_product_index)
main_variant = variants.with_context(
index_id=self.se_product_index.id
).filtered("main")
self.assertEqual(len(main_variant), 1)
main_variant_binding = main_variant.se_binding_ids[0]
main_variant_binding.recompute_json()
self.assertTrue(main_variant_binding.data["main"])

main_variant.se_binding_ids.unlink()
main_variant2 = variants.with_context(index_id=self.se_index.id).filtered(
"main"
)
main_variant2 = variants.with_context(
index_id=self.se_product_index.id
).filtered("main")
self.assertEqual(len(main_variant2), 1)
self.assertNotEqual(main_variant, main_variant2)
main_variant2_binding = main_variant2.se_binding_ids[0]
main_variant2_binding.recompute_json()
self.assertTrue(main_variant2_binding.data["main"])


class TestProductCategBinding(TestProductBindingBase, TestCategoryBindingBase):
def test_serialize_categories_not_in_index(self):
self.product_binding.recompute_json()
self.assertFalse(self.product_binding.data["categories"])

def test_serialize_categories_in_index(self):
self.product.categ_id._add_to_index(self.se_categ_index)
self.product_binding.recompute_json()
self.assertEqual(len(self.product_binding.data["categories"]), 1)
category_data = self.product_binding.data["categories"][0]
self.assertEqual(category_data["id"], self.product.categ_id.id)
self.assertEqual(category_data["name"], self.product.categ_id.name)
self.assertEqual(category_data["level"], 0)

def test_serialize_categories_in_index_other_lang(self):
self.se_categ_index.lang_id = self.env.ref("base.lang_fr").id
self.product.categ_id._add_to_index(self.se_categ_index)
self.product_binding.recompute_json()
self.assertFalse(self.product_binding.data["categories"])
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class TestProductBinding(BaseTestProductBinding):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.product = cls.product.with_context(index_id=cls.se_index.id)
cls.product = cls.product.with_context(index_id=cls.se_product_index.id)

def test_suffix_empty(self):
"""
Expand Down