Skip to content

Commit

Permalink
[REF] Category parent/child and level + se_index_record filter by index
Browse files Browse the repository at this point in the history
  • Loading branch information
qgroulard committed Sep 19, 2023
1 parent ba61d55 commit 3078ced
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 30 deletions.
15 changes: 15 additions & 0 deletions shopinvader_product/models/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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()
2 changes: 1 addition & 1 deletion shopinvader_product/schemas/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions shopinvader_search_engine/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
from . import product_product
from . import product_template
from . import se_index
from . import se_indexable_record
29 changes: 28 additions & 1 deletion shopinvader_search_engine/models/product_category.py
Original file line number Diff line number Diff line change
@@ -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
7 changes: 1 addition & 6 deletions shopinvader_search_engine/models/product_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
14 changes: 2 additions & 12 deletions shopinvader_search_engine/models/product_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
17 changes: 17 additions & 0 deletions shopinvader_search_engine/models/se_indexable_record.py
Original file line number Diff line number Diff line change
@@ -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
13 changes: 3 additions & 10 deletions shopinvader_search_engine/schemas/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3078ced

Please sign in to comment.