Skip to content

Commit

Permalink
[ssi_product_website] 14.0.1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Miftahussalam committed Aug 28, 2023
1 parent 2ff258e commit d81d941
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 957 deletions.
142 changes: 18 additions & 124 deletions ssi_product_website/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ def process(self, products, ppg=20, ppr=4):
maxy = 0
x = 0
for p in products:
x = min(max(p.website_size_x, 1), ppr)
y = min(max(p.website_size_y, 1), ppr)
x = min(1, ppr)
y = min(1, ppr)
if index >= ppg:
x = y = 1

Expand Down Expand Up @@ -91,25 +91,14 @@ def process(self, products, ppg=20, ppr=4):

class ProductWebsite(http.Controller):

def _get_pricelist_context(self):
pricelist_context = dict(request.env.context)
pricelist = False
if not pricelist_context.get('pricelist'):
pricelist = request.website.get_current_pricelist()
pricelist_context['pricelist'] = pricelist.id
else:
pricelist = request.env['product.pricelist'].browse(pricelist_context['pricelist'])

return pricelist_context, pricelist

def _get_search_order(self, post):
# OrderBy will be parsed in orm and so no direct sql injection
# id is added to be sure that order is a unique sort key
order = post.get('order') or 'website_sequence ASC'
return 'is_published desc, %s, id desc' % order
order = post.get('order') or 'name ASC'
return order

def _get_search_domain(self, search, category, attrib_values, search_in_description=True):
domains = [request.website.product_catalog_product_domain()]
def _get_search_domain(self, search, search_in_description=True):
domains = [[("product_catalog", "=", True)]]
if search:
for srch in search.split(" "):
subdomains = [
Expand All @@ -118,57 +107,19 @@ def _get_search_domain(self, search, category, attrib_values, search_in_descript
]
if search_in_description:
subdomains.append([('description', 'ilike', srch)])
subdomains.append([('description_sale', 'ilike', srch)])
domains.append(expression.OR(subdomains))

if category:
domains.append([('public_categ_ids', 'child_of', int(category))])

if attrib_values:
attrib = None
ids = []
for value in attrib_values:
if not attrib:
attrib = value[0]
ids.append(value[1])
elif value[0] == attrib:
ids.append(value[1])
else:
domains.append([('attribute_line_ids.value_ids', 'in', ids)])
attrib = value[0]
ids = [value[1]]
if attrib:
domains.append([('attribute_line_ids.value_ids', 'in', ids)])

return expression.AND(domains)

def sitemap_shop(env, rule, qs):
def sitemap_product_catalog(env, rule, qs):
if not qs or qs.lower() in '/product_catalog':
yield {'loc': '/product_catalog'}

Category = env['product.public.category']
dom = sitemap_qs2dom(qs, '/product_catalog/category', Category._rec_name)
dom += env['website'].get_current_website().website_domain()
for cat in Category.search(dom):
loc = '/product_catalog/category/%s' % slug(cat)
if not qs or qs.lower() in loc:
yield {'loc': loc}

@http.route([
'''/product_catalog''',
'''/product_catalog/page/<int:page>''',
'''/product_catalog/category/<model("product.public.category"):category>''',
'''/product_catalog/category/<model("product.public.category"):category>/page/<int:page>'''
], type='http', auth="public", website=True, sitemap=sitemap_shop)
def shop(self, page=0, category=None, search='', ppg=False, **post):
], type='http', auth="public", website=True, sitemap=sitemap_product_catalog)
def product_catalog(self, page=0, search='', ppg=False, **post):
add_qty = int(post.get('add_qty', 1))
Category = request.env['product.public.category']
if category:
category = Category.search([('id', '=', int(category))], limit=1)
if not category or not category.can_access_from_current_website():
raise NotFound()
else:
category = Category

if ppg:
try:
Expand All @@ -177,45 +128,24 @@ def shop(self, page=0, category=None, search='', ppg=False, **post):
except ValueError:
ppg = False
if not ppg:
ppg = request.env['website'].get_current_website().shop_ppg or 20

ppr = request.env['website'].get_current_website().shop_ppr or 4
ppg = 20

attrib_list = request.httprequest.args.getlist('attrib')
attrib_values = [[int(x) for x in v.split("-")] for v in attrib_list if v]
attributes_ids = {v[0] for v in attrib_values}
attrib_set = {v[1] for v in attrib_values}
ppr = 4

domain = self._get_search_domain(search, category, attrib_values)
domain = self._get_search_domain(search)

keep = QueryURL('/product_catalog', category=category and int(category), search=search, attrib=attrib_list,
keep = QueryURL('/product_catalog', search=search,
order=post.get('order'))

pricelist_context, pricelist = self._get_pricelist_context()

request.context = dict(request.context, pricelist=pricelist.id, partner=request.env.user.partner_id)
request.context = dict(request.context, partner=request.env.user.partner_id)

url = "/product_catalog"
if search:
post["search"] = search
if attrib_list:
post['attrib'] = attrib_list

Product = request.env['product.template'].with_context(bin_size=True)

search_product = Product.search(domain, order=self._get_search_order(post))
website_domain = request.website.website_domain()
categs_domain = [('parent_id', '=', False)] + website_domain
if search:
search_categories = Category.search(
[('product_tmpl_ids', 'in', search_product.ids)] + website_domain).parents_and_self
categs_domain.append(('id', 'in', search_categories.ids))
else:
search_categories = Category
categs = Category.search(categs_domain)

if category:
url = "/product_catalog/category/%s" % slug(category)

product_count = len(search_product)
pager = request.website.pager(url=url, total=product_count, page=page, step=ppg, scope=7, url_args=post)
Expand All @@ -234,79 +164,43 @@ def shop(self, page=0, category=None, search='', ppg=False, **post):
values = {
'search': search,
'order': post.get('order', ''),
'category': category,
'attrib_values': attrib_values,
'attrib_set': attrib_set,
'pager': pager,
'pricelist': pricelist,
'add_qty': add_qty,
'products': products,
'search_count': product_count, # common for all searchbox
'bins': TableCompute().process(products, ppg, ppr),
'ppg': ppg,
'ppr': ppr,
'categories': categs,
'attributes': attributes,
'keep': keep,
'search_categories_ids': search_categories.ids,
'layout_mode': layout_mode,
}
if category:
values['main_object'] = category
return request.render("ssi_product_website.products", values)

@http.route(['/product_catalog/<model("product.template"):product>'], type='http', auth="public", website=True, sitemap=True)
def product(self, product, category='', search='', **kwargs):
def product(self, product, search='', **kwargs):
if not product.can_access_from_current_website():
raise NotFound()

return request.render("ssi_product_website.product", self._prepare_product_values(product, category, search, **kwargs))

@http.route(['/product_catalog/product/<model("product.template"):product>'], type='http', auth="public", website=True,
sitemap=False)
def old_product(self, product, category='', search='', **kwargs):
# Compatibility pre-v14
return request.redirect(_build_url_w_params("/product_catalog/%s" % slug(product), request.params), code=301)
return request.render("ssi_product_website.product", self._prepare_product_values(product, search, **kwargs))

def _prepare_product_values(self, product, category, search, **kwargs):
def _prepare_product_values(self, product, search, **kwargs):
add_qty = int(kwargs.get('add_qty', 1))

product_context = dict(request.env.context, quantity=add_qty,
active_id=product.id,
partner=request.env.user.partner_id)
ProductCategory = request.env['product.public.category']

if category:
category = ProductCategory.browse(int(category)).exists()

attrib_list = request.httprequest.args.getlist('attrib')
attrib_values = [[int(x) for x in v.split("-")] for v in attrib_list if v]
attrib_set = {v[1] for v in attrib_values}

keep = QueryURL('/product_catalog', category=category and category.id, search=search, attrib=attrib_list)

categs = ProductCategory.search([('parent_id', '=', False)])

pricelist = request.website.get_current_pricelist()

if not product_context.get('pricelist'):
product_context['pricelist'] = pricelist.id
product = product.with_context(product_context)
keep = QueryURL('/product_catalog', search=search)

# Needed to trigger the recently viewed product rpc
view_track = request.website.viewref("ssi_product_website.product").track

return {
'search': search,
'category': category,
'pricelist': pricelist,
'attrib_values': attrib_values,
'attrib_set': attrib_set,
'keep': keep,
'categories': categs,
'main_object': product,
'product': product,
'add_qty': add_qty,
'view_track': view_track,
}

10 changes: 7 additions & 3 deletions ssi_product_website/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from . import product_template
from . import product
from . import website
# Copyright 2023 OpenSynergy Indonesia
# Copyright 2023 PT. Simetri Sinergi Indonesia
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

from . import (
product_template,
)
Loading

0 comments on commit d81d941

Please sign in to comment.