Skip to content

Commit c2a2490

Browse files
committed
[16.0][IMP] website_sale_product_assortment: Implement restrictions on the cart in _cart_update and shop_payment
- Add SaleOrder._cart_update validation to prevent adding restricted products - Add WebsiteSale.shop_payment validation to block checkout with restricted products - Add unit tests for both validation methods - Add Spanish translations for error messages
1 parent 5a41ad6 commit c2a2490

File tree

8 files changed

+133
-8
lines changed

8 files changed

+133
-8
lines changed

website_sale_product_assortment/controllers/website_sale.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
33
from werkzeug.exceptions import NotFound
44

5+
from odoo import _, http
6+
from odoo.exceptions import UserError
57
from odoo.http import request, route
68

79
from odoo.addons.website_sale.controllers.main import WebsiteSale
@@ -81,3 +83,25 @@ def _get_search_options(
8183
("product_variant_ids", "in", list(allowed_product_ids))
8284
]
8385
return res
86+
87+
@http.route()
88+
def shop_payment(self, **post):
89+
order = request.website.sale_get_order()
90+
restricted_product_names = []
91+
for line in order.order_line.filtered(lambda l: not l.display_type):
92+
if (
93+
not line.product_id._show_quick_add_accesory_assortments()
94+
and line.product_id.purchase_ok
95+
):
96+
restricted_product_names.append(line.product_id.name)
97+
98+
if restricted_product_names:
99+
restricted_products_str = ", ".join(restricted_product_names)
100+
raise UserError(
101+
_(
102+
"The following products cannot be added to the cart because "
103+
"they are restricted: %s. Please remove the product to continue."
104+
)
105+
% restricted_products_str
106+
)
107+
return super().shop_payment(**post)

website_sale_product_assortment/i18n/es.po

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ msgstr "No aplicar restricción"
9292
msgid "Filters"
9393
msgstr "Filtros"
9494

95+
#. module: website_sale_product_assortment
96+
#. odoo-python
97+
#: code:addons/website_sale_product_assortment/models/sale_order.py:0
98+
#, python-format
99+
msgid "It cannot be added to the cart because the product is restricted."
100+
msgstr ""
101+
"No se puede añadir al carrito porque el producto está restringido."
102+
95103
#. module: website_sale_product_assortment
96104
#: model:ir.model.fields,help:website_sale_product_assortment.field_ir_filters__message_unavailable
97105
msgid ""
@@ -119,6 +127,17 @@ msgstr "No disponible"
119127
msgid "Product Template"
120128
msgstr "Plantilla de producto"
121129

130+
#. module: website_sale_product_assortment
131+
#. odoo-python
132+
#: code:addons/website_sale_product_assortment/controllers/website_sale.py:0
133+
#, python-format
134+
msgid ""
135+
"The following products cannot be added to the cart because they are "
136+
"restricted: %s. Please remove the product to continue."
137+
msgstr ""
138+
"Los siguientes productos no se pueden añadir al carrito porque están "
139+
"restringidos: %s. Por favor, elimínelos para continuar."
140+
122141
#. module: website_sale_product_assortment
123142
#. openerp-web
124143
#: code:addons/website_sale_product_assortment/static/src/xml/website_sale_product_assortment.xml:0

website_sale_product_assortment/i18n/website_sale_product_assortment.pot

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ msgstr ""
7070
msgid "Filters"
7171
msgstr ""
7272

73+
#. module: website_sale_product_assortment
74+
#. odoo-python
75+
#: code:addons/website_sale_product_assortment/models/sale_order.py:0
76+
#, python-format
77+
msgid "It cannot be added to the cart because the product is restricted."
78+
msgstr ""
79+
7380
#. module: website_sale_product_assortment
7481
#: model:ir.model.fields,help:website_sale_product_assortment.field_ir_filters__message_unavailable
7582
msgid ""
@@ -95,6 +102,15 @@ msgstr ""
95102
msgid "Product"
96103
msgstr ""
97104

105+
#. module: website_sale_product_assortment
106+
#. odoo-python
107+
#: code:addons/website_sale_product_assortment/controllers/website_sale.py:0
108+
#, python-format
109+
msgid ""
110+
"The following products cannot be added to the cart because they are "
111+
"restricted: %s. Please remove the product to continue."
112+
msgstr ""
113+
98114
#. module: website_sale_product_assortment
99115
#. odoo-javascript
100116
#: code:addons/website_sale_product_assortment/static/src/xml/website_sale_product_assortment.xml:0

website_sale_product_assortment/models/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
from . import product_template
33
from . import website_snippet_filter
44
from . import product_product
5+
from . import sale_order

website_sale_product_assortment/models/product_product.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class ProductProduct(models.Model):
55
_inherit = "product.product"
66

77
def _show_quick_add_accesory_assortments(self):
8+
self.ensure_one()
89
res = self._website_show_quick_add()
910
if not res:
1011
return res

website_sale_product_assortment/models/product_template.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,21 +95,18 @@ def _get_allowed_show_products(self, products):
9595
if not restricted_products_dict:
9696
return products
9797

98-
allowed_show_products = products.filtered(
98+
return products.filtered(
9999
lambda p: p.id not in restricted_products_dict
100100
or not any(
101101
assortment.website_availability == "no_show"
102102
for assortment in restricted_products_dict[p.id]
103103
)
104104
)
105-
return allowed_show_products
106105

107106
def _get_website_accessory_product(self):
108-
res = super()._get_website_accessory_product()
109-
visible_products = self._get_allowed_show_products(res)
110-
return visible_products
107+
return self._get_allowed_show_products(super()._get_website_accessory_product())
111108

112109
def _get_website_alternative_product(self):
113-
res = super()._get_website_alternative_product()
114-
visible_products = self._get_allowed_show_products(res)
115-
return visible_products
110+
return self._get_allowed_show_products(
111+
super()._get_website_alternative_product()
112+
)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from odoo import _, models
2+
from odoo.exceptions import UserError
3+
4+
5+
class SaleOrder(models.Model):
6+
_inherit = "sale.order"
7+
8+
def _cart_update(self, product_id, line_id=None, add_qty=0, set_qty=0, **kwargs):
9+
product = self.env["product.product"].browse(product_id)
10+
if not product._show_quick_add_accesory_assortments() and not (
11+
add_qty == 0 or (not add_qty and set_qty == 0)
12+
):
13+
raise UserError(
14+
_("It cannot be added to the cart because the product is restricted.")
15+
)
16+
return super()._cart_update(
17+
product_id,
18+
line_id=line_id,
19+
add_qty=add_qty,
20+
set_qty=set_qty,
21+
**kwargs,
22+
)

website_sale_product_assortment/tests/test_ui.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Copyright 2021 Tecnativa - Carlos Roca
22
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
3+
from unittest.mock import patch
4+
5+
from odoo.exceptions import UserError
36
from odoo.tests import tagged
47
from odoo.tests.common import HttpCase
58

@@ -25,6 +28,20 @@ def setUp(self):
2528
}
2629
)
2730

31+
# Configuración adicional para tests unitarios
32+
self.partner = self.env["res.partner"].create(
33+
{
34+
"name": "Test Partner",
35+
"email": "test@example.com",
36+
}
37+
)
38+
39+
self.sale_order = self.env["sale.order"].create(
40+
{
41+
"partner_id": self.partner.id,
42+
}
43+
)
44+
2845
def test_01_ui_no_restriction(self):
2946
self.env["ir.filters"].create(
3047
{
@@ -98,3 +115,31 @@ def test_04_ui_no_restriction_no_show(self):
98115
self.start_tour(
99116
"/shop", "test_assortment_with_no_restriction_no_show", login="admin"
100117
)
118+
119+
def test_05_ui_cart_update_restricted_product(self):
120+
product_patch_path = (
121+
"odoo.addons.website_sale_product_assortment.models.product_product."
122+
"ProductProduct._show_quick_add_accesory_assortments"
123+
)
124+
with patch(product_patch_path, return_value=False):
125+
with self.assertRaises(UserError) as cm:
126+
self.sale_order._cart_update(
127+
product_id=self.product.product_variant_id.id,
128+
add_qty=1,
129+
set_qty=1,
130+
)
131+
self.assertIn(
132+
"It cannot be added to the cart because the product is restricted.",
133+
str(cm.exception),
134+
)
135+
136+
def test_06_ui_cart_update_allowed_product(self):
137+
product_patch_path = (
138+
"odoo.addons.website_sale_product_assortment.models.product_product."
139+
"ProductProduct._show_quick_add_accesory_assortments"
140+
)
141+
with patch(product_patch_path, return_value=True):
142+
result = self.sale_order._cart_update(
143+
product_id=self.product.product_variant_id.id, add_qty=1
144+
)
145+
self.assertIsInstance(result, dict)

0 commit comments

Comments
 (0)