Skip to content

Commit

Permalink
[FIX] Get 'index_id' from context
Browse files Browse the repository at this point in the history
  • Loading branch information
qgroulard committed Oct 26, 2023
1 parent 8fc5862 commit 06721e7
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
)
Expand Down
4 changes: 2 additions & 2 deletions shopinvader_search_engine_product_stock/tests/test_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 06721e7

Please sign in to comment.