Prateeksha Singh | b44ea4c | 2018-05-22 11:57:21 +0530 | [diff] [blame] | 1 | from __future__ import unicode_literals |
Prateeksha Singh | b44ea4c | 2018-05-22 11:57:21 +0530 | [diff] [blame] | 2 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 3 | import json |
| 4 | |
| 5 | import frappe |
Prateeksha Singh | b44ea4c | 2018-05-22 11:57:21 +0530 | [diff] [blame] | 6 | import requests |
| 7 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 8 | |
Prateeksha Singh | b44ea4c | 2018-05-22 11:57:21 +0530 | [diff] [blame] | 9 | def 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 Singh | 8d58429 | 2018-06-12 17:39:12 +0530 | [diff] [blame] | 25 | if frappe.session.user != "Guest": |
| 26 | context.issues = frappe.get_list("Issue", fields=["name", "status", "subject", "modified"])[:3] |
| 27 | else: |
| 28 | context.issues = [] |
Prateeksha Singh | b44ea4c | 2018-05-22 11:57:21 +0530 | [diff] [blame] | 29 | |
| 30 | def 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 |