blob: 19993ee9b1358fc267cc00276acdd7be13e8b6e6 [file] [log] [blame]
Chillar Anand915b3432021-09-02 16:44:59 +05301import json
2
3import frappe
Prateeksha Singhb44ea4c2018-05-22 11:57:21 +05304import requests
5
Chillar Anand915b3432021-09-02 16:44:59 +05306
Prateeksha Singhb44ea4c2018-05-22 11:57:21 +05307def 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 Singh8d584292018-06-12 17:39:12 +053023 if frappe.session.user != "Guest":
24 context.issues = frappe.get_list("Issue", fields=["name", "status", "subject", "modified"])[:3]
25 else:
26 context.issues = []
Prateeksha Singhb44ea4c2018-05-22 11:57:21 +053027
Ankush Menat494bd9e2022-03-28 18:52:46 +053028
Prateeksha Singhb44ea4c2018-05-22 11:57:21 +053029def get_forum_posts(s):
Ankush Menat494bd9e2022-03-28 18:52:46 +053030 response = requests.get(s.forum_url + "/" + s.get_latest_query)
Prateeksha Singhb44ea4c2018-05-22 11:57:21 +053031 response.raise_for_status()
32 response_json = response.json()
33
Ankush Menat494bd9e2022-03-28 18:52:46 +053034 topics_data = {} # it will actually be an array
35 key_list = s.response_key_list.split(",")
Prateeksha Singhb44ea4c2018-05-22 11:57:21 +053036 for key in key_list:
37 topics_data = response_json.get(key) if not topics_data else topics_data.get(key)
38
39 for topic in topics_data:
Ankush Menat494bd9e2022-03-28 18:52:46 +053040 topic["link"] = s.forum_url + "/" + s.post_route_string + "/" + str(topic.get(s.post_route_key))
Prateeksha Singhb44ea4c2018-05-22 11:57:21 +053041
Ankush Menat494bd9e2022-03-28 18:52:46 +053042 post_params = {"title": s.post_title_key, "description": s.post_description_key}
Prateeksha Singhb44ea4c2018-05-22 11:57:21 +053043 return topics_data, post_params