blob: c8854d74901cc8947f939412a0e138992ae16d1f [file] [log] [blame]
Chillar Anand915b3432021-09-02 16:44:59 +05301
2import frappe
3import requests
Prateeksha Singhb44ea4c2018-05-22 11:57:21 +05304from frappe import _
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 html2text import html2text
8from jinja2 import utils
9from six import text_type
10
Prateeksha Singhb44ea4c2018-05-22 11:57:21 +053011
12def get_context(context):
13 context.no_cache = 1
14 if frappe.form_dict.q:
15 query = str(utils.escape(sanitize_html(frappe.form_dict.q)))
Prateeksha Singh0c796222018-08-06 12:33:34 +053016 context.title = _('Help Results for')
17 context.query = query
Aditya Hase691c1662019-02-07 20:35:52 +053018
Prateeksha Singhb44ea4c2018-05-22 11:57:21 +053019 context.route = '/search_help'
20 d = frappe._dict()
21 d.results_sections = get_help_results_sections(query)
22 context.update(d)
23 else:
24 context.title = _('Docs Search')
25
26@frappe.whitelist(allow_guest = True)
27def get_help_results_sections(text):
28 out = []
29 settings = frappe.get_doc("Support Settings", "Support Settings")
30
31 for api in settings.search_apis:
32 results = []
33 if api.source_type == "API":
34 response_json = get_response(api, text)
35 topics_data = get_topics_data(api, response_json)
36 results = prepare_api_results(api, topics_data)
37 else:
38 # Source type is Doctype
39 doctype = api.source_doctype
40 raw = search(text, 0, 20, doctype)
41 results = prepare_doctype_results(api, raw)
42
43 if results:
44 # Add section
45 out.append({
46 "title": api.source_name,
47 "results": results
48 })
49
50 return out
51
52def get_response(api, text):
53 response = requests.get(api.base_url + '/' + api.query_route, data={
54 api.search_term_param_name: text
55 })
56
57 response.raise_for_status()
58 return response.json()
59
60def get_topics_data(api, response_json):
61 if not response_json:
62 response_json = {}
63 topics_data = {} # it will actually be an array
64 key_list = api.response_result_key_path.split(',')
65
66 for key in key_list:
67 topics_data = response_json.get(key) if not topics_data else topics_data.get(key)
68
69 return topics_data or []
70
71def prepare_api_results(api, topics_data):
72 if not topics_data:
73 topics_data = []
74
75 results = []
76 for topic in topics_data:
77 route = api.base_url + '/' + (api.post_route + '/' if api.post_route else "")
78 for key in api.post_route_key_list.split(','):
Aditya Hase691c1662019-02-07 20:35:52 +053079 route += text_type(topic[key])
Prateeksha Singhb44ea4c2018-05-22 11:57:21 +053080
81 results.append(frappe._dict({
82 'title': topic[api.post_title_key],
83 'preview': html2text(topic[api.post_description_key]),
84 'route': route
85 }))
86 return results[:5]
87
88def prepare_doctype_results(api, raw):
89 results = []
90 for r in raw:
91 prepared_result = {}
92 parts = r["content"].split(' ||| ')
93
94 for part in parts:
95 pair = part.split(' : ', 1)
96 prepared_result[pair[0]] = pair[1]
97
98 results.append(frappe._dict({
99 'title': prepared_result[api.result_title_field],
100 'preview': prepared_result[api.result_preview_field],
101 'route': prepared_result[api.result_route_field]
102 }))
103
104 return results