From 06721e7d657336aa6874173bd94fff2c60d82e52 Mon Sep 17 00:00:00 2001 From: Quentin Groulard Date: Thu, 26 Oct 2023 11:59:44 +0200 Subject: [PATCH] [FIX] Get 'index_id' from context --- .../models/product_product.py | 14 ++++++++------ .../tests/test_product.py | 4 ++-- .../models/product_product.py | 5 +++-- .../tests/test_product.py | 2 +- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/shopinvader_search_engine_product_stock/models/product_product.py b/shopinvader_search_engine_product_stock/models/product_product.py index f36c8d4949..37a071667e 100644 --- a/shopinvader_search_engine_product_stock/models/product_product.py +++ b/shopinvader_search_engine_product_stock/models/product_product.py @@ -15,17 +15,19 @@ class ProductProduct(models.Model): def _prepare_stock_data(self): self.ensure_one() - index = self._context.get("index", False) - if index: + index_id = self._context.get("index_id", False) + if index_id: + index = self.env["se.index"].browse(index_id) stock_field = index.product_stock_field_id.name return {"qty": self[stock_field]} return {"qty": None} - @api.depends_context("index") + @api.depends_context("index_id") def _compute_stock_data(self): result = defaultdict(dict) - index = self._context.get("index", False) - if index: + index_id = self.env.context.get("index_id", False) + if index_id: + index = self.env["se.index"].browse(index_id) loc_records = self._filter_by_index() for (wh_key, wh_ids) in index._get_warehouse_list_for_export().items(): for loc_record in loc_records.with_context(warehouse=wh_ids): @@ -53,7 +55,7 @@ def synchronize_all_binding_stock_level(self, company_id=None): all_bindinds = products.mapped("se_binding_ids") indexes = all_bindinds.mapped("index_id") for index in indexes: - for product in products.with_context(index=index)._filter_by_index(): + for product in products.with_context(index_id=index.id)._filter_by_index(): binding = product.sudo().se_binding_ids.filtered( lambda b, i=index: b.index_id == i ) diff --git a/shopinvader_search_engine_product_stock/tests/test_product.py b/shopinvader_search_engine_product_stock/tests/test_product.py index f4179eae0c..eefe37ebdd 100644 --- a/shopinvader_search_engine_product_stock/tests/test_product.py +++ b/shopinvader_search_engine_product_stock/tests/test_product.py @@ -31,7 +31,7 @@ def test_update_qty_from_wizard(self): self.assertEqual(job.count_created(), 1) def _test_update_stock(self, sync_immediatly=True): - self.product_binding.with_context(index=self.index).recompute_json() + self.product_binding.recompute_json() self.assertEqual(self.product_binding.data["stock"], {"global": {"qty": 0.0}}) jobs = self.job_counter() @@ -68,7 +68,7 @@ def test_multi_warehouse(self): warehouses = self.warehouse_1 + self.warehouse_2 self.index.write({"warehouse_ids": [(6, 0, warehouses.ids)]}) - self.product_binding.with_context(index=self.index).recompute_json() + self.product_binding.recompute_json() expected = self._expectect_qty_by_wh(warehouses, self.product) self.assertEqual(self.product_binding.data["stock"], expected) diff --git a/shopinvader_search_engine_product_stock_state/models/product_product.py b/shopinvader_search_engine_product_stock_state/models/product_product.py index 0bba69b9f7..90898cb910 100644 --- a/shopinvader_search_engine_product_stock_state/models/product_product.py +++ b/shopinvader_search_engine_product_stock_state/models/product_product.py @@ -13,8 +13,9 @@ class ProductProduct(models.Model): def _prepare_stock_data(self): self.ensure_one() res = super()._prepare_stock_data() - index = self._context.get("index", False) - if index: + index_id = self.env.context.get("index_id", False) + if index_id: + index = self.env["se.index"].browse(index_id) if "state" in index.stock_level_config: res["state"] = self.stock_state if self._skip_stock_qty_update(index): diff --git a/shopinvader_search_engine_product_stock_state/tests/test_product.py b/shopinvader_search_engine_product_stock_state/tests/test_product.py index 1903cbe751..320a9dd72c 100644 --- a/shopinvader_search_engine_product_stock_state/tests/test_product.py +++ b/shopinvader_search_engine_product_stock_state/tests/test_product.py @@ -17,7 +17,7 @@ def setUpClass(cls): super(TestProductProduct, cls).setUpClass() cls.company = cls.env.ref("base.main_company") cls.index.stock_level_config = "state_and_low_qty" - cls.product = cls.product.with_context(index=cls.index) + cls.product = cls.product.with_context(index_id=cls.index.id) def test_out_of_stock(self): self.assertEqual(