blob: 4857b0d829d9196bcd43f87413a4db17bd1a5649 [file] [log] [blame]
Afshan8546b712020-06-17 16:16:59 +05301from __future__ import unicode_literals
Chillar Anand915b3432021-09-02 16:44:59 +05302
Afshan8546b712020-06-17 16:16:59 +05303import frappe
4
Chillar Anand915b3432021-09-02 16:44:59 +05305
Afshan8546b712020-06-17 16:16:59 +05306def get_context(context):
7 context.no_cache = 1
8 context.align_greeting = ''
9 setting = frappe.get_doc("Support Settings")
10
11 context.greeting_title = setting.greeting_title
12 context.greeting_subtitle = setting.greeting_subtitle
Ankush Menat4551d7d2021-08-19 13:41:10 +053013
Afshan8546b712020-06-17 16:16:59 +053014 # Support content
15 favorite_articles = get_favorite_articles_by_page_view()
16 if len(favorite_articles) < 6:
17 name_list = []
18 if favorite_articles:
19 for article in favorite_articles:
20 name_list.append(article.name)
Ankush Menat4551d7d2021-08-19 13:41:10 +053021 for record in (frappe.get_all("Help Article",
22 fields=["title", "content", "route", "category"],
23 filters={"name": ['not in', tuple(name_list)], "published": 1},
Afshan8546b712020-06-17 16:16:59 +053024 order_by="creation desc", limit=(6-len(favorite_articles)))):
25 favorite_articles.append(record)
Ankush Menat4551d7d2021-08-19 13:41:10 +053026
Afshan8546b712020-06-17 16:16:59 +053027 context.favorite_article_list = get_favorite_articles(favorite_articles)
28 context.help_article_list = get_help_article_list()
Ankush Menat4551d7d2021-08-19 13:41:10 +053029
Afshan8546b712020-06-17 16:16:59 +053030def get_favorite_articles_by_page_view():
31 return frappe.db.sql(
32 """
33 SELECT
34 t1.name as name,
35 t1.title as title,
36 t1.content as content,
37 t1.route as route,
38 t1.category as category,
Ankush Menat4551d7d2021-08-19 13:41:10 +053039 count(t1.route) as count
40 FROM `tabHelp Article` AS t1
Afshan8546b712020-06-17 16:16:59 +053041 INNER JOIN
Ankush Menat4551d7d2021-08-19 13:41:10 +053042 `tabWeb Page View` AS t2
43 ON t1.route = t2.path
Afshan8546b712020-06-17 16:16:59 +053044 WHERE t1.published = 1
Ankush Menat4551d7d2021-08-19 13:41:10 +053045 GROUP BY route
Afshan8546b712020-06-17 16:16:59 +053046 ORDER BY count DESC
47 LIMIT 6;
48 """, as_dict=True)
49
50def get_favorite_articles(favorite_articles):
51 favorite_article_list=[]
52 for article in favorite_articles:
53 description = frappe.utils.strip_html(article.content)
Shivam Mishra64d77242020-06-17 13:18:15 +000054 if len(description) > 120:
55 description = description[:120] + '...'
Afshan8546b712020-06-17 16:16:59 +053056 favorite_article_dict = {
57 'title': article.title,
58 'description': description,
59 'route': article.route,
60 'category': article.category,
61 }
62 favorite_article_list.append(favorite_article_dict)
63 return favorite_article_list
64
65def get_help_article_list():
66 help_article_list=[]
67 category_list = frappe.get_all("Help Category", fields="name")
68 for category in category_list:
69 help_articles = frappe.get_all("Help Article", fields="*", filters={"category": category.name, "published": 1}, order_by="modified desc", limit=5)
70 if help_articles:
71 help_aricles_per_caetgory = {
72 'category': category,
73 'articles': help_articles,
74 }
75 help_article_list.append(help_aricles_per_caetgory)
Ankush Menat4551d7d2021-08-19 13:41:10 +053076 return help_article_list