Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 1 | import frappe |
| 2 | import requests |
Prateeksha Singh | b44ea4c | 2018-05-22 11:57:21 +0530 | [diff] [blame] | 3 | from frappe import _ |
Suraj Shetty | 32fd753 | 2022-07-06 15:09:44 +0530 | [diff] [blame] | 4 | from frappe.core.utils import html2text |
Prateeksha Singh | b44ea4c | 2018-05-22 11:57:21 +0530 | [diff] [blame] | 5 | from frappe.utils import sanitize_html |
| 6 | from frappe.utils.global_search import search |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 7 | from jinja2 import utils |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 8 | |
Prateeksha Singh | b44ea4c | 2018-05-22 11:57:21 +0530 | [diff] [blame] | 9 | |
| 10 | def get_context(context): |
| 11 | context.no_cache = 1 |
| 12 | if frappe.form_dict.q: |
| 13 | query = str(utils.escape(sanitize_html(frappe.form_dict.q))) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 14 | context.title = _("Help Results for") |
Prateeksha Singh | 0c79622 | 2018-08-06 12:33:34 +0530 | [diff] [blame] | 15 | context.query = query |
Aditya Hase | 691c166 | 2019-02-07 20:35:52 +0530 | [diff] [blame] | 16 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 17 | context.route = "/search_help" |
Prateeksha Singh | b44ea4c | 2018-05-22 11:57:21 +0530 | [diff] [blame] | 18 | d = frappe._dict() |
| 19 | d.results_sections = get_help_results_sections(query) |
| 20 | context.update(d) |
| 21 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 22 | context.title = _("Docs Search") |
Prateeksha Singh | b44ea4c | 2018-05-22 11:57:21 +0530 | [diff] [blame] | 23 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 24 | |
| 25 | @frappe.whitelist(allow_guest=True) |
Prateeksha Singh | b44ea4c | 2018-05-22 11:57:21 +0530 | [diff] [blame] | 26 | def get_help_results_sections(text): |
| 27 | out = [] |
| 28 | settings = frappe.get_doc("Support Settings", "Support Settings") |
| 29 | |
| 30 | for api in settings.search_apis: |
| 31 | results = [] |
| 32 | if api.source_type == "API": |
| 33 | response_json = get_response(api, text) |
| 34 | topics_data = get_topics_data(api, response_json) |
| 35 | results = prepare_api_results(api, topics_data) |
| 36 | else: |
| 37 | # Source type is Doctype |
| 38 | doctype = api.source_doctype |
| 39 | raw = search(text, 0, 20, doctype) |
| 40 | results = prepare_doctype_results(api, raw) |
| 41 | |
| 42 | if results: |
| 43 | # Add section |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 44 | out.append({"title": api.source_name, "results": results}) |
Prateeksha Singh | b44ea4c | 2018-05-22 11:57:21 +0530 | [diff] [blame] | 45 | |
| 46 | return out |
| 47 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 48 | |
Prateeksha Singh | b44ea4c | 2018-05-22 11:57:21 +0530 | [diff] [blame] | 49 | def get_response(api, text): |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 50 | response = requests.get(api.base_url + "/" + api.query_route, data={api.search_term_param_name: text}) |
Prateeksha Singh | b44ea4c | 2018-05-22 11:57:21 +0530 | [diff] [blame] | 51 | |
| 52 | response.raise_for_status() |
| 53 | return response.json() |
| 54 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 55 | |
Prateeksha Singh | b44ea4c | 2018-05-22 11:57:21 +0530 | [diff] [blame] | 56 | def get_topics_data(api, response_json): |
| 57 | if not response_json: |
| 58 | response_json = {} |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 59 | topics_data = {} # it will actually be an array |
| 60 | key_list = api.response_result_key_path.split(",") |
Prateeksha Singh | b44ea4c | 2018-05-22 11:57:21 +0530 | [diff] [blame] | 61 | |
| 62 | for key in key_list: |
| 63 | topics_data = response_json.get(key) if not topics_data else topics_data.get(key) |
| 64 | |
| 65 | return topics_data or [] |
| 66 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 67 | |
Prateeksha Singh | b44ea4c | 2018-05-22 11:57:21 +0530 | [diff] [blame] | 68 | def prepare_api_results(api, topics_data): |
| 69 | if not topics_data: |
| 70 | topics_data = [] |
| 71 | |
| 72 | results = [] |
| 73 | for topic in topics_data: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 74 | route = api.base_url + "/" + (api.post_route + "/" if api.post_route else "") |
| 75 | for key in api.post_route_key_list.split(","): |
Ankush Menat | 8fe5feb | 2021-11-04 19:48:32 +0530 | [diff] [blame] | 76 | route += str(topic[key]) |
Prateeksha Singh | b44ea4c | 2018-05-22 11:57:21 +0530 | [diff] [blame] | 77 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 78 | results.append( |
| 79 | frappe._dict( |
| 80 | { |
| 81 | "title": topic[api.post_title_key], |
| 82 | "preview": html2text(topic[api.post_description_key]), |
| 83 | "route": route, |
| 84 | } |
| 85 | ) |
| 86 | ) |
Prateeksha Singh | b44ea4c | 2018-05-22 11:57:21 +0530 | [diff] [blame] | 87 | return results[:5] |
| 88 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 89 | |
Prateeksha Singh | b44ea4c | 2018-05-22 11:57:21 +0530 | [diff] [blame] | 90 | def prepare_doctype_results(api, raw): |
| 91 | results = [] |
| 92 | for r in raw: |
| 93 | prepared_result = {} |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 94 | parts = r["content"].split(" ||| ") |
Prateeksha Singh | b44ea4c | 2018-05-22 11:57:21 +0530 | [diff] [blame] | 95 | |
| 96 | for part in parts: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 97 | pair = part.split(" : ", 1) |
Prateeksha Singh | b44ea4c | 2018-05-22 11:57:21 +0530 | [diff] [blame] | 98 | prepared_result[pair[0]] = pair[1] |
| 99 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 100 | results.append( |
| 101 | frappe._dict( |
| 102 | { |
| 103 | "title": prepared_result[api.result_title_field], |
| 104 | "preview": prepared_result[api.result_preview_field], |
| 105 | "route": prepared_result[api.result_route_field], |
| 106 | } |
| 107 | ) |
| 108 | ) |
Prateeksha Singh | b44ea4c | 2018-05-22 11:57:21 +0530 | [diff] [blame] | 109 | |
| 110 | return results |