2021-06-01 07:14:49 +00:00
|
|
|
# Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and Contributors
|
2021-04-26 01:31:06 +00:00
|
|
|
# License: GNU General Public License v3. See license.txt
|
|
|
|
|
|
|
|
import frappe
|
2021-04-29 15:17:32 +00:00
|
|
|
from frappe.utils.redis_wrapper import RedisWrapper
|
2021-06-01 07:14:49 +00:00
|
|
|
from redisearch import (Client, AutoCompleter, Suggestion, IndexDefinition, TextField, TagField)
|
2021-04-26 01:31:06 +00:00
|
|
|
|
|
|
|
WEBSITE_ITEM_INDEX = 'website_items_index'
|
|
|
|
WEBSITE_ITEM_KEY_PREFIX = 'website_item:'
|
|
|
|
WEBSITE_ITEM_NAME_AUTOCOMPLETE = 'website_items_name_dict'
|
2021-04-26 05:45:20 +00:00
|
|
|
WEBSITE_ITEM_CATEGORY_AUTOCOMPLETE = 'website_items_category_dict'
|
2021-04-26 01:31:06 +00:00
|
|
|
|
|
|
|
ALLOWED_INDEXABLE_FIELDS_SET = {
|
|
|
|
'item_code',
|
|
|
|
'item_name',
|
|
|
|
'item_group',
|
|
|
|
'brand',
|
|
|
|
'description',
|
|
|
|
'web_long_description'
|
|
|
|
}
|
|
|
|
|
2021-06-01 07:14:49 +00:00
|
|
|
def is_search_module_loaded():
|
|
|
|
cache = frappe.cache()
|
|
|
|
out = cache.execute_command('MODULE LIST')
|
|
|
|
|
|
|
|
parsed_output = " ".join(
|
|
|
|
(" ".join([s.decode() for s in o if not isinstance(s, int)]) for o in out)
|
|
|
|
)
|
|
|
|
|
|
|
|
return "search" in parsed_output
|
|
|
|
|
|
|
|
# Decorator for checking wether Redisearch is there or not
|
|
|
|
def if_redisearch_loaded(function):
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
if is_search_module_loaded():
|
|
|
|
func = function(*args, **kwargs)
|
|
|
|
return func
|
|
|
|
return
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
def make_key(key):
|
|
|
|
return "{0}|{1}".format(frappe.conf.db_name, key).encode('utf-8')
|
|
|
|
|
|
|
|
@if_redisearch_loaded
|
2021-04-26 01:31:06 +00:00
|
|
|
def create_website_items_index():
|
|
|
|
'''Creates Index Definition'''
|
|
|
|
# CREATE index
|
2021-04-29 15:17:32 +00:00
|
|
|
client = Client(make_key(WEBSITE_ITEM_INDEX), conn=frappe.cache())
|
2021-04-26 01:31:06 +00:00
|
|
|
|
|
|
|
# DROP if already exists
|
|
|
|
try:
|
|
|
|
client.drop_index()
|
2021-06-01 07:14:49 +00:00
|
|
|
except Exception:
|
2021-04-26 01:31:06 +00:00
|
|
|
pass
|
|
|
|
|
2021-04-29 15:17:32 +00:00
|
|
|
idx_def = IndexDefinition([make_key(WEBSITE_ITEM_KEY_PREFIX)])
|
2021-04-26 01:31:06 +00:00
|
|
|
|
|
|
|
# Based on e-commerce settings
|
|
|
|
idx_fields = frappe.db.get_single_value(
|
2021-06-01 07:14:49 +00:00
|
|
|
'E Commerce Settings',
|
2021-04-26 01:31:06 +00:00
|
|
|
'search_index_fields'
|
|
|
|
).split(',')
|
|
|
|
|
|
|
|
if 'web_item_name' in idx_fields:
|
|
|
|
idx_fields.remove('web_item_name')
|
2021-06-01 07:14:49 +00:00
|
|
|
|
2021-04-26 01:31:06 +00:00
|
|
|
idx_fields = list(map(to_search_field, idx_fields))
|
|
|
|
|
|
|
|
client.create_index(
|
|
|
|
[TextField("web_item_name", sortable=True)] + idx_fields,
|
2021-04-29 15:17:32 +00:00
|
|
|
definition=idx_def,
|
2021-04-26 01:31:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
reindex_all_web_items()
|
|
|
|
define_autocomplete_dictionary()
|
|
|
|
|
|
|
|
def to_search_field(field):
|
|
|
|
if field == "tags":
|
|
|
|
return TagField("tags", separator=",")
|
|
|
|
|
|
|
|
return TextField(field)
|
|
|
|
|
2021-06-01 07:14:49 +00:00
|
|
|
@if_redisearch_loaded
|
2021-04-26 01:31:06 +00:00
|
|
|
def insert_item_to_index(website_item_doc):
|
|
|
|
# Insert item to index
|
|
|
|
key = get_cache_key(website_item_doc.name)
|
2021-06-01 07:14:49 +00:00
|
|
|
cache = frappe.cache()
|
2021-04-26 01:31:06 +00:00
|
|
|
web_item = create_web_item_map(website_item_doc)
|
2021-04-29 15:17:32 +00:00
|
|
|
|
|
|
|
for k, v in web_item.items():
|
2021-06-01 07:14:49 +00:00
|
|
|
super(RedisWrapper, cache).hset(make_key(key), k, v)
|
2021-04-29 15:17:32 +00:00
|
|
|
|
2021-04-26 01:31:06 +00:00
|
|
|
insert_to_name_ac(website_item_doc.web_item_name, website_item_doc.name)
|
|
|
|
|
2021-06-01 07:14:49 +00:00
|
|
|
@if_redisearch_loaded
|
2021-04-26 01:31:06 +00:00
|
|
|
def insert_to_name_ac(web_name, doc_name):
|
2021-04-29 15:17:32 +00:00
|
|
|
ac = AutoCompleter(make_key(WEBSITE_ITEM_NAME_AUTOCOMPLETE), conn=frappe.cache())
|
2021-04-26 01:31:06 +00:00
|
|
|
ac.add_suggestions(Suggestion(web_name, payload=doc_name))
|
|
|
|
|
|
|
|
def create_web_item_map(website_item_doc):
|
|
|
|
fields_to_index = get_fields_indexed()
|
|
|
|
web_item = {}
|
|
|
|
|
|
|
|
for f in fields_to_index:
|
|
|
|
web_item[f] = website_item_doc.get(f) or ''
|
|
|
|
|
|
|
|
return web_item
|
2021-05-26 14:56:34 +00:00
|
|
|
|
2021-06-01 07:14:49 +00:00
|
|
|
@if_redisearch_loaded
|
2021-04-26 01:31:06 +00:00
|
|
|
def update_index_for_item(website_item_doc):
|
|
|
|
# Reinsert to Cache
|
|
|
|
insert_item_to_index(website_item_doc)
|
|
|
|
define_autocomplete_dictionary()
|
|
|
|
|
2021-06-01 07:14:49 +00:00
|
|
|
@if_redisearch_loaded
|
2021-04-26 01:31:06 +00:00
|
|
|
def delete_item_from_index(website_item_doc):
|
2021-06-01 07:14:49 +00:00
|
|
|
cache = frappe.cache()
|
2021-04-26 01:31:06 +00:00
|
|
|
key = get_cache_key(website_item_doc.name)
|
2021-06-01 07:14:49 +00:00
|
|
|
|
2021-04-26 01:31:06 +00:00
|
|
|
try:
|
2021-06-01 07:14:49 +00:00
|
|
|
cache.delete(key)
|
2021-04-26 01:31:06 +00:00
|
|
|
except:
|
|
|
|
return False
|
|
|
|
|
2021-05-05 08:17:43 +00:00
|
|
|
delete_from_ac_dict(website_item_doc)
|
2021-04-26 01:31:06 +00:00
|
|
|
return True
|
|
|
|
|
2021-06-01 07:14:49 +00:00
|
|
|
@if_redisearch_loaded
|
2021-05-05 08:17:43 +00:00
|
|
|
def delete_from_ac_dict(website_item_doc):
|
|
|
|
'''Removes this items's name from autocomplete dictionary'''
|
2021-06-01 07:14:49 +00:00
|
|
|
cache = frappe.cache()
|
|
|
|
name_ac = AutoCompleter(make_key(WEBSITE_ITEM_NAME_AUTOCOMPLETE), conn=cache)
|
2021-05-05 08:17:43 +00:00
|
|
|
name_ac.delete(website_item_doc.web_item_name)
|
|
|
|
|
2021-06-01 07:14:49 +00:00
|
|
|
@if_redisearch_loaded
|
2021-04-26 01:31:06 +00:00
|
|
|
def define_autocomplete_dictionary():
|
2021-04-26 05:45:20 +00:00
|
|
|
"""Creates an autocomplete search dictionary for `name`.
|
2021-06-01 07:14:49 +00:00
|
|
|
Also creats autocomplete dictionary for `categories` if
|
|
|
|
checked in E Commerce Settings"""
|
2021-04-26 01:31:06 +00:00
|
|
|
|
2021-06-01 07:14:49 +00:00
|
|
|
cache = frappe.cache()
|
|
|
|
name_ac = AutoCompleter(make_key(WEBSITE_ITEM_NAME_AUTOCOMPLETE), conn=cache)
|
|
|
|
cat_ac = AutoCompleter(make_key(WEBSITE_ITEM_CATEGORY_AUTOCOMPLETE), conn=cache)
|
2021-04-26 01:31:06 +00:00
|
|
|
|
2021-04-26 05:45:20 +00:00
|
|
|
ac_categories = frappe.db.get_single_value(
|
2021-06-01 07:14:49 +00:00
|
|
|
'E Commerce Settings',
|
2021-04-26 05:45:20 +00:00
|
|
|
'show_categories_in_search_autocomplete'
|
|
|
|
)
|
2021-06-01 07:14:49 +00:00
|
|
|
|
2021-04-26 05:45:20 +00:00
|
|
|
# Delete both autocomplete dicts
|
2021-04-26 01:31:06 +00:00
|
|
|
try:
|
2021-06-01 07:14:49 +00:00
|
|
|
cache.delete(make_key(WEBSITE_ITEM_NAME_AUTOCOMPLETE))
|
|
|
|
cache.delete(make_key(WEBSITE_ITEM_CATEGORY_AUTOCOMPLETE))
|
2021-04-26 01:31:06 +00:00
|
|
|
except:
|
|
|
|
return False
|
2021-06-01 07:14:49 +00:00
|
|
|
|
2021-04-26 01:31:06 +00:00
|
|
|
items = frappe.get_all(
|
2021-06-01 07:14:49 +00:00
|
|
|
'Website Item',
|
|
|
|
fields=['web_item_name', 'item_group'],
|
|
|
|
filters={"published": 1}
|
2021-04-26 01:31:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
for item in items:
|
2021-04-26 05:45:20 +00:00
|
|
|
name_ac.add_suggestions(Suggestion(item.web_item_name))
|
|
|
|
if ac_categories and item.item_group:
|
|
|
|
cat_ac.add_suggestions(Suggestion(item.item_group))
|
2021-04-26 01:31:06 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
2021-06-01 07:14:49 +00:00
|
|
|
@if_redisearch_loaded
|
2021-04-26 01:31:06 +00:00
|
|
|
def reindex_all_web_items():
|
|
|
|
items = frappe.get_all(
|
2021-06-01 07:14:49 +00:00
|
|
|
'Website Item',
|
|
|
|
fields=get_fields_indexed(),
|
2021-04-26 01:31:06 +00:00
|
|
|
filters={"published": True}
|
|
|
|
)
|
|
|
|
|
2021-06-01 07:14:49 +00:00
|
|
|
cache = frappe.cache()
|
2021-04-26 01:31:06 +00:00
|
|
|
for item in items:
|
|
|
|
web_item = create_web_item_map(item)
|
2021-04-29 15:17:32 +00:00
|
|
|
key = make_key(get_cache_key(item.name))
|
|
|
|
|
|
|
|
for k, v in web_item.items():
|
2021-06-01 07:14:49 +00:00
|
|
|
super(RedisWrapper, cache).hset(key, k, v)
|
2021-04-29 15:17:32 +00:00
|
|
|
|
2021-04-26 01:31:06 +00:00
|
|
|
def get_cache_key(name):
|
|
|
|
name = frappe.scrub(name)
|
|
|
|
return f"{WEBSITE_ITEM_KEY_PREFIX}{name}"
|
|
|
|
|
|
|
|
def get_fields_indexed():
|
|
|
|
fields_to_index = frappe.db.get_single_value(
|
2021-06-01 07:14:49 +00:00
|
|
|
'E Commerce Settings',
|
2021-04-26 01:31:06 +00:00
|
|
|
'search_index_fields'
|
|
|
|
).split(',')
|
|
|
|
|
|
|
|
mandatory_fields = ['name', 'web_item_name', 'route', 'thumbnail']
|
|
|
|
fields_to_index = fields_to_index + mandatory_fields
|
|
|
|
|
|
|
|
return fields_to_index
|
|
|
|
|
|
|
|
# TODO: Remove later
|
|
|
|
# # Figure out a way to run this at startup
|
|
|
|
define_autocomplete_dictionary()
|
|
|
|
create_website_items_index()
|