Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.0][FIX] Fix shopinvader - Variants/Product active/inactive #568

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions shopinvader/models/shopinvader_variant.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

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 @@ -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):
"""
Expand Down