blob: 366b2835e2ef0d1c0204a1233b823995d27abaab [file] [log] [blame]
Prateeksha Singhb44ea4c2018-05-22 11:57:21 +05301from __future__ import unicode_literals
Prateeksha Singhb44ea4c2018-05-22 11:57:21 +05302
Chillar Anand915b3432021-09-02 16:44:59 +05303import json
4
5import frappe
Prateeksha Singhb44ea4c2018-05-22 11:57:21 +05306import requests
7
Chillar Anand915b3432021-09-02 16:44:59 +05308
Prateeksha Singhb44ea4c2018-05-22 11:57:21 +05309def get_context(context):
10 context.no_cache = 1
11 settings = frappe.get_doc("Support Settings", "Support Settings")
12 s = settings
13
14 # Get Started sections
15 sections = json.loads(s.get_started_sections)
16 context.get_started_sections = sections
17
18 # Forum posts
19 topics_data, post_params = get_forum_posts(s)
20 context.post_params = post_params
21 context.forum_url = s.forum_url
22 context.topics = topics_data[:3]
23
24 # Issues
Prateeksha Singh8d584292018-06-12 17:39:12 +053025 if frappe.session.user != "Guest":
26 context.issues = frappe.get_list("Issue", fields=["name", "status", "subject", "modified"])[:3]
27 else:
28 context.issues = []
Prateeksha Singhb44ea4c2018-05-22 11:57:21 +053029
30def get_forum_posts(s):
31 response = requests.get(s.forum_url + '/' + s.get_latest_query)
32 response.raise_for_status()
33 response_json = response.json()
34
35 topics_data = {} # it will actually be an array
36 key_list = s.response_key_list.split(',')
37 for key in key_list:
38 topics_data = response_json.get(key) if not topics_data else topics_data.get(key)
39
40 for topic in topics_data:
41 topic["link"] = s.forum_url + '/' + s.post_route_string + '/' + str(topic.get(s.post_route_key))
42
43 post_params = {
44 "title": s.post_title_key,
45 "description": s.post_description_key
46 }
47 return topics_data, post_params