Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 1 | |
| 2 | import frappe |
| 3 | import requests |
Prateeksha Singh | b44ea4c | 2018-05-22 11:57:21 +0530 | [diff] [blame] | 4 | from frappe import _ |
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 html2text import html2text |
| 8 | from jinja2 import utils |
| 9 | from six import text_type |
| 10 | |
Prateeksha Singh | b44ea4c | 2018-05-22 11:57:21 +0530 | [diff] [blame] | 11 | |
| 12 | def 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 Singh | 0c79622 | 2018-08-06 12:33:34 +0530 | [diff] [blame] | 16 | context.title = _('Help Results for') |
| 17 | context.query = query |
Aditya Hase | 691c166 | 2019-02-07 20:35:52 +0530 | [diff] [blame] | 18 | |
Prateeksha Singh | b44ea4c | 2018-05-22 11:57:21 +0530 | [diff] [blame] | 19 | 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) |
| 27 | def 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 | |
| 52 | def 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 | |
| 60 | def 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 | |
| 71 | def 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 Hase | 691c166 | 2019-02-07 20:35:52 +0530 | [diff] [blame] | 79 | route += text_type(topic[key]) |
Prateeksha Singh | b44ea4c | 2018-05-22 11:57:21 +0530 | [diff] [blame] | 80 | |
| 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 | |
| 88 | def 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 |