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