Skip to content

Commit

Permalink
[ADD] website_sale_disable_shop module
Browse files Browse the repository at this point in the history
pre-commit fix
  • Loading branch information
ByteMeAsap committed Jul 11, 2024
1 parent a67a7b1 commit aff3d0d
Show file tree
Hide file tree
Showing 13 changed files with 148 additions and 0 deletions.
4 changes: 4 additions & 0 deletions website_sale_disable_shop/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright 2024 Onestein
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl-3.0).

from . import models
18 changes: 18 additions & 0 deletions website_sale_disable_shop/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2024 Onestein
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl-3.0).

{
"name": "Website Sale Enable/Disable Shop",
"category": "Website",
"version": "16.0.1.0.0",
"author": "Onestein, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/website",
"license": "LGPL-3",
"depends": [
"website_sale",
],
"data": [
"views/res_config_settings_view.xml",
],
"installable": True,
}
6 changes: 6 additions & 0 deletions website_sale_disable_shop/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Copyright 2024 Onestein
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl-3.0).

from . import ir_http
from . import res_config_settings
from . import website
37 changes: 37 additions & 0 deletions website_sale_disable_shop/models/ir_http.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright 2024 Onestein
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl-3.0).
from pathlib import Path

from odoo import models
from odoo.http import request


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

@classmethod
def _dispatch(cls, endpoint):
res = cls._check_shop_enabled_disabled()
if res:
return res
return super()._dispatch(endpoint)

@classmethod
def _serve_fallback(cls):
res = cls._check_shop_enabled_disabled()
if res:
return res
return super()._serve_fallback()

@classmethod
def _check_shop_enabled_disabled(cls):
# if not website request - skip
website = request.env["website"].sudo().get_current_website()
if not website:
return None
if not website.shop_enabled:
path = request.httprequest.path
if path == "/shop" or Path("/shop") in Path(path).parents:
if path == "/shop/cart" and request.params.get("type") == "popover":
return None
return request.redirect("/")
10 changes: 10 additions & 0 deletions website_sale_disable_shop/models/res_config_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright 2024 Onestein
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import fields, models


class ResConfigSettings(models.TransientModel):
_inherit = "res.config.settings"

shop_enabled = fields.Boolean(related="website_id.shop_enabled", readonly=False)
10 changes: 10 additions & 0 deletions website_sale_disable_shop/models/website.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright 2024 Onestein
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import fields, models


class Website(models.Model):
_inherit = "website"

shop_enabled = fields.Boolean(default=True)
1 change: 1 addition & 0 deletions website_sale_disable_shop/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Onestein <https://www.onestein.nl>
1 change: 1 addition & 0 deletions website_sale_disable_shop/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This module allows to enable/disable shops per website.
5 changes: 5 additions & 0 deletions website_sale_disable_shop/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
To configure this module, you need to:

#. Go to **Website > Configuration > Settings**
#. Search 'Shop Enabled' option.
#. When shop is disabled when user tries to access mywebsite.com/shop or any of its child pages, they will be redirected to homepage.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions website_sale_disable_shop/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_ir_http
27 changes: 27 additions & 0 deletions website_sale_disable_shop/tests/test_ir_http.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from odoo.tests import HttpCase


class TestIrHttp(HttpCase):
def setUp(self):
super().setUp()
self.website = self.env["website"].sudo().get_current_website()
self.path = "/shop"

def test_dispatch_shop_enabled(self):
# Test that a user can access "/shop
response = self.url_open(self.path)
self.assertEqual(
response.status_code,
200,
"Expected the response status code to be 200 which means no redirection",
)

def test_dispatch__shop_disabled(self):
# Test that a user cannot access "/shop
self.website.shop_enabled = False
response = self.url_open(self.path, allow_redirects=False)
self.assertEqual(
response.status_code,
303,
"Expected the response status code to be 303 indicating a redirect",
)
28 changes: 28 additions & 0 deletions website_sale_disable_shop/views/res_config_settings_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!-- Copyright 2024 Onestein
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
<odoo>
<record id="view_website_config_settings" model="ir.ui.view">
<field name="name">Website Shop Enable/Disable</field>
<field name="model">res.config.settings</field>
<field name="inherit_id" ref="website_sale.res_config_settings_view_form" />
<field name="arch" type="xml">
<div id="website_shop_checkout" position="after">
<h2>Website Shop</h2>
<div class="row mt16 o_settings_container" id="website_shop_setting">
<div class="col-12 col-lg-6 o_setting_box">
<div class="o_setting_left_pane">
<field name="shop_enabled"/>
</div>
<div class="o_setting_right_pane">
<label for="shop_enabled"/>
<div class="text-muted">
Is website shop enabled?
</div>
</div>
</div>
</div>
</div>
</field>
</record>
</odoo>

0 comments on commit aff3d0d

Please sign in to comment.