Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 1 | import json |
| 2 | |
| 3 | import frappe |
Prateeksha Singh | b44ea4c | 2018-05-22 11:57:21 +0530 | [diff] [blame] | 4 | import requests |
| 5 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 6 | |
Prateeksha Singh | b44ea4c | 2018-05-22 11:57:21 +0530 | [diff] [blame] | 7 | def get_context(context): |
| 8 | context.no_cache = 1 |
| 9 | settings = frappe.get_doc("Support Settings", "Support Settings") |
| 10 | s = settings |
| 11 | |
| 12 | # Get Started sections |
| 13 | sections = json.loads(s.get_started_sections) |
| 14 | context.get_started_sections = sections |
| 15 | |
| 16 | # Forum posts |
| 17 | topics_data, post_params = get_forum_posts(s) |
| 18 | context.post_params = post_params |
| 19 | context.forum_url = s.forum_url |
| 20 | context.topics = topics_data[:3] |
| 21 | |
| 22 | # Issues |
Prateeksha Singh | 8d58429 | 2018-06-12 17:39:12 +0530 | [diff] [blame] | 23 | if frappe.session.user != "Guest": |
| 24 | context.issues = frappe.get_list("Issue", fields=["name", "status", "subject", "modified"])[:3] |
| 25 | else: |
| 26 | context.issues = [] |
Prateeksha Singh | b44ea4c | 2018-05-22 11:57:21 +0530 | [diff] [blame] | 27 | |
| 28 | def get_forum_posts(s): |
| 29 | response = requests.get(s.forum_url + '/' + s.get_latest_query) |
| 30 | response.raise_for_status() |
| 31 | response_json = response.json() |
| 32 | |
| 33 | topics_data = {} # it will actually be an array |
| 34 | key_list = s.response_key_list.split(',') |
| 35 | for key in key_list: |
| 36 | topics_data = response_json.get(key) if not topics_data else topics_data.get(key) |
| 37 | |
| 38 | for topic in topics_data: |
| 39 | topic["link"] = s.forum_url + '/' + s.post_route_string + '/' + str(topic.get(s.post_route_key)) |
| 40 | |
| 41 | post_params = { |
| 42 | "title": s.post_title_key, |
| 43 | "description": s.post_description_key |
| 44 | } |
| 45 | return topics_data, post_params |