marination | ea036e4 | 2022-04-04 11:07:53 +0530 | [diff] [blame] | 1 | # Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and Contributors |
Hussain Nagaria | 8e55c95 | 2021-04-26 07:01:06 +0530 | [diff] [blame] | 2 | # License: GNU General Public License v3. See license.txt |
| 3 | |
marination | 97e3a85 | 2022-04-04 11:32:49 +0530 | [diff] [blame] | 4 | import json |
| 5 | |
Hussain Nagaria | 8e55c95 | 2021-04-26 07:01:06 +0530 | [diff] [blame] | 6 | import frappe |
marination | ea036e4 | 2022-04-04 11:07:53 +0530 | [diff] [blame] | 7 | from frappe import _ |
Hussain Nagaria | ffc8616 | 2021-04-29 20:47:32 +0530 | [diff] [blame] | 8 | from frappe.utils.redis_wrapper import RedisWrapper |
marination | ea036e4 | 2022-04-04 11:07:53 +0530 | [diff] [blame] | 9 | from redis import ResponseError |
Ankush Menat | 4a38ce6 | 2022-08-22 00:23:22 +0530 | [diff] [blame] | 10 | from redis.commands.search.field import TagField, TextField |
| 11 | from redis.commands.search.indexDefinition import IndexDefinition |
| 12 | from redis.commands.search.suggestion import Suggestion |
Hussain Nagaria | 8e55c95 | 2021-04-26 07:01:06 +0530 | [diff] [blame] | 13 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 14 | WEBSITE_ITEM_INDEX = "website_items_index" |
| 15 | WEBSITE_ITEM_KEY_PREFIX = "website_item:" |
| 16 | WEBSITE_ITEM_NAME_AUTOCOMPLETE = "website_items_name_dict" |
| 17 | WEBSITE_ITEM_CATEGORY_AUTOCOMPLETE = "website_items_category_dict" |
| 18 | |
Hussain Nagaria | 8e55c95 | 2021-04-26 07:01:06 +0530 | [diff] [blame] | 19 | |
marination | bbcbcf7 | 2021-09-02 14:07:59 +0530 | [diff] [blame] | 20 | def get_indexable_web_fields(): |
| 21 | "Return valid fields from Website Item that can be searched for." |
| 22 | web_item_meta = frappe.get_meta("Website Item", cached=True) |
| 23 | valid_fields = filter( |
| 24 | lambda df: df.fieldtype in ("Link", "Table MultiSelect", "Data", "Small Text", "Text Editor"), |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 25 | web_item_meta.fields, |
| 26 | ) |
marination | bbcbcf7 | 2021-09-02 14:07:59 +0530 | [diff] [blame] | 27 | |
| 28 | return [df.fieldname for df in valid_fields] |
| 29 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 30 | |
marination | 7e207c8 | 2022-03-31 16:29:18 +0530 | [diff] [blame] | 31 | def is_redisearch_enabled(): |
| 32 | "Return True only if redisearch is loaded and enabled." |
| 33 | is_redisearch_enabled = frappe.db.get_single_value("E Commerce Settings", "is_redisearch_enabled") |
| 34 | return is_search_module_loaded() and is_redisearch_enabled |
| 35 | |
| 36 | |
marination | b0d7e32 | 2021-06-01 12:44:49 +0530 | [diff] [blame] | 37 | def is_search_module_loaded(): |
Marica | d637f79 | 2021-09-18 14:23:44 +0530 | [diff] [blame] | 38 | try: |
| 39 | cache = frappe.cache() |
Ankush Menat | 4a38ce6 | 2022-08-22 00:23:22 +0530 | [diff] [blame] | 40 | for module in cache.module_list(): |
| 41 | if module.get(b"name") == b"search": |
| 42 | return True |
Marica | d637f79 | 2021-09-18 14:23:44 +0530 | [diff] [blame] | 43 | except Exception: |
marination | ea036e4 | 2022-04-04 11:07:53 +0530 | [diff] [blame] | 44 | return False # handling older redis versions |
marination | b0d7e32 | 2021-06-01 12:44:49 +0530 | [diff] [blame] | 45 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 46 | |
marination | 7e207c8 | 2022-03-31 16:29:18 +0530 | [diff] [blame] | 47 | def if_redisearch_enabled(function): |
| 48 | "Decorator to check if Redisearch is enabled." |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 49 | |
marination | b0d7e32 | 2021-06-01 12:44:49 +0530 | [diff] [blame] | 50 | def wrapper(*args, **kwargs): |
marination | 7e207c8 | 2022-03-31 16:29:18 +0530 | [diff] [blame] | 51 | if is_redisearch_enabled(): |
marination | b0d7e32 | 2021-06-01 12:44:49 +0530 | [diff] [blame] | 52 | func = function(*args, **kwargs) |
| 53 | return func |
| 54 | return |
| 55 | |
| 56 | return wrapper |
| 57 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 58 | |
marination | b0d7e32 | 2021-06-01 12:44:49 +0530 | [diff] [blame] | 59 | def make_key(key): |
Ankush Menat | 4a38ce6 | 2022-08-22 00:23:22 +0530 | [diff] [blame] | 60 | return frappe.cache().make_key(key) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 61 | |
marination | b0d7e32 | 2021-06-01 12:44:49 +0530 | [diff] [blame] | 62 | |
marination | 7e207c8 | 2022-03-31 16:29:18 +0530 | [diff] [blame] | 63 | @if_redisearch_enabled |
Hussain Nagaria | 8e55c95 | 2021-04-26 07:01:06 +0530 | [diff] [blame] | 64 | def create_website_items_index(): |
marination | bbcbcf7 | 2021-09-02 14:07:59 +0530 | [diff] [blame] | 65 | "Creates Index Definition." |
| 66 | |
Ankush Menat | 4a38ce6 | 2022-08-22 00:23:22 +0530 | [diff] [blame] | 67 | redis = frappe.cache() |
| 68 | index = redis.ft(WEBSITE_ITEM_INDEX) |
Hussain Nagaria | 8e55c95 | 2021-04-26 07:01:06 +0530 | [diff] [blame] | 69 | |
Hussain Nagaria | 8e55c95 | 2021-04-26 07:01:06 +0530 | [diff] [blame] | 70 | try: |
Ankush Menat | 4a38ce6 | 2022-08-22 00:23:22 +0530 | [diff] [blame] | 71 | index.dropindex() # drop if already exists |
marination | ea036e4 | 2022-04-04 11:07:53 +0530 | [diff] [blame] | 72 | except ResponseError: |
| 73 | # will most likely raise a ResponseError if index does not exist |
| 74 | # ignore and create index |
Hussain Nagaria | 8e55c95 | 2021-04-26 07:01:06 +0530 | [diff] [blame] | 75 | pass |
marination | ea036e4 | 2022-04-04 11:07:53 +0530 | [diff] [blame] | 76 | except Exception: |
| 77 | raise_redisearch_error() |
Hussain Nagaria | 8e55c95 | 2021-04-26 07:01:06 +0530 | [diff] [blame] | 78 | |
Hussain Nagaria | ffc8616 | 2021-04-29 20:47:32 +0530 | [diff] [blame] | 79 | idx_def = IndexDefinition([make_key(WEBSITE_ITEM_KEY_PREFIX)]) |
Hussain Nagaria | 8e55c95 | 2021-04-26 07:01:06 +0530 | [diff] [blame] | 80 | |
marination | ea036e4 | 2022-04-04 11:07:53 +0530 | [diff] [blame] | 81 | # Index fields mentioned in e-commerce settings |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 82 | idx_fields = frappe.db.get_single_value("E Commerce Settings", "search_index_fields") |
| 83 | idx_fields = idx_fields.split(",") if idx_fields else [] |
Hussain Nagaria | 8e55c95 | 2021-04-26 07:01:06 +0530 | [diff] [blame] | 84 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 85 | if "web_item_name" in idx_fields: |
| 86 | idx_fields.remove("web_item_name") |
marination | b0d7e32 | 2021-06-01 12:44:49 +0530 | [diff] [blame] | 87 | |
Ankush Menat | 4a38ce6 | 2022-08-22 00:23:22 +0530 | [diff] [blame] | 88 | idx_fields = [to_search_field(f) for f in idx_fields] |
Hussain Nagaria | 8e55c95 | 2021-04-26 07:01:06 +0530 | [diff] [blame] | 89 | |
Ankush Menat | 4a38ce6 | 2022-08-22 00:23:22 +0530 | [diff] [blame] | 90 | # TODO: sortable? |
| 91 | index.create_index( |
Hussain Nagaria | 8e55c95 | 2021-04-26 07:01:06 +0530 | [diff] [blame] | 92 | [TextField("web_item_name", sortable=True)] + idx_fields, |
Hussain Nagaria | ffc8616 | 2021-04-29 20:47:32 +0530 | [diff] [blame] | 93 | definition=idx_def, |
Hussain Nagaria | 8e55c95 | 2021-04-26 07:01:06 +0530 | [diff] [blame] | 94 | ) |
| 95 | |
| 96 | reindex_all_web_items() |
| 97 | define_autocomplete_dictionary() |
| 98 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 99 | |
Hussain Nagaria | 8e55c95 | 2021-04-26 07:01:06 +0530 | [diff] [blame] | 100 | def to_search_field(field): |
| 101 | if field == "tags": |
| 102 | return TagField("tags", separator=",") |
| 103 | |
| 104 | return TextField(field) |
| 105 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 106 | |
marination | 7e207c8 | 2022-03-31 16:29:18 +0530 | [diff] [blame] | 107 | @if_redisearch_enabled |
Hussain Nagaria | 8e55c95 | 2021-04-26 07:01:06 +0530 | [diff] [blame] | 108 | def insert_item_to_index(website_item_doc): |
| 109 | # Insert item to index |
| 110 | key = get_cache_key(website_item_doc.name) |
marination | b0d7e32 | 2021-06-01 12:44:49 +0530 | [diff] [blame] | 111 | cache = frappe.cache() |
Hussain Nagaria | 8e55c95 | 2021-04-26 07:01:06 +0530 | [diff] [blame] | 112 | web_item = create_web_item_map(website_item_doc) |
Hussain Nagaria | ffc8616 | 2021-04-29 20:47:32 +0530 | [diff] [blame] | 113 | |
marination | ea036e4 | 2022-04-04 11:07:53 +0530 | [diff] [blame] | 114 | for field, value in web_item.items(): |
| 115 | super(RedisWrapper, cache).hset(make_key(key), field, value) |
Hussain Nagaria | ffc8616 | 2021-04-29 20:47:32 +0530 | [diff] [blame] | 116 | |
Hussain Nagaria | 8e55c95 | 2021-04-26 07:01:06 +0530 | [diff] [blame] | 117 | insert_to_name_ac(website_item_doc.web_item_name, website_item_doc.name) |
| 118 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 119 | |
marination | 7e207c8 | 2022-03-31 16:29:18 +0530 | [diff] [blame] | 120 | @if_redisearch_enabled |
Hussain Nagaria | 8e55c95 | 2021-04-26 07:01:06 +0530 | [diff] [blame] | 121 | def insert_to_name_ac(web_name, doc_name): |
Ankush Menat | 4a38ce6 | 2022-08-22 00:23:22 +0530 | [diff] [blame] | 122 | ac = frappe.cache().ft() |
| 123 | ac.sugadd(WEBSITE_ITEM_NAME_AUTOCOMPLETE, Suggestion(web_name, payload=doc_name)) |
Hussain Nagaria | 8e55c95 | 2021-04-26 07:01:06 +0530 | [diff] [blame] | 124 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 125 | |
Hussain Nagaria | 8e55c95 | 2021-04-26 07:01:06 +0530 | [diff] [blame] | 126 | def create_web_item_map(website_item_doc): |
| 127 | fields_to_index = get_fields_indexed() |
Hussain Nagaria | 8e55c95 | 2021-04-26 07:01:06 +0530 | [diff] [blame] | 128 | web_item = {} |
| 129 | |
marination | ea036e4 | 2022-04-04 11:07:53 +0530 | [diff] [blame] | 130 | for field in fields_to_index: |
| 131 | web_item[field] = website_item_doc.get(field) or "" |
Hussain Nagaria | 8e55c95 | 2021-04-26 07:01:06 +0530 | [diff] [blame] | 132 | |
| 133 | return web_item |
Hussain Nagaria | bd0d0ad | 2021-05-26 20:26:34 +0530 | [diff] [blame] | 134 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 135 | |
marination | 7e207c8 | 2022-03-31 16:29:18 +0530 | [diff] [blame] | 136 | @if_redisearch_enabled |
Hussain Nagaria | 8e55c95 | 2021-04-26 07:01:06 +0530 | [diff] [blame] | 137 | def update_index_for_item(website_item_doc): |
| 138 | # Reinsert to Cache |
| 139 | insert_item_to_index(website_item_doc) |
| 140 | define_autocomplete_dictionary() |
Hussain Nagaria | 8e55c95 | 2021-04-26 07:01:06 +0530 | [diff] [blame] | 141 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 142 | |
marination | 7e207c8 | 2022-03-31 16:29:18 +0530 | [diff] [blame] | 143 | @if_redisearch_enabled |
Hussain Nagaria | 8e55c95 | 2021-04-26 07:01:06 +0530 | [diff] [blame] | 144 | def delete_item_from_index(website_item_doc): |
marination | b0d7e32 | 2021-06-01 12:44:49 +0530 | [diff] [blame] | 145 | cache = frappe.cache() |
Hussain Nagaria | 8e55c95 | 2021-04-26 07:01:06 +0530 | [diff] [blame] | 146 | key = get_cache_key(website_item_doc.name) |
marination | b0d7e32 | 2021-06-01 12:44:49 +0530 | [diff] [blame] | 147 | |
Hussain Nagaria | 8e55c95 | 2021-04-26 07:01:06 +0530 | [diff] [blame] | 148 | try: |
marination | b0d7e32 | 2021-06-01 12:44:49 +0530 | [diff] [blame] | 149 | cache.delete(key) |
marination | ea30ce4 | 2021-06-02 13:24:06 +0530 | [diff] [blame] | 150 | except Exception: |
marination | ea036e4 | 2022-04-04 11:07:53 +0530 | [diff] [blame] | 151 | raise_redisearch_error() |
Hussain Nagaria | 8e55c95 | 2021-04-26 07:01:06 +0530 | [diff] [blame] | 152 | |
Hussain Nagaria | 34f3a66 | 2021-05-05 13:47:43 +0530 | [diff] [blame] | 153 | delete_from_ac_dict(website_item_doc) |
Hussain Nagaria | 8e55c95 | 2021-04-26 07:01:06 +0530 | [diff] [blame] | 154 | return True |
| 155 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 156 | |
marination | 7e207c8 | 2022-03-31 16:29:18 +0530 | [diff] [blame] | 157 | @if_redisearch_enabled |
Hussain Nagaria | 34f3a66 | 2021-05-05 13:47:43 +0530 | [diff] [blame] | 158 | def delete_from_ac_dict(website_item_doc): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 159 | """Removes this items's name from autocomplete dictionary""" |
Ankush Menat | 4a38ce6 | 2022-08-22 00:23:22 +0530 | [diff] [blame] | 160 | ac = frappe.cache().ft() |
| 161 | ac.sugdel(website_item_doc.web_item_name) |
Hussain Nagaria | 34f3a66 | 2021-05-05 13:47:43 +0530 | [diff] [blame] | 162 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 163 | |
marination | 7e207c8 | 2022-03-31 16:29:18 +0530 | [diff] [blame] | 164 | @if_redisearch_enabled |
Hussain Nagaria | 8e55c95 | 2021-04-26 07:01:06 +0530 | [diff] [blame] | 165 | def define_autocomplete_dictionary(): |
marination | 07f1745 | 2022-04-01 18:47:01 +0530 | [diff] [blame] | 166 | """ |
| 167 | Defines/Redefines an autocomplete search dictionary for Website Item Name. |
| 168 | Also creats autocomplete dictionary for Published Item Groups. |
| 169 | """ |
Hussain Nagaria | 8e55c95 | 2021-04-26 07:01:06 +0530 | [diff] [blame] | 170 | |
marination | b0d7e32 | 2021-06-01 12:44:49 +0530 | [diff] [blame] | 171 | cache = frappe.cache() |
marination | b0d7e32 | 2021-06-01 12:44:49 +0530 | [diff] [blame] | 172 | |
Hussain Nagaria | f342155 | 2021-04-26 11:15:20 +0530 | [diff] [blame] | 173 | # Delete both autocomplete dicts |
Hussain Nagaria | 8e55c95 | 2021-04-26 07:01:06 +0530 | [diff] [blame] | 174 | try: |
marination | b0d7e32 | 2021-06-01 12:44:49 +0530 | [diff] [blame] | 175 | cache.delete(make_key(WEBSITE_ITEM_NAME_AUTOCOMPLETE)) |
| 176 | cache.delete(make_key(WEBSITE_ITEM_CATEGORY_AUTOCOMPLETE)) |
marination | ea30ce4 | 2021-06-02 13:24:06 +0530 | [diff] [blame] | 177 | except Exception: |
marination | ea036e4 | 2022-04-04 11:07:53 +0530 | [diff] [blame] | 178 | raise_redisearch_error() |
marination | b0d7e32 | 2021-06-01 12:44:49 +0530 | [diff] [blame] | 179 | |
Ankush Menat | 4a38ce6 | 2022-08-22 00:23:22 +0530 | [diff] [blame] | 180 | create_items_autocomplete_dict() |
| 181 | create_item_groups_autocomplete_dict() |
marination | 07f1745 | 2022-04-01 18:47:01 +0530 | [diff] [blame] | 182 | |
| 183 | |
| 184 | @if_redisearch_enabled |
Ankush Menat | 4a38ce6 | 2022-08-22 00:23:22 +0530 | [diff] [blame] | 185 | def create_items_autocomplete_dict(): |
marination | 07f1745 | 2022-04-01 18:47:01 +0530 | [diff] [blame] | 186 | "Add items as suggestions in Autocompleter." |
Ankush Menat | 4a38ce6 | 2022-08-22 00:23:22 +0530 | [diff] [blame] | 187 | |
| 188 | ac = frappe.cache().ft() |
Hussain Nagaria | 8e55c95 | 2021-04-26 07:01:06 +0530 | [diff] [blame] | 189 | items = frappe.get_all( |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 190 | "Website Item", fields=["web_item_name", "item_group"], filters={"published": 1} |
Hussain Nagaria | 8e55c95 | 2021-04-26 07:01:06 +0530 | [diff] [blame] | 191 | ) |
Hussain Nagaria | 8e55c95 | 2021-04-26 07:01:06 +0530 | [diff] [blame] | 192 | for item in items: |
Ankush Menat | 4a38ce6 | 2022-08-22 00:23:22 +0530 | [diff] [blame] | 193 | ac.sugadd(WEBSITE_ITEM_NAME_AUTOCOMPLETE, Suggestion(item.web_item_name)) |
Hussain Nagaria | 8e55c95 | 2021-04-26 07:01:06 +0530 | [diff] [blame] | 194 | |
marination | 07f1745 | 2022-04-01 18:47:01 +0530 | [diff] [blame] | 195 | |
| 196 | @if_redisearch_enabled |
Ankush Menat | 4a38ce6 | 2022-08-22 00:23:22 +0530 | [diff] [blame] | 197 | def create_item_groups_autocomplete_dict(): |
marination | 07f1745 | 2022-04-01 18:47:01 +0530 | [diff] [blame] | 198 | "Add item groups with weightage as suggestions in Autocompleter." |
Ankush Menat | 4a38ce6 | 2022-08-22 00:23:22 +0530 | [diff] [blame] | 199 | |
marination | 07f1745 | 2022-04-01 18:47:01 +0530 | [diff] [blame] | 200 | published_item_groups = frappe.get_all( |
| 201 | "Item Group", fields=["name", "route", "weightage"], filters={"show_in_website": 1} |
| 202 | ) |
| 203 | if not published_item_groups: |
| 204 | return |
| 205 | |
Ankush Menat | 4a38ce6 | 2022-08-22 00:23:22 +0530 | [diff] [blame] | 206 | ac = frappe.cache().ft() |
| 207 | |
marination | 07f1745 | 2022-04-01 18:47:01 +0530 | [diff] [blame] | 208 | for item_group in published_item_groups: |
marination | 3445682 | 2022-04-04 12:33:25 +0530 | [diff] [blame] | 209 | payload = json.dumps({"name": item_group.name, "route": item_group.route}) |
Ankush Menat | 4a38ce6 | 2022-08-22 00:23:22 +0530 | [diff] [blame] | 210 | ac.sugadd( |
| 211 | WEBSITE_ITEM_CATEGORY_AUTOCOMPLETE, |
marination | 07f1745 | 2022-04-01 18:47:01 +0530 | [diff] [blame] | 212 | Suggestion( |
| 213 | string=item_group.name, |
marination | 7ef1ccb | 2022-04-04 12:04:35 +0530 | [diff] [blame] | 214 | score=frappe.utils.flt(item_group.weightage) or 1.0, |
marination | 07f1745 | 2022-04-01 18:47:01 +0530 | [diff] [blame] | 215 | payload=payload, # additional info that can be retrieved later |
Ankush Menat | 4a38ce6 | 2022-08-22 00:23:22 +0530 | [diff] [blame] | 216 | ), |
marination | 07f1745 | 2022-04-01 18:47:01 +0530 | [diff] [blame] | 217 | ) |
Hussain Nagaria | 8e55c95 | 2021-04-26 07:01:06 +0530 | [diff] [blame] | 218 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 219 | |
marination | 7e207c8 | 2022-03-31 16:29:18 +0530 | [diff] [blame] | 220 | @if_redisearch_enabled |
Hussain Nagaria | 8e55c95 | 2021-04-26 07:01:06 +0530 | [diff] [blame] | 221 | def reindex_all_web_items(): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 222 | items = frappe.get_all("Website Item", fields=get_fields_indexed(), filters={"published": True}) |
Hussain Nagaria | 8e55c95 | 2021-04-26 07:01:06 +0530 | [diff] [blame] | 223 | |
marination | b0d7e32 | 2021-06-01 12:44:49 +0530 | [diff] [blame] | 224 | cache = frappe.cache() |
Hussain Nagaria | 8e55c95 | 2021-04-26 07:01:06 +0530 | [diff] [blame] | 225 | for item in items: |
| 226 | web_item = create_web_item_map(item) |
Hussain Nagaria | ffc8616 | 2021-04-29 20:47:32 +0530 | [diff] [blame] | 227 | key = make_key(get_cache_key(item.name)) |
| 228 | |
marination | ea036e4 | 2022-04-04 11:07:53 +0530 | [diff] [blame] | 229 | for field, value in web_item.items(): |
| 230 | super(RedisWrapper, cache).hset(key, field, value) |
Hussain Nagaria | ffc8616 | 2021-04-29 20:47:32 +0530 | [diff] [blame] | 231 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 232 | |
Hussain Nagaria | 8e55c95 | 2021-04-26 07:01:06 +0530 | [diff] [blame] | 233 | def get_cache_key(name): |
| 234 | name = frappe.scrub(name) |
| 235 | return f"{WEBSITE_ITEM_KEY_PREFIX}{name}" |
| 236 | |
Hussain Nagaria | 8e55c95 | 2021-04-26 07:01:06 +0530 | [diff] [blame] | 237 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 238 | def get_fields_indexed(): |
| 239 | fields_to_index = frappe.db.get_single_value("E Commerce Settings", "search_index_fields") |
| 240 | fields_to_index = fields_to_index.split(",") if fields_to_index else [] |
| 241 | |
| 242 | mandatory_fields = ["name", "web_item_name", "route", "thumbnail", "ranking"] |
Hussain Nagaria | 8e55c95 | 2021-04-26 07:01:06 +0530 | [diff] [blame] | 243 | fields_to_index = fields_to_index + mandatory_fields |
| 244 | |
| 245 | return fields_to_index |
marination | ea036e4 | 2022-04-04 11:07:53 +0530 | [diff] [blame] | 246 | |
| 247 | |
| 248 | def raise_redisearch_error(): |
| 249 | "Create an Error Log and raise error." |
Rushabh Mehta | 548afba | 2022-05-02 15:04:26 +0530 | [diff] [blame] | 250 | log = frappe.log_error("Redisearch Error") |
marination | ea036e4 | 2022-04-04 11:07:53 +0530 | [diff] [blame] | 251 | log_link = frappe.utils.get_link_to_form("Error Log", log.name) |
| 252 | |
| 253 | frappe.throw( |
| 254 | msg=_("Something went wrong. Check {0}").format(log_link), title=_("Redisearch Error") |
| 255 | ) |