diff --git a/setup/website_rewrite_absolute_url/odoo/addons/website_rewrite_absolute_url b/setup/website_rewrite_absolute_url/odoo/addons/website_rewrite_absolute_url new file mode 120000 index 0000000000..4596cfb46b --- /dev/null +++ b/setup/website_rewrite_absolute_url/odoo/addons/website_rewrite_absolute_url @@ -0,0 +1 @@ +../../../../website_rewrite_absolute_url \ No newline at end of file diff --git a/setup/website_rewrite_absolute_url/setup.py b/setup/website_rewrite_absolute_url/setup.py new file mode 100644 index 0000000000..28c57bb640 --- /dev/null +++ b/setup/website_rewrite_absolute_url/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +) diff --git a/website_rewrite_absolute_url/__init__.py b/website_rewrite_absolute_url/__init__.py new file mode 100644 index 0000000000..a466ab6017 --- /dev/null +++ b/website_rewrite_absolute_url/__init__.py @@ -0,0 +1,3 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from . import models \ No newline at end of file diff --git a/website_rewrite_absolute_url/__manifest__.py b/website_rewrite_absolute_url/__manifest__.py new file mode 100644 index 0000000000..b3a210a644 --- /dev/null +++ b/website_rewrite_absolute_url/__manifest__.py @@ -0,0 +1,14 @@ +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +{ + 'name': 'Rewrite rules with absolute url', + 'version': '16.0.1.0.0', + 'category': 'base', + 'summary': 'Rewrite rules with absolute url', + 'author': 'Nextev, Odoo Community Association (OCA)', + "website": "https://github.com/OCA/website", + 'license': 'AGPL-3', + 'depends': ['website'], + 'data': ['views/website_rewrite.xml'], + 'installable': True +} diff --git a/website_rewrite_absolute_url/models/__init__.py b/website_rewrite_absolute_url/models/__init__.py new file mode 100644 index 0000000000..f86759e660 --- /dev/null +++ b/website_rewrite_absolute_url/models/__init__.py @@ -0,0 +1 @@ +from . import website_rewrite diff --git a/website_rewrite_absolute_url/models/website_rewrite.py b/website_rewrite_absolute_url/models/website_rewrite.py new file mode 100644 index 0000000000..fef78b68eb --- /dev/null +++ b/website_rewrite_absolute_url/models/website_rewrite.py @@ -0,0 +1,36 @@ +import re +import werkzeug + +from odoo import models, fields, api, _ +from odoo.exceptions import ValidationError + + +class WebsiteRewrite(models.Model): + _inherit = 'website.rewrite' + + absolute_url = fields.Boolean("Is absolute url") + + @api.constrains('url_to', 'url_from', 'redirect_type') + def _check_url_to(self): + for rewrite in self: + if rewrite.redirect_type in ['301', '302', '308']: + if not rewrite.url_to: + raise ValidationError(_('"URL to" can not be empty.')) + elif not rewrite.absolute_url and not rewrite.url_to.startswith('/'): + raise ValidationError(_('"URL to" must start with a leading slash.')) + for param in re.findall('/<.*?>', rewrite.url_from): + if param not in rewrite.url_to: + raise ValidationError(_('"URL to" must contain parameter %s used in "URL from".') % param) + for param in re.findall('/<.*?>', rewrite.url_to): + if param not in rewrite.url_from: + raise ValidationError(_('"URL to" cannot contain parameter %s which is not used in "URL from".') % param) + try: + converters = self.env['ir.http']._get_converters() + routing_map = werkzeug.routing.Map(strict_slashes=False, converters=converters) + if not rewrite.absolute_url: + rule = werkzeug.routing.Rule(rewrite.url_to) + routing_map.add(rule) + else: + routing_map.bind(rewrite.url_to) + except ValueError as e: + raise ValidationError(_('"URL to" is invalid: %s') % e) \ No newline at end of file diff --git a/website_rewrite_absolute_url/readme/CONTRIBUTORS.rst b/website_rewrite_absolute_url/readme/CONTRIBUTORS.rst new file mode 100644 index 0000000000..c045e8cc0b --- /dev/null +++ b/website_rewrite_absolute_url/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* Nextev diff --git a/website_rewrite_absolute_url/readme/DESCRIPTION.rst b/website_rewrite_absolute_url/readme/DESCRIPTION.rst new file mode 100644 index 0000000000..04901afe44 --- /dev/null +++ b/website_rewrite_absolute_url/readme/DESCRIPTION.rst @@ -0,0 +1 @@ +This module allows to add absolute url redirects. \ No newline at end of file diff --git a/website_rewrite_absolute_url/readme/USAGE.rst b/website_rewrite_absolute_url/readme/USAGE.rst new file mode 100644 index 0000000000..c88771b6e4 --- /dev/null +++ b/website_rewrite_absolute_url/readme/USAGE.rst @@ -0,0 +1,3 @@ +Website > Configuration >  Redirects + +To add an absolute url redirect first check "Is absolute url" and then add it on "Url to" \ No newline at end of file diff --git a/website_rewrite_absolute_url/views/website_rewrite.xml b/website_rewrite_absolute_url/views/website_rewrite.xml new file mode 100644 index 0000000000..1d859a1e5b --- /dev/null +++ b/website_rewrite_absolute_url/views/website_rewrite.xml @@ -0,0 +1,14 @@ + + + + rewrite.absolute.url + website.rewrite + + + + + + + + +