Skip to content

Commit

Permalink
[10.0][IMP] product_template_multi_link: Makes wizard bi-directional
Browse files Browse the repository at this point in the history
  • Loading branch information
rousseldenis committed Oct 27, 2020
1 parent 740516d commit 31623bb
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 67 deletions.
1 change: 1 addition & 0 deletions product_template_multi_link/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"views/product_template_view.xml",
"views/product_template_link_view.xml",
"views/menu.xml",
"wizards/product_template_linker.xml",
],
'demo': [
"data/product_template_link_type.xml",
Expand Down
1 change: 1 addition & 0 deletions product_template_multi_link/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from . import test_product_template_link_type
from . import test_product_template_link
from . import test_product_template_linker
94 changes: 42 additions & 52 deletions product_template_multi_link/tests/test_product_template_linker.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ class TestProductTemplateLinker(SavepointCase):
def setUpClass(cls):
super(TestProductTemplateLinker, cls).setUpClass()
cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True))
cls.cross_sell = cls.env.ref(
"product_template_multi_link.product_template_link_type_cross_selling")
cls.up_sell = cls.env.ref(
"product_template_multi_link.product_template_link_type_up_selling")
cls.wizard_obj = cls.env['product.template.linker']
cls.product_link_obj = cls.env['product.template.link']
cls.product1 = cls.env.ref(
Expand All @@ -24,107 +28,93 @@ def setUpClass(cls):
cls.products = cls.product1 | cls.product2 | cls.product3
cls.products.mapped("product_template_link_ids").unlink()

def _launch_wizard(self, products, operation_type, link_type=False):
def _launch_wizard(self, products, operation_type, link_type=None):
"""
:param products: product.template recordset
:return: product.template.linker recordset
"""
values = {
"operation_type": operation_type,
"link_type": link_type,
"type_id": link_type.id if link_type else False,
"product_ids": [(6, False, products.ids)],
}
return self.wizard_obj.create(values)

def test_wizard_link_cross_sell(self):
link_type = "cross_sell"
wizard = self._launch_wizard(
self.products, operation_type="link", link_type=link_type)
links = wizard.action_apply_link()
def _test_link_created(self, links, link_type):
for link in links:
source_product = link.product_template_id
source_product = link.left_product_tmpl_id
linked_products = source_product.product_template_link_ids
full_links = (
linked_products.mapped("left_product_tmpl_id") |
linked_products.mapped("right_product_tmpl_id")) - \
source_product
expected_products_linked = self.products - source_product
self.assertEquals(
set(expected_products_linked.ids),
set(linked_products.mapped("linked_product_template_id").ids)
set(full_links.ids)
)
self.assertEquals(link_type, link.link_type)
self.assertEquals(link_type, link.type_id)

def test_wizard_link_cross_sell(self):
link_type = self.cross_sell
wizard = self._launch_wizard(
self.products, operation_type="link", link_type=link_type)
links = wizard.action_apply_link()
self._test_link_created(links, link_type)

def test_wizard_link_up_sell(self):
link_type = "up_sell"
link_type = self.up_sell
wizard = self._launch_wizard(
self.products, operation_type="link", link_type=link_type)
links = wizard.action_apply_link()
for link in links:
source_product = link.product_template_id
linked_products = source_product.product_template_link_ids
expected_products_linked = self.products - source_product
self.assertEquals(
set(expected_products_linked.ids),
set(linked_products.mapped("linked_product_template_id").ids)
)
self.assertEquals(link_type, link.link_type)
self._test_link_created(links, link_type)

def test_wizard_link_duplicate1(self):
link_type = "up_sell"
link_type = self.up_sell
wizard = self._launch_wizard(
self.products, operation_type="link", link_type=link_type)
self.product_link_obj.create({
"product_template_id": self.product1.id,
"linked_product_template_id": self.product2.id,
"link_type": link_type,
"left_product_tmpl_id": self.product1.id,
"right_product_tmpl_id": self.product2.id,
"type_id": link_type.id,
})
links = wizard.action_apply_link()
for link in links:
source_product = link.product_template_id
linked_products = source_product.product_template_link_ids
expected_products_linked = self.products - source_product
self.assertEquals(
set(expected_products_linked.ids),
set(linked_products.mapped("linked_product_template_id").ids))
self.assertEquals(link_type, link.link_type)
self._test_link_created(links, link_type)
# Ensure no duplicates
link = self.product1.product_template_link_ids.filtered(
lambda l: l.linked_product_template_id == self.product2)
lambda l: l.right_product_tmpl_id == self.product2)
self.assertEquals(1, len(link))

def test_wizard_link_duplicate2(self):
link_type = "cross_sell"
link_type = self.cross_sell
wizard = self._launch_wizard(
self.products, operation_type="link", link_type=link_type)
self.product_link_obj.create({
"product_template_id": self.product1.id,
"linked_product_template_id": self.product2.id,
"link_type": "up_sell",
"left_product_tmpl_id": self.product1.id,
"right_product_tmpl_id": self.product2.id,
"type_id": self.up_sell.id,
})
links = wizard.action_apply_link()
for link in links:
source_product = link.product_template_id
linked_products = source_product.product_template_link_ids
expected_products_linked = self.products - source_product
self.assertEquals(
set(expected_products_linked.ids),
set(linked_products.mapped("linked_product_template_id").ids))
self.assertEquals(link_type, link.link_type)
self._test_link_created(links, link_type)
# Ensure no duplicates
link = self.product1.product_template_link_ids.filtered(
lambda l: l.linked_product_template_id == self.product2)
lambda l: l.right_product_tmpl_id == self.product2 or
l.left_product_tmpl_id == self.product2)
# 2 because we have up_sell and cross_sell
self.assertEquals(2, len(link))

def test_wizard_unlink(self):
wizard = self._launch_wizard(self.products, operation_type="unlink")
self.product_link_obj.create({
"product_template_id": self.product1.id,
"linked_product_template_id": self.product2.id,
"link_type": "up_sell",
"left_product_tmpl_id": self.product1.id,
"right_product_tmpl_id": self.product2.id,
"type_id": self.up_sell.id,
})
self.product_link_obj.create({
"product_template_id": self.product1.id,
"linked_product_template_id": self.product3.id,
"link_type": "cross_sell",
"left_product_tmpl_id": self.product1.id,
"right_product_tmpl_id": self.product3.id,
"type_id": self.cross_sell.id,
})
wizard.action_apply_unlink()
self.assertFalse(self.product1.product_template_link_ids)
24 changes: 10 additions & 14 deletions product_template_multi_link/wizards/product_template_linker.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,10 @@ class ProductTemplateLinker(models.TransientModel):
comodel_name="product.template",
string="Products",
)
link_type = fields.Selection(
string='Link Type',
selection=lambda self: self.env[
'product.template.link']._selection_link_type(),
default='cross_sell',
help="* Cross-Sell: suggest your customer to purchase an "
"additional product;\n"
"* Up-Sell: suggest your customer to purchase a "
"higher-end product, an upgrade, etc.")
type_id = fields.Many2one(
comodel_name='product.template.link.type',
ondelete='cascade',
)

@api.model
def default_get(self, fields_list):
Expand Down Expand Up @@ -81,9 +76,10 @@ def action_apply_link(self):
links = self.env['product.template.link'].browse()
for product in self.product_ids:
existing_links = product.product_template_link_ids.filtered(
lambda l: l.link_type == self.link_type)
lambda l: l.type_id == self.type_id)
linked_products = existing_links.mapped(
"linked_product_template_id")
"left_product_tmpl_id") | existing_links.mapped(
"right_product_tmpl_id")
products_to_link = self.product_ids - linked_products - product
links |= self._create_link(product, products_to_link)
return links
Expand All @@ -101,9 +97,9 @@ def _create_link(self, product_source, target_products):
product_links = prod_link_obj.browse()
for target_product in target_products:
values = {
"product_template_id": product_source.id,
"linked_product_template_id": target_product.id,
"link_type": self.link_type,
"left_product_tmpl_id": product_source.id,
"right_product_tmpl_id": target_product.id,
"type_id": self.type_id.id,
}
product_links |= prod_link_obj.create(values)
return product_links
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<group name="main_group">
<group name="main_data_group">
<field name="operation_type"/>
<field name="link_type" attrs="{'invisible': [('operation_type', '!=', 'link')], 'required': [('operation_type', '=', 'link')]}"/>
<field name="type_id" attrs="{'invisible': [('operation_type', '!=', 'link')], 'required': [('operation_type', '=', 'link')]}"/>
</group>
</group>
<group colspan="2" name="product_group">
Expand Down

0 comments on commit 31623bb

Please sign in to comment.