Skip to content

Commit

Permalink
[ADD] website_page_redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-pelykh committed Nov 28, 2024
1 parent a74c1d2 commit 8fcb3c5
Show file tree
Hide file tree
Showing 16 changed files with 878 additions and 0 deletions.
1 change: 1 addition & 0 deletions setup/_metapackage/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ dependencies = [
"odoo-addon-website_forum_subscription>=17.0dev,<17.1dev",
"odoo-addon-website_google_tag_manager>=17.0dev,<17.1dev",
"odoo-addon-website_odoo_debranding>=17.0dev,<17.1dev",
"odoo-addon-website_page_redirect>=17.0dev,<17.1dev",
"odoo-addon-website_require_login>=17.0dev,<17.1dev",
]
classifiers=[
Expand Down
98 changes: 98 additions & 0 deletions website_page_redirect/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
=====================
Website Page Redirect
=====================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:3eaca071ad228aab0540926070c564d408d2868cd41e0dc43a59d67aba5e5e68
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fwebsite-lightgray.png?logo=github
:target: https://github.com/OCA/website/tree/17.0/website_page_redirect
:alt: OCA/website
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/website-17-0/website-17-0-website_page_redirect
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/website&target_branch=17.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|

This module allows to configure an redirect for specific website pages.

**Table of contents**

.. contents::
:local:

Configuration
=============

To configure a redirect for the page, you need to:

1. Go to *Website > Site > Pages*
2. Open the page of interest
3. Check the *Redirect* checkbox
4. Configure the *Redirect URL* field
5. Configure the *Redirect Method* and related fields
6. Save the page

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/website/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`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**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
-------

* CorporateHub

Contributors
------------

- ``CorporateHub <https://corporatehub.eu/>``\ \_\_

- Alexey Pelykh [email protected]

Maintainers
-----------

This module is maintained by the OCA.

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

.. |maintainer-alexey-pelykh| image:: https://github.com/alexey-pelykh.png?size=40px
:target: https://github.com/alexey-pelykh
:alt: alexey-pelykh

Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:

|maintainer-alexey-pelykh|

This module is part of the `OCA/website <https://github.com/OCA/website/tree/17.0/website_page_redirect>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
4 changes: 4 additions & 0 deletions website_page_redirect/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright 2024 CorporateHub (https://corporatehub.eu)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import models
21 changes: 21 additions & 0 deletions website_page_redirect/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright 2024 CorporateHub (https://corporatehub.eu)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

{
"name": "Website Page Redirect",
"summary": "Redirect page to another URL",
"category": "Website",
"version": "17.0.1.0.0",
"author": "CorporateHub, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/website",
"license": "AGPL-3",
"depends": [
"website",
],
"data": [
"views/website_layout.xml",
"views/website_page.xml",
],
"installable": True,
"maintainers": ["alexey-pelykh"],
}
5 changes: 5 additions & 0 deletions website_page_redirect/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Copyright 2024 CorporateHub (https://corporatehub.eu)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import ir_http
from . import website_page
40 changes: 40 additions & 0 deletions website_page_redirect/models/ir_http.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright 2024 CorporateHub (https://corporatehub.eu)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import logging

from odoo import http, models

logger = logging.getLogger(__name__)


class IrHttp(models.AbstractModel):
_inherit = "ir.http"

@classmethod
def _serve_page(cls):
response = super()._serve_page()

if not response and getattr(response, "status_code", 0) != 200:
return response

Check warning on line 19 in website_page_redirect/models/ir_http.py

View check run for this annotation

Codecov / codecov/patch

website_page_redirect/models/ir_http.py#L19

Added line #L19 was not covered by tests

page = (
http.request.env["website.page"]
.sudo()
.search(
[("url", "=", http.request.httprequest.path)],
order="website_id asc",
limit=1,
)
)
if not page:
logger.error("Served page found for URL %s", http.request.httprequest.path)
return response

Check warning on line 32 in website_page_redirect/models/ir_http.py

View check run for this annotation

Codecov / codecov/patch

website_page_redirect/models/ir_http.py#L31-L32

Added lines #L31 - L32 were not covered by tests

if not page.is_redirect or page.redirect_method != "http":
return response
return http.request.redirect(
page.redirect_url,
code=int(page.redirect_http_code) if page.redirect_http_code else 301,
local=not page.redirect_url.lower().startswith("http"),
)
67 changes: 67 additions & 0 deletions website_page_redirect/models/website_page.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Copyright 2024 CorporateHub (https://corporatehub.eu)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import api, fields, models


class WebsitePage(models.Model):
_inherit = "website.page"

is_redirect = fields.Boolean(
string="Redirect",
help="If checked, this page will redirect to another URL.",
)
redirect_url = fields.Char(
string="Redirect URL",
help="URL to redirect to when this page is accessed.",
)
redirect_method = fields.Selection(
selection=[
("http", "HTTP"),
("meta", "Meta Refresh"),
("js-href", "JavaScript HREF"),
("js-replace", "JavaScript Replace"),
],
default="http",
help="Method to use for the redirect.",
)
redirect_http_code = fields.Selection(
selection=[
("301", "301 Moved Permanently"),
("302", "302 Found"),
("303", "303 See Other"),
("307", "307 Temporary Redirect"),
("308", "308 Permanent Redirect"),
],
string="Redirect HTTP Code",
default="301",
help="HTTP status code to use for the redirect.",
)
redirect_delay = fields.Integer(
default=0,
help=(
"Delay before redirect (in seconds) for Meta-Refresh and JavaScript"
" redirect methods."
),
)
redirect_js_code = fields.Html(
compute="_compute_redirect_js_code",
sanitize=False,
string="Redirect JavaScript Code",
)

@api.depends("redirect_url", "redirect_delay", "redirect_method")
def _compute_redirect_js_code(self):
for page in self:
if page.redirect_method not in ("js-href", "js-replace"):
page.redirect_js_code = ""
continue

Check warning on line 57 in website_page_redirect/models/website_page.py

View check run for this annotation

Codecov / codecov/patch

website_page_redirect/models/website_page.py#L56-L57

Added lines #L56 - L57 were not covered by tests
if page.redirect_method == "js-href":
function_body = f"window.location.href = '{page.redirect_url}';"
elif page.redirect_method == "js-replace":
function_body = f"window.location.replace('{page.redirect_url}');"
page.redirect_js_code = (
'<script type="text/javascript">setTimeout(\n'
f" function() {{ {function_body} }},\n"
f" {page.redirect_delay * 1000},\n"
");</script>"
)
3 changes: 3 additions & 0 deletions website_page_redirect/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["whool"]
build-backend = "whool.buildapi"
8 changes: 8 additions & 0 deletions website_page_redirect/readme/CONFIGURE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
To configure a redirect for the page, you need to:

1. Go to *Website \> Site \> Pages*
2. Open the page of interest
3. Check the *Redirect* checkbox
4. Configure the *Redirect URL* field
5. Configure the *Redirect Method* and related fields
6. Save the page
4 changes: 4 additions & 0 deletions website_page_redirect/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
* `CorporateHub <https://corporatehub.eu/>`__

* Alexey Pelykh <[email protected]>

1 change: 1 addition & 0 deletions website_page_redirect/readme/DESCRIPTION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This module allows to configure an redirect for specific website pages.
Loading

0 comments on commit 8fcb3c5

Please sign in to comment.