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 d7869b8
Show file tree
Hide file tree
Showing 16 changed files with 797 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 alexey.pelykh@corphub.eu

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
52 changes: 52 additions & 0 deletions website_page_redirect/models/ir_http.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Copyright 2024 CorporateHub (https://corporatehub.eu)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import logging

from markupsafe import Markup
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()

# In case there was no matching page or the response is not a Markup,
# or it's not a visible page, or it's a redirect page.
logger.error("Response: %s", response)
if (
not response
and not isinstance(response, Markup)
and getattr(response, "status_code", 0) != 200
):
logger.error("BAIL OUT")
return response

# Get the page that was just served
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

logger.error("Page: %s", page)
if not page.is_redirect or page.redirect_method != "http":
logger.error("No redirect")
return response
return http.request.redirect(
page.redirect_url,
code=int(page.redirect_http_code) if page.redirect_http_code else 301,
local=page.redirect_url.lower().startswith("http"),
)
61 changes: 61 additions & 0 deletions website_page_redirect/models/website_page.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# 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", "JavaScript"),
],
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.Text(
compute="_compute_redirect_js_code",
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 != "js":
page.redirect_js_code = ""
continue
page.redirect_js_code = (
"setTimeout("
f" function() {{ window.location.replace('{page.redirect.url}'); }},"
f" {page.redirect_delay * 1000},"
");"
)
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 <alexey.pelykh@corphub.eu>

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 d7869b8

Please sign in to comment.