blob: aa00e9288041f89064c0fd79fb9a4dd9dbcf3f83 [file] [log] [blame]
Afshan8546b712020-06-17 16:16:59 +05301import frappe
2
Chillar Anand915b3432021-09-02 16:44:59 +05303
Afshan8546b712020-06-17 16:16:59 +05304def get_context(context):
5 context.no_cache = 1
Ankush Menat494bd9e2022-03-28 18:52:46 +05306 context.align_greeting = ""
Afshan8546b712020-06-17 16:16:59 +05307 setting = frappe.get_doc("Support Settings")
8
9 context.greeting_title = setting.greeting_title
10 context.greeting_subtitle = setting.greeting_subtitle
Ankush Menat4551d7d2021-08-19 13:41:10 +053011
Afshan8546b712020-06-17 16:16:59 +053012 # Support content
13 favorite_articles = get_favorite_articles_by_page_view()
14 if len(favorite_articles) < 6:
15 name_list = []
16 if favorite_articles:
17 for article in favorite_articles:
18 name_list.append(article.name)
Ankush Menat494bd9e2022-03-28 18:52:46 +053019 for record in frappe.get_all(
20 "Help Article",
Ankush Menat4551d7d2021-08-19 13:41:10 +053021 fields=["title", "content", "route", "category"],
Ankush Menat494bd9e2022-03-28 18:52:46 +053022 filters={"name": ["not in", tuple(name_list)], "published": 1},
23 order_by="creation desc",
24 limit=(6 - len(favorite_articles)),
25 ):
Afshan8546b712020-06-17 16:16:59 +053026 favorite_articles.append(record)
Ankush Menat4551d7d2021-08-19 13:41:10 +053027
Afshan8546b712020-06-17 16:16:59 +053028 context.favorite_article_list = get_favorite_articles(favorite_articles)
29 context.help_article_list = get_help_article_list()
Ankush Menat4551d7d2021-08-19 13:41:10 +053030
Ankush Menat494bd9e2022-03-28 18:52:46 +053031
Afshan8546b712020-06-17 16:16:59 +053032def get_favorite_articles_by_page_view():
33 return frappe.db.sql(
Ankush Menat494bd9e2022-03-28 18:52:46 +053034 """
Afshan8546b712020-06-17 16:16:59 +053035 SELECT
36 t1.name as name,
37 t1.title as title,
38 t1.content as content,
39 t1.route as route,
40 t1.category as category,
Ankush Menat4551d7d2021-08-19 13:41:10 +053041 count(t1.route) as count
42 FROM `tabHelp Article` AS t1
Afshan8546b712020-06-17 16:16:59 +053043 INNER JOIN
Ankush Menat4551d7d2021-08-19 13:41:10 +053044 `tabWeb Page View` AS t2
45 ON t1.route = t2.path
Afshan8546b712020-06-17 16:16:59 +053046 WHERE t1.published = 1
Ankush Menat4551d7d2021-08-19 13:41:10 +053047 GROUP BY route
Afshan8546b712020-06-17 16:16:59 +053048 ORDER BY count DESC
49 LIMIT 6;
Ankush Menat494bd9e2022-03-28 18:52:46 +053050 """,
51 as_dict=True,
52 )
53
Afshan8546b712020-06-17 16:16:59 +053054
55def get_favorite_articles(favorite_articles):
Ankush Menat494bd9e2022-03-28 18:52:46 +053056 favorite_article_list = []
Afshan8546b712020-06-17 16:16:59 +053057 for article in favorite_articles:
58 description = frappe.utils.strip_html(article.content)
Shivam Mishra64d77242020-06-17 13:18:15 +000059 if len(description) > 120:
Ankush Menat494bd9e2022-03-28 18:52:46 +053060 description = description[:120] + "..."
Afshan8546b712020-06-17 16:16:59 +053061 favorite_article_dict = {
Ankush Menat494bd9e2022-03-28 18:52:46 +053062 "title": article.title,
63 "description": description,
64 "route": article.route,
65 "category": article.category,
Afshan8546b712020-06-17 16:16:59 +053066 }
67 favorite_article_list.append(favorite_article_dict)
68 return favorite_article_list
69
Ankush Menat494bd9e2022-03-28 18:52:46 +053070
Afshan8546b712020-06-17 16:16:59 +053071def get_help_article_list():
Ankush Menat494bd9e2022-03-28 18:52:46 +053072 help_article_list = []
Afshan8546b712020-06-17 16:16:59 +053073 category_list = frappe.get_all("Help Category", fields="name")
74 for category in category_list:
Ankush Menat494bd9e2022-03-28 18:52:46 +053075 help_articles = frappe.get_all(
76 "Help Article",
77 fields="*",
78 filters={"category": category.name, "published": 1},
79 order_by="modified desc",
80 limit=5,
81 )
Afshan8546b712020-06-17 16:16:59 +053082 if help_articles:
83 help_aricles_per_caetgory = {
Ankush Menat494bd9e2022-03-28 18:52:46 +053084 "category": category,
85 "articles": help_articles,
Afshan8546b712020-06-17 16:16:59 +053086 }
87 help_article_list.append(help_aricles_per_caetgory)
Ankush Menat4551d7d2021-08-19 13:41:10 +053088 return help_article_list