From ba61d550d2a7a7fb7d64ade707f69322e8c5aafb Mon Sep 17 00:00:00 2001 From: Quentin Groulard Date: Fri, 8 Sep 2023 12:15:32 +0200 Subject: [PATCH] Get index from record context --- shopinvader_product/schemas/category.py | 16 ++++-------- shopinvader_product/schemas/product.py | 6 ++--- .../schemas/category.py | 4 +-- .../schemas/product.py | 4 +-- shopinvader_product_seo/schemas/category.py | 4 +-- shopinvader_search_engine/models/se_index.py | 4 +-- shopinvader_search_engine/schemas/__init__.py | 1 - shopinvader_search_engine/schemas/category.py | 15 ++++------- shopinvader_search_engine/schemas/product.py | 25 ------------------- .../tools/category_serializer.py | 5 +--- .../tools/product_serializer.py | 5 +--- 11 files changed, 22 insertions(+), 67 deletions(-) delete mode 100644 shopinvader_search_engine/schemas/product.py diff --git a/shopinvader_product/schemas/category.py b/shopinvader_product/schemas/category.py index 15429c9cbc..b32f533cac 100644 --- a/shopinvader_product/schemas/category.py +++ b/shopinvader_product/schemas/category.py @@ -14,7 +14,7 @@ class ShortShopinvaderCategory(StrictExtendableBaseModel): childs: list[ShortShopinvaderCategory] = [] @classmethod - def from_shopinvader_category(cls, odoo_rec, with_hierarchy=False, *args, **kwargs): + 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 ) @@ -22,16 +22,12 @@ def from_shopinvader_category(cls, odoo_rec, with_hierarchy=False, *args, **kwar parent = odoo_rec.parent_id children = odoo_rec.child_id obj.parent = ( - ShortShopinvaderCategory.from_shopinvader_category( - parent, *args, **kwargs - ) + ShortShopinvaderCategory.from_shopinvader_category(parent) if parent else None ) obj.childs = [ - ShortShopinvaderCategory.from_shopinvader_category( - child, *args, **kwargs - ) + ShortShopinvaderCategory.from_shopinvader_category(child) for child in children ] return obj @@ -41,9 +37,7 @@ class ShopinvaderCategory(ShortShopinvaderCategory): sequence: int | None = None @classmethod - def from_shopinvader_category(cls, odoo_rec, *args, **kwargs): - obj = super().from_shopinvader_category( - odoo_rec, with_hierarchy=True, *args, **kwargs - ) + def from_shopinvader_category(cls, odoo_rec): + obj = super().from_shopinvader_category(odoo_rec, with_hierarchy=True) obj.sequence = odoo_rec.sequence or None return obj diff --git a/shopinvader_product/schemas/product.py b/shopinvader_product/schemas/product.py index 32f8e30583..8e77763b23 100644 --- a/shopinvader_product/schemas/product.py +++ b/shopinvader_product/schemas/product.py @@ -36,7 +36,7 @@ class ShopinvaderVariant(StrictExtendableBaseModel): price: dict[str, ShopinvaderProductPriceInfo] = {} @classmethod - def from_shopinvader_variant(cls, odoo_rec, *args, **kwargs): + def from_shopinvader_variant(cls, odoo_rec): return cls.model_construct( id=odoo_rec.record_id, model=ShopinvaderProduct.from_shopinvader_product( @@ -47,9 +47,7 @@ def from_shopinvader_variant(cls, odoo_rec, *args, **kwargs): short_name=odoo_rec.short_name or None, variant_count=odoo_rec.product_variant_count, categories=[ - ShortShopinvaderCategory.from_shopinvader_category( - shopinvader_category, with_hierarchy=False, *args, **kwargs - ) + ShortShopinvaderCategory.from_shopinvader_category(shopinvader_category) for shopinvader_category in odoo_rec.shopinvader_categ_ids ], sku=odoo_rec.default_code or None, diff --git a/shopinvader_product_description/schemas/category.py b/shopinvader_product_description/schemas/category.py index 6fba692867..130d8426a0 100644 --- a/shopinvader_product_description/schemas/category.py +++ b/shopinvader_product_description/schemas/category.py @@ -12,8 +12,8 @@ class ShopinvaderCategory(BaseShopinvaderCategory, extends=True): short_description: str | None = None @classmethod - def from_shopinvader_category(cls, odoo_rec, *args, **kwargs): - obj = super().from_shopinvader_category(odoo_rec, *args, **kwargs) + def from_shopinvader_category(cls, odoo_rec): + obj = super().from_shopinvader_category(odoo_rec) obj.subtitle = odoo_rec.subtitle or None obj.description = odoo_rec.description or None obj.short_description = odoo_rec.short_description or None diff --git a/shopinvader_product_description/schemas/product.py b/shopinvader_product_description/schemas/product.py index 2b3fa99aaf..0a5de480ce 100644 --- a/shopinvader_product_description/schemas/product.py +++ b/shopinvader_product_description/schemas/product.py @@ -11,8 +11,8 @@ class ShopinvaderVariant(BaseShopinvaderVariant, extends=True): description: str | None = None @classmethod - def from_shopinvader_variant(cls, odoo_rec, *args, **kwargs): - obj = super().from_shopinvader_variant(odoo_rec, *args, **kwargs) + def from_shopinvader_variant(cls, odoo_rec): + obj = super().from_shopinvader_variant(odoo_rec) obj.short_description = odoo_rec.description_sale_short or None obj.description = odoo_rec.description_sale_long or None return obj diff --git a/shopinvader_product_seo/schemas/category.py b/shopinvader_product_seo/schemas/category.py index 1e43ea640e..2ddf736625 100644 --- a/shopinvader_product_seo/schemas/category.py +++ b/shopinvader_product_seo/schemas/category.py @@ -12,8 +12,8 @@ class ShopinvaderCategory(BaseShopinvaderCategory, extends=True): meta_description: str | None = None @classmethod - def from_shopinvader_category(cls, odoo_rec, *args, **kwargs): - obj = super().from_shopinvader_category(odoo_rec, *args, **kwargs) + def from_shopinvader_category(cls, odoo_rec): + obj = super().from_shopinvader_category(odoo_rec) obj.seo_title = odoo_rec.seo_title or None obj.meta_keywords = odoo_rec.meta_keywords or None obj.meta_description = odoo_rec.meta_description or None diff --git a/shopinvader_search_engine/models/se_index.py b/shopinvader_search_engine/models/se_index.py index dbd93da0f2..528b5bd44f 100644 --- a/shopinvader_search_engine/models/se_index.py +++ b/shopinvader_search_engine/models/se_index.py @@ -43,8 +43,8 @@ def _check_model(self): def _get_serializer(self): self.ensure_one() if self.serializer_type == "shopinvader_category_exports": - return ProductCategoryShopinvaderSerializer(self) + return ProductCategoryShopinvaderSerializer() elif self.serializer_type == "shopinvader_product_exports": - return ProductProductShopinvaderSerializer(self) + return ProductProductShopinvaderSerializer() else: return super()._get_serializer() diff --git a/shopinvader_search_engine/schemas/__init__.py b/shopinvader_search_engine/schemas/__init__.py index c4b279f562..2ac1648572 100644 --- a/shopinvader_search_engine/schemas/__init__.py +++ b/shopinvader_search_engine/schemas/__init__.py @@ -1,2 +1 @@ from . import category -from . import product diff --git a/shopinvader_search_engine/schemas/category.py b/shopinvader_search_engine/schemas/category.py index e602d908a5..7e0f622970 100644 --- a/shopinvader_search_engine/schemas/category.py +++ b/shopinvader_search_engine/schemas/category.py @@ -8,10 +8,9 @@ class ShortShopinvaderCategory(BaseShortShopinvaderCategory, extends=True): @classmethod - def from_shopinvader_category( - cls, odoo_rec, with_hierarchy=False, index=None, *args, **kwargs - ): - obj = super().from_shopinvader_category(odoo_rec, *args, **kwargs) + 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 @@ -22,16 +21,12 @@ def from_shopinvader_category( in child.se_binding_ids.mapped("index_id") ) obj.parent = ( - ShortShopinvaderCategory.from_shopinvader_category( - parent, *args, **kwargs - ) + ShortShopinvaderCategory.from_shopinvader_category(parent) if parent else None ) obj.childs = [ - ShortShopinvaderCategory.from_shopinvader_category( - child, *args, **kwargs - ) + ShortShopinvaderCategory.from_shopinvader_category(child) for child in children ] return obj diff --git a/shopinvader_search_engine/schemas/product.py b/shopinvader_search_engine/schemas/product.py deleted file mode 100644 index 1c3d1cd8ef..0000000000 --- a/shopinvader_search_engine/schemas/product.py +++ /dev/null @@ -1,25 +0,0 @@ -# Copyright 2023 ACSONE SA/NV -# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). - -from odoo.addons.shopinvader_product.schemas.category import ShortShopinvaderCategory -from odoo.addons.shopinvader_product.schemas.product import ( - ShopinvaderVariant as BaseShopinvaderVariant, -) - - -class ShopinvaderVariant(BaseShopinvaderVariant, extends=True): - @classmethod - def from_shopinvader_variant(cls, odoo_rec, index=None, *args, **kwargs): - obj = super().from_shopinvader_variant(odoo_rec, *args, **kwargs) - obj.main = odoo_rec.with_context(index=index).main - obj.categories = ( - [ - ShortShopinvaderCategory.from_shopinvader_category( - shopinvader_category, with_hierarchy=False, *args, **kwargs - ) - for shopinvader_category in odoo_rec.with_context( - index=index - ).shopinvader_categ_ids - ], - ) - return obj diff --git a/shopinvader_search_engine/tools/category_serializer.py b/shopinvader_search_engine/tools/category_serializer.py index f7a4825285..7ee023f6a8 100644 --- a/shopinvader_search_engine/tools/category_serializer.py +++ b/shopinvader_search_engine/tools/category_serializer.py @@ -5,8 +5,5 @@ class ProductCategoryShopinvaderSerializer: - def __init__(self, index): - self.index = index - def serialize(self, record): - return ShopinvaderCategory.from_shopinvader_category(record, index=self.index) + return ShopinvaderCategory.from_shopinvader_category(record) diff --git a/shopinvader_search_engine/tools/product_serializer.py b/shopinvader_search_engine/tools/product_serializer.py index 5b927cdead..c40d0577ab 100644 --- a/shopinvader_search_engine/tools/product_serializer.py +++ b/shopinvader_search_engine/tools/product_serializer.py @@ -5,8 +5,5 @@ class ProductProductShopinvaderSerializer: - def __init__(self, index): - self.index = index - def serialize(self, record): - return ShopinvaderVariant.from_shopinvader_variant(record, index=self.index) + return ShopinvaderVariant.from_shopinvader_variant(record)