-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by sebastienbeau
- Loading branch information
Showing
6 changed files
with
218 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |