blob: 25e7c2623db0432c60faaf71768f38e2d5750c9d [file] [log] [blame]
Prateeksha Singhb44ea4c2018-05-22 11:57:21 +05301
Chillar Anand915b3432021-09-02 16:44:59 +05302import json
3
4import frappe
Prateeksha Singhb44ea4c2018-05-22 11:57:21 +05305import requests
6
Chillar Anand915b3432021-09-02 16:44:59 +05307
Prateeksha Singhb44ea4c2018-05-22 11:57:21 +05308def get_context(context):
9 context.no_cache = 1
10 settings = frappe.get_doc("Support Settings", "Support Settings")
11 s = settings
12
13 # Get Started sections
14 sections = json.loads(s.get_started_sections)
15 context.get_started_sections = sections
16
17 # Forum posts
18 topics_data, post_params = get_forum_posts(s)
19 context.post_params = post_params
20 context.forum_url = s.forum_url
21 context.topics = topics_data[:3]
22
23 # Issues
Prateeksha Singh8d584292018-06-12 17:39:12 +053024 if frappe.session.user != "Guest":
25 context.issues = frappe.get_list("Issue", fields=["name", "status", "subject", "modified"])[:3]
26 else:
27 context.issues = []
Prateeksha Singhb44ea4c2018-05-22 11:57:21 +053028
29def get_forum_posts(s):
30 response = requests.get(s.forum_url + '/' + s.get_latest_query)
31 response.raise_for_status()
32 response_json = response.json()
33
34 topics_data = {} # it will actually be an array
35 key_list = s.response_key_list.split(',')
36 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:
40 topic["link"] = s.forum_url + '/' + s.post_route_string + '/' + str(topic.get(s.post_route_key))
41
42 post_params = {
43 "title": s.post_title_key,
44 "description": s.post_description_key
45 }
46 return topics_data, post_params