diff --git a/shopinvader/__manifest__.py b/shopinvader/__manifest__.py index abbe80d0f9..0c50cd2c27 100644 --- a/shopinvader/__manifest__.py +++ b/shopinvader/__manifest__.py @@ -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", diff --git a/shopinvader/tests/__init__.py b/shopinvader/tests/__init__.py index 041cf6a0bd..7b772865e6 100644 --- a/shopinvader/tests/__init__.py +++ b/shopinvader/tests/__init__.py @@ -17,3 +17,4 @@ from . import test_invoice from . import test_shopinvader_partner_binding from . import test_notification +from . import test_redirection diff --git a/shopinvader/tests/test_redirection.py b/shopinvader/tests/test_redirection.py new file mode 100644 index 0000000000..ee7e22264f --- /dev/null +++ b/shopinvader/tests/test_redirection.py @@ -0,0 +1,103 @@ +# -*- coding: utf-8 -*- +# Copyright 2020 Akretion (http://www.akretion.com). +# @author Sébastien BEAU +# 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() diff --git a/shopinvader/wizards/__init__.py b/shopinvader/wizards/__init__.py index 2b0c2abc8c..ad776863af 100644 --- a/shopinvader/wizards/__init__.py +++ b/shopinvader/wizards/__init__.py @@ -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 diff --git a/shopinvader/wizards/shopinvader_redirection_wizard.py b/shopinvader/wizards/shopinvader_redirection_wizard.py new file mode 100644 index 0000000000..8ec92aed3e --- /dev/null +++ b/shopinvader/wizards/shopinvader_redirection_wizard.py @@ -0,0 +1,66 @@ +# -*- coding: utf-8 -*- +# Copyright 2020 Akretion (http://www.akretion.com). +# @author Sébastien BEAU +# 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 diff --git a/shopinvader/wizards/shopinvader_redirection_wizard.xml b/shopinvader/wizards/shopinvader_redirection_wizard.xml new file mode 100644 index 0000000000..75a06cbbd5 --- /dev/null +++ b/shopinvader/wizards/shopinvader_redirection_wizard.xml @@ -0,0 +1,46 @@ + + + + + shopinvader.redirection.wizard + +
+ + + + + + +
+
+
+
+
+ + + Redirect Shopinvader Record + ir.actions.act_window + shopinvader.redirection.wizard + form + new + {} + + + + + + Redirect Product + + + + + + + Redirect Category + + + +