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