2021-04-26 01:31:06 +00:00
|
|
|
# Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and Contributors
|
2014-04-18 10:45:31 +00:00
|
|
|
# License: GNU General Public License v3. See license.txt
|
|
|
|
|
|
|
|
import frappe
|
2017-05-11 12:33:12 +00:00
|
|
|
from frappe.utils import cint, cstr, nowdate
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2014-06-18 12:03:01 +00:00
|
|
|
from erpnext.setup.doctype.item_group.item_group import get_item_for_list_in_html
|
2021-02-25 08:26:38 +00:00
|
|
|
from erpnext.e_commerce.shopping_cart.product_info import set_product_info_for_website
|
2014-04-18 10:45:31 +00:00
|
|
|
|
2021-04-21 08:22:23 +00:00
|
|
|
# For SEARCH -------
|
2021-04-26 01:31:06 +00:00
|
|
|
from redisearch import AutoCompleter, Client, Query
|
|
|
|
from erpnext.e_commerce.website_item_indexing import WEBSITE_ITEM_INDEX, WEBSITE_ITEM_NAME_AUTOCOMPLETE
|
2021-04-21 08:22:23 +00:00
|
|
|
# -----------------
|
|
|
|
|
2014-04-18 10:45:31 +00:00
|
|
|
no_cache = 1
|
|
|
|
|
2021-04-21 08:22:23 +00:00
|
|
|
|
2016-06-27 09:24:46 +00:00
|
|
|
def get_context(context):
|
|
|
|
context.show_search = True
|
|
|
|
|
2014-04-18 10:45:31 +00:00
|
|
|
@frappe.whitelist(allow_guest=True)
|
2015-12-16 07:27:02 +00:00
|
|
|
def get_product_list(search=None, start=0, limit=12):
|
|
|
|
# limit = 12 because we show 12 items in the grid view
|
|
|
|
|
2014-04-18 10:45:31 +00:00
|
|
|
# base query
|
2018-07-03 05:03:49 +00:00
|
|
|
query = """select I.name, I.item_name, I.item_code, I.route, I.image, I.website_image, I.thumbnail, I.item_group,
|
|
|
|
I.description, I.web_long_description as website_description, I.is_stock_item,
|
|
|
|
case when (S.actual_qty - S.reserved_qty) > 0 then 1 else 0 end as in_stock, I.website_warehouse,
|
|
|
|
I.has_batch_no
|
2017-11-17 06:11:07 +00:00
|
|
|
from `tabItem` I
|
|
|
|
left join tabBin S on I.item_code = S.item_code and I.website_warehouse = S.warehouse
|
2018-08-16 03:52:33 +00:00
|
|
|
where (I.show_in_website = 1)
|
2017-11-17 06:11:07 +00:00
|
|
|
and I.disabled = 0
|
|
|
|
and (I.end_of_life is null or I.end_of_life='0000-00-00' or I.end_of_life > %(today)s)"""
|
2014-04-18 10:45:31 +00:00
|
|
|
|
|
|
|
# search term condition
|
|
|
|
if search:
|
2017-11-17 06:11:07 +00:00
|
|
|
query += """ and (I.web_long_description like %(search)s
|
|
|
|
or I.description like %(search)s
|
|
|
|
or I.item_name like %(search)s
|
|
|
|
or I.name like %(search)s)"""
|
2014-04-18 10:45:31 +00:00
|
|
|
search = "%" + cstr(search) + "%"
|
|
|
|
|
|
|
|
# order by
|
2018-02-28 13:29:55 +00:00
|
|
|
query += """ order by I.weightage desc, in_stock desc, I.modified desc limit %s, %s""" % (cint(start), cint(limit))
|
2014-04-18 10:45:31 +00:00
|
|
|
|
|
|
|
data = frappe.db.sql(query, {
|
|
|
|
"search": search,
|
2015-10-29 06:05:16 +00:00
|
|
|
"today": nowdate()
|
2014-04-18 10:45:31 +00:00
|
|
|
}, as_dict=1)
|
|
|
|
|
2018-03-20 08:58:44 +00:00
|
|
|
for item in data:
|
|
|
|
set_product_info_for_website(item)
|
|
|
|
|
2014-04-18 10:45:31 +00:00
|
|
|
return [get_item_for_list_in_html(r) for r in data]
|
2021-04-21 08:22:23 +00:00
|
|
|
|
|
|
|
@frappe.whitelist(allow_guest=True)
|
|
|
|
def search(query):
|
2021-04-22 08:51:29 +00:00
|
|
|
if not query:
|
|
|
|
# TODO: return top/recent searches
|
|
|
|
return []
|
|
|
|
|
2021-04-21 08:22:23 +00:00
|
|
|
ac = AutoCompleter(WEBSITE_ITEM_NAME_AUTOCOMPLETE, port=13000)
|
2021-04-22 08:51:29 +00:00
|
|
|
client = Client(WEBSITE_ITEM_INDEX, port=13000)
|
2021-04-21 08:22:23 +00:00
|
|
|
suggestions = ac.get_suggestions(query, num=10)
|
2021-04-22 08:51:29 +00:00
|
|
|
|
|
|
|
# Build a query
|
|
|
|
query_string = query
|
|
|
|
|
|
|
|
for s in suggestions:
|
|
|
|
query_string += f"|({s.string})"
|
|
|
|
|
|
|
|
q = Query(query_string)
|
|
|
|
|
|
|
|
print(f"Executing query: {q.query_string()}")
|
|
|
|
|
|
|
|
results = client.search(q)
|
|
|
|
results = list(map(convert_to_dict, results.docs))
|
|
|
|
|
|
|
|
print("SEARCH RESULTS ------------------\n ", results)
|
|
|
|
|
|
|
|
return results
|
|
|
|
|
|
|
|
def convert_to_dict(redis_search_doc):
|
|
|
|
return redis_search_doc.__dict__
|