Skip to content

Commit

Permalink
Merge PR #682 into 10.0
Browse files Browse the repository at this point in the history
Signed-off-by sebastienbeau
  • Loading branch information
shopinvader-git-bot committed Mar 26, 2021
2 parents dda332b + 5008be8 commit 4e10054
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 0 deletions.
1 change: 1 addition & 0 deletions shopinvader/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"wizards/shopinvader_category_binding_wizard.xml",
"wizards/shopinvader_category_unbinding_wizard.xml",
"wizards/shopinvader_partner_binding.xml",
"wizards/shopinvader_redirection_wizard.xml",
"views/shopinvader_menu.xml",
"views/shopinvader_product_view.xml",
"views/shopinvader_variant_view.xml",
Expand Down
1 change: 1 addition & 0 deletions shopinvader/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
from . import test_invoice
from . import test_shopinvader_partner_binding
from . import test_notification
from . import test_redirection
103 changes: 103 additions & 0 deletions shopinvader/tests/test_redirection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# -*- coding: utf-8 -*-
# Copyright 2020 Akretion (http://www.akretion.com).
# @author Sébastien BEAU <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).


from odoo.exceptions import UserError

from .common import ProductCommonCase


class TestRedirect(ProductCommonCase):
@classmethod
def setUpClass(cls):
super(TestRedirect, cls).setUpClass()
cls.backend.bind_all_category()
cls.backend.bind_all_product()

def test_redirect_category(self):
dest_categ = self.env.ref("product.product_category_1")
categ_2 = self.env.ref("product.product_category_2")
categ_3 = self.env.ref("product.product_category_3")

wizard = (
self.env["shopinvader.redirection.wizard"]
.with_context(
{
"active_model": "product.category",
"active_ids": [categ_2.id, categ_3.id],
}
)
.create({})
)
wizard.dest_categ_id = dest_categ.id
wizard.apply_redirection()

urls = dest_categ.shopinvader_bind_ids.redirect_url_url_ids.mapped(
"url_key"
)

self.assertEqual(set(urls), {"all/internal", "all/saleable/services"})
self.assertEqual(
dest_categ.shopinvader_bind_ids.url_key, "all/saleable"
)

self.assertEqual(len(categ_2.shopinvader_bind_ids.url_url_ids), 0)
self.assertEqual(len(categ_3.shopinvader_bind_ids.url_url_ids), 0)
self.assertFalse(categ_2.shopinvader_bind_ids.active)
self.assertFalse(categ_3.shopinvader_bind_ids.active)

def test_redirect_product(self):
dest_product = self.env.ref(
"product.product_product_1_product_template"
)
product_2 = self.env.ref("product.product_product_2_product_template")
product_3 = self.env.ref("product.product_product_3_product_template")

wizard = (
self.env["shopinvader.redirection.wizard"]
.with_context(
{
"active_model": "product.template",
"active_ids": [product_2.id, product_3.id],
}
)
.create({})
)
wizard.dest_product_id = dest_product.id
wizard.apply_redirection()

urls = dest_product.shopinvader_bind_ids.redirect_url_url_ids.mapped(
"url_key"
)

self.assertEqual(
set(urls), {"computer-sc234-pcsc234", "support-services"}
)
self.assertEqual(
dest_product.shopinvader_bind_ids.url_key, "gap-analysis-service"
)

self.assertEqual(len(product_2.shopinvader_bind_ids.url_url_ids), 0)
self.assertEqual(len(product_3.shopinvader_bind_ids.url_url_ids), 0)
self.assertFalse(product_2.shopinvader_bind_ids.active)
self.assertFalse(product_3.shopinvader_bind_ids.active)

def test_redirect_on_itself(self):
dest_product = self.env.ref(
"product.product_product_1_product_template"
)
wizard = (
self.env["shopinvader.redirection.wizard"]
.with_context(
{
"active_model": "product.template",
"active_ids": [dest_product.id],
}
)
.create({})
)
wizard.dest_product_id = dest_product.id
with self.assertRaises(UserError):
wizard.apply_redirection()
1 change: 1 addition & 0 deletions shopinvader/wizards/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
from . import shopinvader_variant_unbinding_wizard
from . import shopinvader_partner_binding
from . import shopinvader_partner_binding_line
from . import shopinvader_redirection_wizard
66 changes: 66 additions & 0 deletions shopinvader/wizards/shopinvader_redirection_wizard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# -*- coding: utf-8 -*-
# Copyright 2020 Akretion (http://www.akretion.com).
# @author Sébastien BEAU <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import _, fields, models
from odoo.exceptions import UserError


class ShopinvaderRedirection(models.TransientModel):
_name = "shopinvader.redirection.wizard"

dest_product_id = fields.Many2one(
"product.template", "Redirect to Product"
)
dest_categ_id = fields.Many2one("product.category", "Redirect to Category")

def apply_redirection(self):
if self._context.get("active_model") == "product.category":
return self._redirect_category()
elif self._context.get("active_model") == "product.template":
return self._redirect_product()

def _redirect_record_to(self, records, dest_record):
if dest_record in records:
raise UserError(_(u"You can not redirect a record on itself"))
for record in records:
for binding in record.shopinvader_bind_ids:
dest_binding = dest_record.shopinvader_bind_ids.filtered(
lambda s: s.backend_id == binding.backend_id
and s.lang_id == binding.lang_id
)
if not dest_binding:
raise UserError(
_(
u"The destination record do not have binding for "
u"the backend {}"
).format(binding.backend_id.name)
)
binding.url_url_ids.write(
{
"redirect": True,
"model_id": "{},{}".format(
dest_binding._name, dest_binding.id
),
}
)
binding.refresh()
binding.active = False

def _redirect_category(self):
categ_ids = self._context["active_ids"]
products = self.env["product.template"].search(
[("categ_id", "=", categ_ids)]
)
products.write({"categ_id": self.dest_categ_id.id})
categs = self.env["product.category"].browse(categ_ids)
self._redirect_record_to(categs, self.dest_categ_id)
return True

def _redirect_product(self):
self._redirect_record_to(
self.env["product.template"].browse(self._context["active_ids"]),
self.dest_product_id,
)
return True
46 changes: 46 additions & 0 deletions shopinvader/wizards/shopinvader_redirection_wizard.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<odoo>

<record id="shopinvader_redirection_view_form" model="ir.ui.view">
<field name="model">shopinvader.redirection.wizard</field>
<field name="arch" type="xml">
<form string="Redirection Url">
<sheet>
<group>
<field name="dest_categ_id" invisible="context.get('active_model') != 'product.category'"/>
<field name="dest_product_id" invisible="context.get('active_model') != 'product.template'"/>
</group>
</sheet>
<footer>
<button string="Apply Redirection" name="apply_redirection" type="object" class="oe_highlight"/>
or
<button string="Cancel" class="oe_link" special="cancel"/>
</footer>
</form>
</field>
</record>

<record model="ir.actions.act_window" id="act_open_shopinvader_redirection_wizard_view">
<field name="name">Redirect Shopinvader Record</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">shopinvader.redirection.wizard</field>
<field name="view_mode">form</field>
<field name="target">new</field>
<field name="context">{}</field>
</record>

<record id="ir_redirect_product_url" model="ir.values">
<field eval="'client_action_multi'" name="key2"/>
<field eval="'product.template'" name="model"/>
<field name="name">Redirect Product</field>
<field eval="'ir.actions.act_window,%d'%act_open_shopinvader_redirection_wizard_view" name="value"/>
</record>

<record id="ir_redirect_category_url" model="ir.values">
<field eval="'client_action_multi'" name="key2"/>
<field eval="'product.category'" name="model"/>
<field name="name">Redirect Category</field>
<field eval="'ir.actions.act_window,%d'%act_open_shopinvader_redirection_wizard_view" name="value"/>
</record>

</odoo>

0 comments on commit 4e10054

Please sign in to comment.