Skip to content

Commit

Permalink
Merge PR #656 into 12.0
Browse files Browse the repository at this point in the history
Signed-off-by rousseldenis
  • Loading branch information
shopinvader-git-bot committed May 29, 2020
2 parents e6eb3aa + 4395cdd commit 4305d8b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
26 changes: 21 additions & 5 deletions shopinvader/models/shopinvader_variant.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,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
Expand All @@ -73,17 +81,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

Expand Down
13 changes: 5 additions & 8 deletions shopinvader/tests/test_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -798,29 +798,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):
"""
Expand Down

0 comments on commit 4305d8b

Please sign in to comment.