Skip to content

Commit 8098713

Browse files
committed
[ADD] website_page_redirect
1 parent a74c1d2 commit 8098713

16 files changed

+904
-0
lines changed

setup/_metapackage/pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ dependencies = [
88
"odoo-addon-website_forum_subscription>=17.0dev,<17.1dev",
99
"odoo-addon-website_google_tag_manager>=17.0dev,<17.1dev",
1010
"odoo-addon-website_odoo_debranding>=17.0dev,<17.1dev",
11+
"odoo-addon-website_page_redirect>=17.0dev,<17.1dev",
1112
"odoo-addon-website_require_login>=17.0dev,<17.1dev",
1213
]
1314
classifiers=[

website_page_redirect/README.rst

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
=====================
2+
Website Page Redirect
3+
=====================
4+
5+
..
6+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
7+
!! This file is generated by oca-gen-addon-readme !!
8+
!! changes will be overwritten. !!
9+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
10+
!! source digest: sha256:3eaca071ad228aab0540926070c564d408d2868cd41e0dc43a59d67aba5e5e68
11+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
12+
13+
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
14+
:target: https://odoo-community.org/page/development-status
15+
:alt: Beta
16+
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
17+
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
18+
:alt: License: AGPL-3
19+
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fwebsite-lightgray.png?logo=github
20+
:target: https://github.com/OCA/website/tree/17.0/website_page_redirect
21+
:alt: OCA/website
22+
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
23+
:target: https://translation.odoo-community.org/projects/website-17-0/website-17-0-website_page_redirect
24+
:alt: Translate me on Weblate
25+
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
26+
:target: https://runboat.odoo-community.org/builds?repo=OCA/website&target_branch=17.0
27+
:alt: Try me on Runboat
28+
29+
|badge1| |badge2| |badge3| |badge4| |badge5|
30+
31+
This module allows to configure an redirect for specific website pages.
32+
33+
**Table of contents**
34+
35+
.. contents::
36+
:local:
37+
38+
Configuration
39+
=============
40+
41+
To configure a redirect for the page, you need to:
42+
43+
1. Go to *Website > Site > Pages*
44+
2. Open the page of interest
45+
3. Check the *Redirect* checkbox
46+
4. Configure the *Redirect URL* field
47+
5. Configure the *Redirect Method* and related fields
48+
6. Save the page
49+
50+
Bug Tracker
51+
===========
52+
53+
Bugs are tracked on `GitHub Issues <https://github.com/OCA/website/issues>`_.
54+
In case of trouble, please check there if your issue has already been reported.
55+
If you spotted it first, help us to smash it by providing a detailed and welcomed
56+
`feedback <https://github.com/OCA/website/issues/new?body=module:%20website_page_redirect%0Aversion:%2017.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
57+
58+
Do not contact contributors directly about support or help with technical issues.
59+
60+
Credits
61+
=======
62+
63+
Authors
64+
-------
65+
66+
* CorporateHub
67+
68+
Contributors
69+
------------
70+
71+
- ``CorporateHub <https://corporatehub.eu/>``\ \_\_
72+
73+
- Alexey Pelykh [email protected]
74+
75+
Maintainers
76+
-----------
77+
78+
This module is maintained by the OCA.
79+
80+
.. image:: https://odoo-community.org/logo.png
81+
:alt: Odoo Community Association
82+
:target: https://odoo-community.org
83+
84+
OCA, or the Odoo Community Association, is a nonprofit organization whose
85+
mission is to support the collaborative development of Odoo features and
86+
promote its widespread use.
87+
88+
.. |maintainer-alexey-pelykh| image:: https://github.com/alexey-pelykh.png?size=40px
89+
:target: https://github.com/alexey-pelykh
90+
:alt: alexey-pelykh
91+
92+
Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:
93+
94+
|maintainer-alexey-pelykh|
95+
96+
This module is part of the `OCA/website <https://github.com/OCA/website/tree/17.0/website_page_redirect>`_ project on GitHub.
97+
98+
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

website_page_redirect/__init__.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Copyright 2024 CorporateHub (https://corporatehub.eu)
2+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
3+
4+
from . import models

website_page_redirect/__manifest__.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright 2024 CorporateHub (https://corporatehub.eu)
2+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
3+
4+
{
5+
"name": "Website Page Redirect",
6+
"summary": "Redirect page to another URL",
7+
"category": "Website",
8+
"version": "17.0.1.0.0",
9+
"author": "CorporateHub, Odoo Community Association (OCA)",
10+
"website": "https://github.com/OCA/website",
11+
"license": "AGPL-3",
12+
"depends": [
13+
"website",
14+
],
15+
"data": [
16+
"views/website_layout.xml",
17+
"views/website_page.xml",
18+
],
19+
"installable": True,
20+
"maintainers": ["alexey-pelykh"],
21+
}
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Copyright 2024 CorporateHub (https://corporatehub.eu)
2+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
3+
4+
from . import ir_http
5+
from . import website_page
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Copyright 2024 CorporateHub (https://corporatehub.eu)
2+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
3+
4+
import logging
5+
6+
from odoo import http, models
7+
8+
logger = logging.getLogger(__name__)
9+
10+
11+
class IrHttp(models.AbstractModel):
12+
_inherit = "ir.http"
13+
14+
@classmethod
15+
def _serve_page(cls):
16+
response = super()._serve_page()
17+
18+
if not response and getattr(response, "status_code", 0) != 200:
19+
return response
20+
21+
page = (
22+
http.request.env["website.page"]
23+
.sudo()
24+
.search(
25+
[("url", "=", http.request.httprequest.path)],
26+
order="website_id asc",
27+
limit=1,
28+
)
29+
)
30+
if not page: # pragma: no cover
31+
logger.error("Served page found for URL %s", http.request.httprequest.path)
32+
return response
33+
34+
if not page.is_redirect or page.redirect_method != "http":
35+
return response
36+
return http.request.redirect(
37+
page.redirect_url,
38+
code=int(page.redirect_http_code) if page.redirect_http_code else 301,
39+
local=not page.redirect_url.lower().startswith("http"),
40+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Copyright 2024 CorporateHub (https://corporatehub.eu)
2+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
3+
from odoo import api, fields, models
4+
5+
6+
class WebsitePage(models.Model):
7+
_inherit = "website.page"
8+
9+
is_redirect = fields.Boolean(
10+
string="Redirect",
11+
help="If checked, this page will redirect to another URL.",
12+
)
13+
redirect_url = fields.Char(
14+
string="Redirect URL",
15+
help="URL to redirect to when this page is accessed.",
16+
)
17+
redirect_method = fields.Selection(
18+
selection=[
19+
("http", "HTTP"),
20+
("meta", "Meta Refresh"),
21+
("js-href", "JavaScript HREF"),
22+
("js-replace", "JavaScript Replace"),
23+
],
24+
default="http",
25+
help="Method to use for the redirect.",
26+
)
27+
redirect_http_code = fields.Selection(
28+
selection=[
29+
("301", "301 Moved Permanently"),
30+
("302", "302 Found"),
31+
("303", "303 See Other"),
32+
("307", "307 Temporary Redirect"),
33+
("308", "308 Permanent Redirect"),
34+
],
35+
string="Redirect HTTP Code",
36+
default="301",
37+
help="HTTP status code to use for the redirect.",
38+
)
39+
redirect_delay = fields.Integer(
40+
default=0,
41+
help=(
42+
"Delay before redirect (in seconds) for Meta-Refresh and JavaScript"
43+
" redirect methods."
44+
),
45+
)
46+
redirect_js_code = fields.Html(
47+
compute="_compute_redirect_js_code",
48+
sanitize=False,
49+
string="Redirect JavaScript Code",
50+
)
51+
52+
@api.depends("redirect_url", "redirect_delay", "redirect_method")
53+
def _compute_redirect_js_code(self):
54+
for page in self:
55+
if page.redirect_method not in ("js-href", "js-replace"):
56+
page.redirect_js_code = ""
57+
continue
58+
if page.redirect_method == "js-href":
59+
function_body = f"window.location.href = '{page.redirect_url}';"
60+
elif page.redirect_method == "js-replace":
61+
function_body = f"window.location.replace('{page.redirect_url}');"
62+
page.redirect_js_code = (
63+
'<script type="text/javascript">setTimeout(\n'
64+
f" function() {{ {function_body} }},\n"
65+
f" {page.redirect_delay * 1000},\n"
66+
");</script>"
67+
)

website_page_redirect/pyproject.toml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[build-system]
2+
requires = ["whool"]
3+
build-backend = "whool.buildapi"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
To configure a redirect for the page, you need to:
2+
3+
1. Go to *Website \> Site \> Pages*
4+
2. Open the page of interest
5+
3. Check the *Redirect* checkbox
6+
4. Configure the *Redirect URL* field
7+
5. Configure the *Redirect Method* and related fields
8+
6. Save the page
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
* `CorporateHub <https://corporatehub.eu/>`__
2+
3+
* Alexey Pelykh <[email protected]>
4+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This module allows to configure an redirect for specific website pages.

0 commit comments

Comments
 (0)