blob: 70090c7805d9ba87d669bbfcacc6b62e7b2ba9a7 [file] [log] [blame]
Afshan8546b712020-06-17 16:16:59 +05301from __future__ import unicode_literals
2import frappe
3
4def get_context(context):
5 context.no_cache = 1
6 context.align_greeting = ''
7 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 Menat4551d7d2021-08-19 13:41:10 +053019 for record in (frappe.get_all("Help Article",
20 fields=["title", "content", "route", "category"],
21 filters={"name": ['not in', tuple(name_list)], "published": 1},
Afshan8546b712020-06-17 16:16:59 +053022 order_by="creation desc", limit=(6-len(favorite_articles)))):
23 favorite_articles.append(record)
Ankush Menat4551d7d2021-08-19 13:41:10 +053024
Afshan8546b712020-06-17 16:16:59 +053025 context.favorite_article_list = get_favorite_articles(favorite_articles)
26 context.help_article_list = get_help_article_list()
Ankush Menat4551d7d2021-08-19 13:41:10 +053027
Afshan8546b712020-06-17 16:16:59 +053028def get_favorite_articles_by_page_view():
29 return frappe.db.sql(
30 """
31 SELECT
32 t1.name as name,
33 t1.title as title,
34 t1.content as content,
35 t1.route as route,
36 t1.category as category,
Ankush Menat4551d7d2021-08-19 13:41:10 +053037 count(t1.route) as count
38 FROM `tabHelp Article` AS t1
Afshan8546b712020-06-17 16:16:59 +053039 INNER JOIN
Ankush Menat4551d7d2021-08-19 13:41:10 +053040 `tabWeb Page View` AS t2
41 ON t1.route = t2.path
Afshan8546b712020-06-17 16:16:59 +053042 WHERE t1.published = 1
Ankush Menat4551d7d2021-08-19 13:41:10 +053043 GROUP BY route
Afshan8546b712020-06-17 16:16:59 +053044 ORDER BY count DESC
45 LIMIT 6;
46 """, as_dict=True)
47
48def get_favorite_articles(favorite_articles):
49 favorite_article_list=[]
50 for article in favorite_articles:
51 description = frappe.utils.strip_html(article.content)
Shivam Mishra64d77242020-06-17 13:18:15 +000052 if len(description) > 120:
53 description = description[:120] + '...'
Afshan8546b712020-06-17 16:16:59 +053054 favorite_article_dict = {
55 'title': article.title,
56 'description': description,
57 'route': article.route,
58 'category': article.category,
59 }
60 favorite_article_list.append(favorite_article_dict)
61 return favorite_article_list
62
63def get_help_article_list():
64 help_article_list=[]
65 category_list = frappe.get_all("Help Category", fields="name")
66 for category in category_list:
67 help_articles = frappe.get_all("Help Article", fields="*", filters={"category": category.name, "published": 1}, order_by="modified desc", limit=5)
68 if help_articles:
69 help_aricles_per_caetgory = {
70 'category': category,
71 'articles': help_articles,
72 }
73 help_article_list.append(help_aricles_per_caetgory)
Ankush Menat4551d7d2021-08-19 13:41:10 +053074 return help_article_list