22# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
33
44from odoo import http
5+ from odoo .fields import Domain
56from odoo .http import request
6- from odoo .osv import expression
77
88from odoo .addons .website_sale .controllers .main import QueryURL , WebsiteSale
99
1010
1111class WebsiteSale (WebsiteSale ):
12- def _get_shop_domain (
13- self , search , category , attrib_values , search_in_description = True
14- ):
15- domain = super ()._get_shop_domain (
16- search = search ,
17- category = category ,
18- attrib_values = attrib_values ,
19- search_in_description = search_in_description ,
20- )
21- # add selected brands to product search domain
22- brands_list = self ._get_brand_ids (request .httprequest .args )
23- return self ._update_domain (brands_list , domain )
24-
2512 def _update_domain (self , brands_list , domain ):
2613 selected_brand_ids = [int (brand ) for brand in brands_list ]
2714 if brands_list :
@@ -49,20 +36,20 @@ def _build_brands_list(
4936 domain = [("product_ids" , "in" , search_products .ids )]
5037 return (
5138 request .env ["product.brand" ]
39+ .sudo ()
5240 .search (domain )
53- .filtered (lambda x : x .products_count > 0 )
41+ .filtered (lambda x : x .is_published and x . published_products_count > 0 )
5442 )
5543
5644 def _get_shop_domain_no_brands (
57- self , search , category , attrib_values , search_in_description
45+ self , search , category , attribute_value_dict , search_in_description
5846 ):
59- domain = super ()._get_shop_domain (
60- search = search ,
61- category = category ,
62- attrib_values = attrib_values ,
47+ return super ()._get_shop_domain (
48+ search ,
49+ category ,
50+ attribute_value_dict ,
6351 search_in_description = search_in_description ,
6452 )
65- return domain
6653
6754 def _remove_extra_brands (self , brands , search_products , attrib_values ):
6855 if attrib_values :
@@ -74,35 +61,44 @@ def _remove_extra_brands(self, brands, search_products, attrib_values):
7461 def _get_search_options (
7562 self ,
7663 category = None ,
77- attrib_values = None ,
78- pricelist = None ,
64+ attribute_value_dict = None ,
65+ tags = None ,
7966 min_price = 0.0 ,
8067 max_price = 0.0 ,
8168 conversion_rate = 1 ,
8269 ** post ,
8370 ):
8471 res = super ()._get_search_options (
8572 category = category ,
86- attrib_values = attrib_values ,
87- pricelist = pricelist ,
73+ attribute_value_dict = attribute_value_dict ,
74+ tags = tags ,
8875 min_price = min_price ,
8976 max_price = max_price ,
9077 conversion_rate = conversion_rate ,
9178 ** post ,
9279 )
93- res ["brand " ] = request .context .get ("brand_id " )
80+ res ["brand_ids " ] = request .env . context .get ("brand_ids " )
9481 return res
9582
9683 def _get_shop_domain (
97- self , search , category , attrib_values , search_in_description = True
84+ self , search , category , attribute_value_dict , search_in_description = True
9885 ):
9986 domain = super ()._get_shop_domain (
100- search , category , attrib_values , search_in_description = search_in_description
87+ search ,
88+ category ,
89+ attribute_value_dict ,
90+ search_in_description = search_in_description ,
10191 )
102- if "brand_id" in request .context :
103- domain = expression .AND (
104- [domain , [("product_brand_id" , "=" , request .context ["brand_id" ])]]
92+ brand_ids = request .env .context .get ("brand_ids" ) or [
93+ int (b )
94+ for b in (
95+ request .httprequest .args .getlist ("brand" )
96+ or request .httprequest .args .getlist ("brand_ids" )
10597 )
98+ if b and str (b ).isdigit ()
99+ ]
100+ if brand_ids :
101+ domain = Domain .AND ([domain , [("product_brand_id" , "in" , brand_ids )]])
106102 return domain
107103
108104 @http .route (
@@ -129,9 +125,12 @@ def shop(
129125 brand = None ,
130126 ** post ,
131127 ):
132- if brand :
133- context = dict (request .context )
134- context .setdefault ("brand_id" , int (brand ))
128+ brands_list = self ._get_brand_ids (request .httprequest .args )
129+ if brands_list :
130+ request .update_context (brand_ids = [int (b ) for b in brands_list ])
131+ elif brand :
132+ context = dict (request .env .context )
133+ context .setdefault ("brand_ids" , [int (brand )])
135134 request .update_context (** context )
136135 res = super ().shop (
137136 page = page ,
@@ -143,19 +142,19 @@ def shop(
143142 brand = brand ,
144143 ** post ,
145144 )
146- # parse selected attributes
147- attrib_list = request .httprequest .args .getlist ("attribute_value" )
148- attrib_values = res .qcontext ["attrib_values" ]
149- if attrib_list :
150- post ["attribute_value" ] = attrib_list
151- # get filtered products
152- products = res .qcontext ["products" ]
145+ qcontext = getattr (res , "qcontext" , None ) or {}
146+ attrib_values = qcontext .get ("attrib_values" )
147+ products = qcontext .get ("products" )
148+ if attrib_values is None or products is None :
149+ return res
150+ attribute_values = request .httprequest .args .getlist ("attribute_values" )
151+ if attribute_values :
152+ post ["attribute_values" ] = attribute_values
153153 domain = self ._get_shop_domain_no_brands (
154154 search , category , attrib_values , search_in_description = False
155155 )
156156 search_products = request .env ["product.template" ].search (domain )
157157 # build brands list
158- brands_list = self ._get_brand_ids (request .httprequest .args )
159158 selected_brand_ids = [int (brand ) for brand in brands_list ]
160159 brands = self ._build_brands_list (
161160 selected_brand_ids , search , products , search_products , category
@@ -178,22 +177,16 @@ def shop(
178177 )
179178 .ids
180179 )
181- # keep selected brands in URL
182- keep = QueryURL (
183- "/shop" ,
184- ** self ._shop_get_query_url_kwargs (
185- category and int (category ), search , min_price , max_price , ** post
186- ),
187- brand = brands_list ,
188- brand_ids = selected_brand_ids ,
189- )
180+ keep = qcontext .get ("keep" )
181+ if keep and hasattr (keep , "args" ):
182+ keep .args ["brand" ] = brands_list
183+ keep .args ["brand_ids" ] = selected_brand_ids
190184 # assign values for usage in qweb
191- res . qcontext .update (
185+ qcontext .update (
192186 {
193187 "brands" : brands ,
194188 "selected_brand_ids" : selected_brand_ids ,
195189 "attr_valid" : attrib_valid_ids ,
196- "keep" : keep ,
197190 }
198191 )
199192 return res
@@ -202,7 +195,7 @@ def shop(
202195 @http .route (["/page/product_brands" ], type = "http" , auth = "public" , website = True )
203196 def product_brands (self , ** post ):
204197 b_obj = request .env ["product.brand" ]
205- domain = [("website_published " , "=" , True )]
198+ domain = [("is_published " , "=" , True ), ( "published_products_count" , ">" , 0 )]
206199 if post .get ("search" ):
207200 domain += [("name" , "ilike" , post .get ("search" ))]
208201 brand_rec = b_obj .sudo ().search (domain )
0 commit comments