diff --git a/shopinvader_product/models/category.py b/shopinvader_product/models/category.py index 9252e89113..4c6452fcbc 100644 --- a/shopinvader_product/models/category.py +++ b/shopinvader_product/models/category.py @@ -18,6 +18,8 @@ class ProductCategory(models.Model): _rec_name = None name = fields.Char(translate=True) + active = fields.Boolean(default=True) + level = fields.Integer(compute="_compute_level") def name_get(self): def get_names(cat): @@ -78,3 +80,16 @@ def name_search(self, name, args=None, operator="ilike", limit=100): else: categories = self.search(args, limit=limit) return categories.name_get() + + def _get_parent(self): + self.ensure_one() + return self.parent_id + + @api.depends("parent_id", "parent_id.active") + def _compute_level(self): + for record in self: + record.level = 0 + parent = record._get_parent() + while parent and parent.active: + record.level += 1 + parent = parent._get_parent() diff --git a/shopinvader_product/schemas/category.py b/shopinvader_product/schemas/category.py index b32f533cac..1b42cd4aab 100644 --- a/shopinvader_product/schemas/category.py +++ b/shopinvader_product/schemas/category.py @@ -16,7 +16,7 @@ class ShortShopinvaderCategory(StrictExtendableBaseModel): @classmethod def from_shopinvader_category(cls, odoo_rec, with_hierarchy=False): obj = cls.model_construct( - id=odoo_rec.record_id, name=odoo_rec.name, level=odoo_rec.short_description + id=odoo_rec.record_id, name=odoo_rec.name, level=odoo_rec.level ) if with_hierarchy: parent = odoo_rec.parent_id diff --git a/shopinvader_search_engine/models/__init__.py b/shopinvader_search_engine/models/__init__.py index 2a629a78e4..3f9066f927 100644 --- a/shopinvader_search_engine/models/__init__.py +++ b/shopinvader_search_engine/models/__init__.py @@ -2,3 +2,4 @@ from . import product_product from . import product_template from . import se_index +from . import se_indexable_record diff --git a/shopinvader_search_engine/models/product_category.py b/shopinvader_search_engine/models/product_category.py index 1e35083271..3ba20e46ce 100644 --- a/shopinvader_search_engine/models/product_category.py +++ b/shopinvader_search_engine/models/product_category.py @@ -1,9 +1,36 @@ # Copyright 2023 ACSONE SA/NV # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -from odoo import models +from odoo import api, fields, models class ProductCategory(models.Model): _name = "product.category" _inherit = ["product.category", "se.indexable.record"] + + shopinvader_parent_id = fields.Many2one( + "product.category", + "Shopinvader Parent", + compute="_compute_parent_category", + ) + shopinvader_child_ids = fields.Many2many( + "product.category", + "Shopinvader Childs", + compute="_compute_child_category", + ) + + @api.depends_context("index") + @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() + + @api.depends_context("index") + @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() + + def _get_parent(self): + self.ensure_one() + return self.shopinvader_parent_id diff --git a/shopinvader_search_engine/models/product_product.py b/shopinvader_search_engine/models/product_product.py index fc4659d83a..ceed246879 100644 --- a/shopinvader_search_engine/models/product_product.py +++ b/shopinvader_search_engine/models/product_product.py @@ -11,12 +11,7 @@ class ProductProduct(models.Model): @api.model def _get_shopinvader_product_variants(self, product_ids): variants = super()._get_shopinvader_product_variants(product_ids) - index = self._context.get("index", False) - if index: - variants = variants.filtered( - lambda variant, index=index: index - in variant.se_binding_ids.mapped("index_id") - ) + variants = variants._filter_by_index() return variants @api.depends_context("index") diff --git a/shopinvader_search_engine/models/product_template.py b/shopinvader_search_engine/models/product_template.py index d917fb03d8..71569264c4 100644 --- a/shopinvader_search_engine/models/product_template.py +++ b/shopinvader_search_engine/models/product_template.py @@ -7,26 +7,16 @@ class ProductTemplate(models.Model): _inherit = "product.template" - @api.model - def _filter_categories_by_index(self, categories): - index = self._context.get("index", False) - if index: - categories = categories.filtered( - lambda category, index=index: index - in category.se_binding_ids.mapped("index_id") - ) - return categories - def _get_categories(self): self.ensure_one() categories = super()._get_categories() - self._filter_categories_by_index(categories) + categories = categories._filter_by_index() return categories @api.model def _get_parent_categories(self, categ_ids): categories = super()._get_parent_categories(categ_ids) - self._filter_categories_by_index(categories) + categories = categories._filter_by_index() return categories @api.depends_context("index") diff --git a/shopinvader_search_engine/models/se_indexable_record.py b/shopinvader_search_engine/models/se_indexable_record.py new file mode 100644 index 0000000000..9d3a4a213d --- /dev/null +++ b/shopinvader_search_engine/models/se_indexable_record.py @@ -0,0 +1,17 @@ +# Copyright 2023 ACSONE SA/NV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from odoo import models + + +class SeIndexableRecord(models.AbstractModel): + + _inherit = "se.indexable.record" + + def _filter_by_index(self): + index = self._context.get("index", False) + records = self + if index: + records = records.filtered( + lambda rec, index=index: index in rec.se_binding_ids.mapped("index_id") + ) + return records diff --git a/shopinvader_search_engine/schemas/category.py b/shopinvader_search_engine/schemas/category.py index 7e0f622970..861c292abd 100644 --- a/shopinvader_search_engine/schemas/category.py +++ b/shopinvader_search_engine/schemas/category.py @@ -10,16 +10,9 @@ class ShortShopinvaderCategory(BaseShortShopinvaderCategory, extends=True): @classmethod def from_shopinvader_category(cls, odoo_rec, with_hierarchy=False): obj = super().from_shopinvader_category(odoo_rec) - index = odoo_rec._context.get("index", False) - if with_hierarchy and index: - parent = odoo_rec.parent_id.filtered( - lambda parent, index=index: index - in parent.se_binding_ids.mapped("index_id") - ) - children = odoo_rec.child_id.filtered( - lambda child, index=index: index - in child.se_binding_ids.mapped("index_id") - ) + if with_hierarchy: + parent = odoo_rec.shopinvader_parent_id + children = odoo_rec.shopinvader_child_ids obj.parent = ( ShortShopinvaderCategory.from_shopinvader_category(parent) if parent