Prateeksha Singh | b44ea4c | 2018-05-22 11:57:21 +0530 | [diff] [blame] | 1 | from __future__ import unicode_literals |
| 2 | import frappe, json |
| 3 | |
| 4 | import requests |
| 5 | |
| 6 | def 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 |
Prateeksha Singh | 8d58429 | 2018-06-12 17:39:12 +0530 | [diff] [blame] | 22 | if frappe.session.user != "Guest": |
| 23 | context.issues = frappe.get_list("Issue", fields=["name", "status", "subject", "modified"])[:3] |
| 24 | else: |
| 25 | context.issues = [] |
Prateeksha Singh | b44ea4c | 2018-05-22 11:57:21 +0530 | [diff] [blame] | 26 | |
| 27 | def get_forum_posts(s): |
| 28 | response = requests.get(s.forum_url + '/' + s.get_latest_query) |
| 29 | response.raise_for_status() |
| 30 | response_json = response.json() |
| 31 | |
| 32 | topics_data = {} # it will actually be an array |
| 33 | key_list = s.response_key_list.split(',') |
| 34 | for key in key_list: |
| 35 | topics_data = response_json.get(key) if not topics_data else topics_data.get(key) |
| 36 | |
| 37 | for topic in topics_data: |
| 38 | topic["link"] = s.forum_url + '/' + s.post_route_string + '/' + str(topic.get(s.post_route_key)) |
| 39 | |
| 40 | post_params = { |
| 41 | "title": s.post_title_key, |
| 42 | "description": s.post_description_key |
| 43 | } |
| 44 | return topics_data, post_params |