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): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 50 | response = requests.get( |
| 51 | api.base_url + "/" + api.query_route, data={api.search_term_param_name: text} |
| 52 | ) |
Prateeksha Singh | b44ea4c | 2018-05-22 11:57:21 +0530 | [diff] [blame] | 53 | |
| 54 | response.raise_for_status() |
| 55 | return response.json() |
| 56 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 57 | |
Prateeksha Singh | b44ea4c | 2018-05-22 11:57:21 +0530 | [diff] [blame] | 58 | def get_topics_data(api, response_json): |
| 59 | if not response_json: |
| 60 | response_json = {} |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 61 | topics_data = {} # it will actually be an array |
| 62 | key_list = api.response_result_key_path.split(",") |
Prateeksha Singh | b44ea4c | 2018-05-22 11:57:21 +0530 | [diff] [blame] | 63 | |
| 64 | for key in key_list: |
| 65 | topics_data = response_json.get(key) if not topics_data else topics_data.get(key) |
| 66 | |
| 67 | return topics_data or [] |
| 68 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 69 | |
Prateeksha Singh | b44ea4c | 2018-05-22 11:57:21 +0530 | [diff] [blame] | 70 | def prepare_api_results(api, topics_data): |
| 71 | if not topics_data: |
| 72 | topics_data = [] |
| 73 | |
| 74 | results = [] |
| 75 | for topic in topics_data: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 76 | route = api.base_url + "/" + (api.post_route + "/" if api.post_route else "") |
| 77 | for key in api.post_route_key_list.split(","): |
Ankush Menat | 8fe5feb | 2021-11-04 19:48:32 +0530 | [diff] [blame] | 78 | route += str(topic[key]) |
Prateeksha Singh | b44ea4c | 2018-05-22 11:57:21 +0530 | [diff] [blame] | 79 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 80 | results.append( |
| 81 | frappe._dict( |
| 82 | { |
| 83 | "title": topic[api.post_title_key], |
| 84 | "preview": html2text(topic[api.post_description_key]), |
| 85 | "route": route, |
| 86 | } |
| 87 | ) |
| 88 | ) |
Prateeksha Singh | b44ea4c | 2018-05-22 11:57:21 +0530 | [diff] [blame] | 89 | return results[:5] |
| 90 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 91 | |
Prateeksha Singh | b44ea4c | 2018-05-22 11:57:21 +0530 | [diff] [blame] | 92 | def prepare_doctype_results(api, raw): |
| 93 | results = [] |
| 94 | for r in raw: |
| 95 | prepared_result = {} |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 96 | parts = r["content"].split(" ||| ") |
Prateeksha Singh | b44ea4c | 2018-05-22 11:57:21 +0530 | [diff] [blame] | 97 | |
| 98 | for part in parts: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 99 | pair = part.split(" : ", 1) |
Prateeksha Singh | b44ea4c | 2018-05-22 11:57:21 +0530 | [diff] [blame] | 100 | prepared_result[pair[0]] = pair[1] |
| 101 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 102 | results.append( |
| 103 | frappe._dict( |
| 104 | { |
| 105 | "title": prepared_result[api.result_title_field], |
| 106 | "preview": prepared_result[api.result_preview_field], |
| 107 | "route": prepared_result[api.result_route_field], |
| 108 | } |
| 109 | ) |
| 110 | ) |
Prateeksha Singh | b44ea4c | 2018-05-22 11:57:21 +0530 | [diff] [blame] | 111 | |
| 112 | return results |