blob: 18a0a67d0c88dee095a26b2486c74a76dc5cdffa [file] [log] [blame]
Chillar Anand915b3432021-09-02 16:44:59 +05301import frappe
2import requests
Prateeksha Singhb44ea4c2018-05-22 11:57:21 +05303from frappe import _
Suraj Shetty32fd7532022-07-06 15:09:44 +05304from frappe.core.utils import html2text
Prateeksha Singhb44ea4c2018-05-22 11:57:21 +05305from frappe.utils import sanitize_html
6from frappe.utils.global_search import search
Chillar Anand915b3432021-09-02 16:44:59 +05307from jinja2 import utils
Chillar Anand915b3432021-09-02 16:44:59 +05308
Prateeksha Singhb44ea4c2018-05-22 11:57:21 +05309
10def 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 Menat494bd9e2022-03-28 18:52:46 +053014 context.title = _("Help Results for")
Prateeksha Singh0c796222018-08-06 12:33:34 +053015 context.query = query
Aditya Hase691c1662019-02-07 20:35:52 +053016
Ankush Menat494bd9e2022-03-28 18:52:46 +053017 context.route = "/search_help"
Prateeksha Singhb44ea4c2018-05-22 11:57:21 +053018 d = frappe._dict()
19 d.results_sections = get_help_results_sections(query)
20 context.update(d)
21 else:
Ankush Menat494bd9e2022-03-28 18:52:46 +053022 context.title = _("Docs Search")
Prateeksha Singhb44ea4c2018-05-22 11:57:21 +053023
Ankush Menat494bd9e2022-03-28 18:52:46 +053024
25@frappe.whitelist(allow_guest=True)
Prateeksha Singhb44ea4c2018-05-22 11:57:21 +053026def 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 Menat494bd9e2022-03-28 18:52:46 +053044 out.append({"title": api.source_name, "results": results})
Prateeksha Singhb44ea4c2018-05-22 11:57:21 +053045
46 return out
47
Ankush Menat494bd9e2022-03-28 18:52:46 +053048
Prateeksha Singhb44ea4c2018-05-22 11:57:21 +053049def get_response(api, text):
Akhil Narang3effaf22024-03-27 11:37:26 +053050 response = requests.get(api.base_url + "/" + api.query_route, data={api.search_term_param_name: text})
Prateeksha Singhb44ea4c2018-05-22 11:57:21 +053051
52 response.raise_for_status()
53 return response.json()
54
Ankush Menat494bd9e2022-03-28 18:52:46 +053055
Prateeksha Singhb44ea4c2018-05-22 11:57:21 +053056def get_topics_data(api, response_json):
57 if not response_json:
58 response_json = {}
Ankush Menat494bd9e2022-03-28 18:52:46 +053059 topics_data = {} # it will actually be an array
60 key_list = api.response_result_key_path.split(",")
Prateeksha Singhb44ea4c2018-05-22 11:57:21 +053061
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 Menat494bd9e2022-03-28 18:52:46 +053067
Prateeksha Singhb44ea4c2018-05-22 11:57:21 +053068def prepare_api_results(api, topics_data):
69 if not topics_data:
70 topics_data = []
71
72 results = []
73 for topic in topics_data:
Ankush Menat494bd9e2022-03-28 18:52:46 +053074 route = api.base_url + "/" + (api.post_route + "/" if api.post_route else "")
75 for key in api.post_route_key_list.split(","):
Ankush Menat8fe5feb2021-11-04 19:48:32 +053076 route += str(topic[key])
Prateeksha Singhb44ea4c2018-05-22 11:57:21 +053077
Ankush Menat494bd9e2022-03-28 18:52:46 +053078 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 Singhb44ea4c2018-05-22 11:57:21 +053087 return results[:5]
88
Ankush Menat494bd9e2022-03-28 18:52:46 +053089
Prateeksha Singhb44ea4c2018-05-22 11:57:21 +053090def prepare_doctype_results(api, raw):
91 results = []
92 for r in raw:
93 prepared_result = {}
Ankush Menat494bd9e2022-03-28 18:52:46 +053094 parts = r["content"].split(" ||| ")
Prateeksha Singhb44ea4c2018-05-22 11:57:21 +053095
96 for part in parts:
Ankush Menat494bd9e2022-03-28 18:52:46 +053097 pair = part.split(" : ", 1)
Prateeksha Singhb44ea4c2018-05-22 11:57:21 +053098 prepared_result[pair[0]] = pair[1]
99
Ankush Menat494bd9e2022-03-28 18:52:46 +0530100 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 Singhb44ea4c2018-05-22 11:57:21 +0530109
110 return results