blob: 754a09c30ceedf480b7e894fc1e3980c1b19fba9 [file] [log] [blame]
Prateeksha Singhb44ea4c2018-05-22 11:57:21 +05301from __future__ import unicode_literals
2import frappe, json
3
4import requests
5
6def get_context(context):
7 context.no_cache = 1
8 settings = frappe.get_doc("Support Settings", "Support Settings")
9 s = settings
10
11 # Get Started sections
12 sections = json.loads(s.get_started_sections)
13 context.get_started_sections = sections
14
15 # Forum posts
16 topics_data, post_params = get_forum_posts(s)
17 context.post_params = post_params
18 context.forum_url = s.forum_url
19 context.topics = topics_data[:3]
20
21 # Issues
22 context.issues = frappe.get_list("Issue")[:3]
23
24def get_forum_posts(s):
25 response = requests.get(s.forum_url + '/' + s.get_latest_query)
26 response.raise_for_status()
27 response_json = response.json()
28
29 topics_data = {} # it will actually be an array
30 key_list = s.response_key_list.split(',')
31 for key in key_list:
32 topics_data = response_json.get(key) if not topics_data else topics_data.get(key)
33
34 for topic in topics_data:
35 topic["link"] = s.forum_url + '/' + s.post_route_string + '/' + str(topic.get(s.post_route_key))
36
37 post_params = {
38 "title": s.post_title_key,
39 "description": s.post_description_key
40 }
41 return topics_data, post_params