Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[16.0][ADD]website_rewrite_absolute_url: Module to add absolute url redirects #994

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions setup/website_rewrite_absolute_url/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)
Empty file.
3 changes: 3 additions & 0 deletions website_rewrite_absolute_url/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from . import models
14 changes: 14 additions & 0 deletions website_rewrite_absolute_url/__manifest__.py
Original file line number Diff line number Diff line change
@@ -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,
}
1 change: 1 addition & 0 deletions website_rewrite_absolute_url/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import website_rewrite
50 changes: 50 additions & 0 deletions website_rewrite_absolute_url/models/website_rewrite.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import re

import werkzeug

from odoo import _, api, fields, models
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.'))

Check warning on line 19 in website_rewrite_absolute_url/models/website_rewrite.py

View check run for this annotation

Codecov / codecov/patch

website_rewrite_absolute_url/models/website_rewrite.py#L19

Added line #L19 was not covered by tests
elif not rewrite.absolute_url and not rewrite.url_to.startswith("/"):
raise ValidationError(

Check warning on line 21 in website_rewrite_absolute_url/models/website_rewrite.py

View check run for this annotation

Codecov / codecov/patch

website_rewrite_absolute_url/models/website_rewrite.py#L21

Added line #L21 was not covered by tests
_('"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(

Check warning on line 26 in website_rewrite_absolute_url/models/website_rewrite.py

View check run for this annotation

Codecov / codecov/patch

website_rewrite_absolute_url/models/website_rewrite.py#L26

Added line #L26 was not covered by tests
_('"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(

Check warning on line 32 in website_rewrite_absolute_url/models/website_rewrite.py

View check run for this annotation

Codecov / codecov/patch

website_rewrite_absolute_url/models/website_rewrite.py#L32

Added line #L32 was not covered by tests
_(
""""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(

Check warning on line 41 in website_rewrite_absolute_url/models/website_rewrite.py

View check run for this annotation

Codecov / codecov/patch

website_rewrite_absolute_url/models/website_rewrite.py#L39-L41

Added lines #L39 - L41 were not covered by tests
strict_slashes=False, converters=converters
)
if not rewrite.absolute_url:
rule = werkzeug.routing.Rule(rewrite.url_to)
routing_map.add(rule)

Check warning on line 46 in website_rewrite_absolute_url/models/website_rewrite.py

View check run for this annotation

Codecov / codecov/patch

website_rewrite_absolute_url/models/website_rewrite.py#L45-L46

Added lines #L45 - L46 were not covered by tests
else:
routing_map.bind(rewrite.url_to)
except ValueError as e:
raise ValidationError(_('"URL to" is invalid: %s') % e) from e

Check warning on line 50 in website_rewrite_absolute_url/models/website_rewrite.py

View check run for this annotation

Codecov / codecov/patch

website_rewrite_absolute_url/models/website_rewrite.py#L48-L50

Added lines #L48 - L50 were not covered by tests
1 change: 1 addition & 0 deletions website_rewrite_absolute_url/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Nextev <https://www.nextev.it>
1 change: 1 addition & 0 deletions website_rewrite_absolute_url/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This module allows to add absolute url redirects.
3 changes: 3 additions & 0 deletions website_rewrite_absolute_url/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -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"
14 changes: 14 additions & 0 deletions website_rewrite_absolute_url/views/website_rewrite.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record id="rewrite_absolute_url" model="ir.ui.view">
<field name="name">rewrite.absolute.url</field>
<field name="model">website.rewrite</field>
<field name="inherit_id" ref="website.view_website_rewrite_form" />
<field name="arch" type="xml">
<field name="url_to" position="after">
<field name="absolute_url" />
</field>
</field>
</record>

</odoo>
Loading