diff --git a/shopinvader/models/shopinvader_variant.py b/shopinvader/models/shopinvader_variant.py index de464f9b66..16aa2aaf01 100644 --- a/shopinvader/models/shopinvader_variant.py +++ b/shopinvader/models/shopinvader_variant.py @@ -50,20 +50,28 @@ class ShopinvaderVariant(models.Model): @contextmanager @api.multi - def _action_product_disabled(self): + def _action_product_toggle_active(self): """ Action a deactivation of a variant, if every variants are disabled: disable the product too. + Also when a variant is enabled, the related shopinvader product + should be re-enabled too. :return: """ product_active_dict = { p: p.active for p in self.mapped("shopinvader_product_id") } yield + to_activate_ids = set() + to_inactivate_ids = set() for variant in self: + shopinv_product = variant.shopinvader_product_id if variant.active: + # If the variant is active and the related shop. product is + # not active, we have to active it. + if not shopinv_product.active: + to_activate_ids.add(shopinv_product.id) continue - shopinv_product = variant.shopinvader_product_id # If the product is already disabled, we don't have anything to do! if not product_active_dict.get(shopinv_product, True): continue @@ -72,17 +80,25 @@ def _action_product_disabled(self): if all( [not v.active for v in shopinv_product.shopinvader_variant_ids] ): - shopinv_product.write({"active": False}) + to_inactivate_ids.add(shopinv_product.id) + if to_activate_ids: + self.env["shopinvader.product"].browse(to_activate_ids).write( + {"active": True} + ) + if to_inactivate_ids: + self.env["shopinvader.product"].browse(to_inactivate_ids).write( + {"active": False} + ) @api.multi def write(self, vals): """ Inherit to manage behaviour when the variant is disabled. - We may habe to disable also the shopinvader.product + We may have to disable also the shopinvader.product :param vals: dict :return: bool """ - with self._action_product_disabled(): + with self._action_product_toggle_active(): result = super(ShopinvaderVariant, self).write(vals) return result diff --git a/shopinvader/tests/test_product.py b/shopinvader/tests/test_product.py index e792fd5083..27a724a111 100644 --- a/shopinvader/tests/test_product.py +++ b/shopinvader/tests/test_product.py @@ -808,29 +808,26 @@ def _check_correct_unbind_active(self, variants): """ During the execution of some cases, check the value of shopinvader products depending on related variants. + If every variants are disabled => Shop. product should be disabled + If at least 1 variant is enabled => Shop. product should be enabled :param variants: shopinvader.variant recordset :return: """ variants = variants.with_context(active_test=False) # Save if the shopinvader product is active or not - product_active_dict = { - p: p.active for p in variants.mapped("shopinvader_product_id") - } yield for variant in variants: shopinv_product = variant.shopinvader_product_id - product_active = product_active_dict.get(shopinv_product) all_variants = shopinv_product.shopinvader_variant_ids # If all variants are disabled, the product should be disabled too. - if all([not a.active for a in all_variants]): + variants_disabled = all([not a.active for a in all_variants]) + if variants_disabled: self.assertFalse(shopinv_product.active) self._check_category_after_unbind(shopinv_product) # But if at least 1 is active, the product should stay into his # previous state. - elif product_active: - self.assertTrue(shopinv_product.active) else: - self.assertFalse(shopinv_product.active) + self.assertTrue(shopinv_product.active) def _check_category_after_unbind(self, shopinv_product): """