blob: 6a83fc8b57a754542a2a2c6a9c04bb489b366691 [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
28def 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