Afshan | 8546b71 | 2020-06-17 16:16:59 +0530 | [diff] [blame] | 1 | import frappe |
| 2 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 3 | |
Afshan | 8546b71 | 2020-06-17 16:16:59 +0530 | [diff] [blame] | 4 | def get_context(context): |
| 5 | context.no_cache = 1 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 6 | context.align_greeting = "" |
Afshan | 8546b71 | 2020-06-17 16:16:59 +0530 | [diff] [blame] | 7 | setting = frappe.get_doc("Support Settings") |
| 8 | |
| 9 | context.greeting_title = setting.greeting_title |
| 10 | context.greeting_subtitle = setting.greeting_subtitle |
Ankush Menat | 4551d7d | 2021-08-19 13:41:10 +0530 | [diff] [blame] | 11 | |
Afshan | 8546b71 | 2020-06-17 16:16:59 +0530 | [diff] [blame] | 12 | # 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 Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 19 | for record in frappe.get_all( |
| 20 | "Help Article", |
Ankush Menat | 4551d7d | 2021-08-19 13:41:10 +0530 | [diff] [blame] | 21 | fields=["title", "content", "route", "category"], |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 22 | filters={"name": ["not in", tuple(name_list)], "published": 1}, |
| 23 | order_by="creation desc", |
| 24 | limit=(6 - len(favorite_articles)), |
| 25 | ): |
Afshan | 8546b71 | 2020-06-17 16:16:59 +0530 | [diff] [blame] | 26 | favorite_articles.append(record) |
Ankush Menat | 4551d7d | 2021-08-19 13:41:10 +0530 | [diff] [blame] | 27 | |
Afshan | 8546b71 | 2020-06-17 16:16:59 +0530 | [diff] [blame] | 28 | context.favorite_article_list = get_favorite_articles(favorite_articles) |
| 29 | context.help_article_list = get_help_article_list() |
Ankush Menat | 4551d7d | 2021-08-19 13:41:10 +0530 | [diff] [blame] | 30 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 31 | |
Afshan | 8546b71 | 2020-06-17 16:16:59 +0530 | [diff] [blame] | 32 | def get_favorite_articles_by_page_view(): |
| 33 | return frappe.db.sql( |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 34 | """ |
Afshan | 8546b71 | 2020-06-17 16:16:59 +0530 | [diff] [blame] | 35 | 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 Menat | 4551d7d | 2021-08-19 13:41:10 +0530 | [diff] [blame] | 41 | count(t1.route) as count |
| 42 | FROM `tabHelp Article` AS t1 |
Afshan | 8546b71 | 2020-06-17 16:16:59 +0530 | [diff] [blame] | 43 | INNER JOIN |
Ankush Menat | 4551d7d | 2021-08-19 13:41:10 +0530 | [diff] [blame] | 44 | `tabWeb Page View` AS t2 |
| 45 | ON t1.route = t2.path |
Afshan | 8546b71 | 2020-06-17 16:16:59 +0530 | [diff] [blame] | 46 | WHERE t1.published = 1 |
Ankush Menat | 4551d7d | 2021-08-19 13:41:10 +0530 | [diff] [blame] | 47 | GROUP BY route |
Afshan | 8546b71 | 2020-06-17 16:16:59 +0530 | [diff] [blame] | 48 | ORDER BY count DESC |
| 49 | LIMIT 6; |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 50 | """, |
| 51 | as_dict=True, |
| 52 | ) |
| 53 | |
Afshan | 8546b71 | 2020-06-17 16:16:59 +0530 | [diff] [blame] | 54 | |
| 55 | def get_favorite_articles(favorite_articles): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 56 | favorite_article_list = [] |
Afshan | 8546b71 | 2020-06-17 16:16:59 +0530 | [diff] [blame] | 57 | for article in favorite_articles: |
| 58 | description = frappe.utils.strip_html(article.content) |
Shivam Mishra | 64d7724 | 2020-06-17 13:18:15 +0000 | [diff] [blame] | 59 | if len(description) > 120: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 60 | description = description[:120] + "..." |
Afshan | 8546b71 | 2020-06-17 16:16:59 +0530 | [diff] [blame] | 61 | favorite_article_dict = { |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 62 | "title": article.title, |
| 63 | "description": description, |
| 64 | "route": article.route, |
| 65 | "category": article.category, |
Afshan | 8546b71 | 2020-06-17 16:16:59 +0530 | [diff] [blame] | 66 | } |
| 67 | favorite_article_list.append(favorite_article_dict) |
| 68 | return favorite_article_list |
| 69 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 70 | |
Afshan | 8546b71 | 2020-06-17 16:16:59 +0530 | [diff] [blame] | 71 | def get_help_article_list(): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 72 | help_article_list = [] |
Afshan | 8546b71 | 2020-06-17 16:16:59 +0530 | [diff] [blame] | 73 | category_list = frappe.get_all("Help Category", fields="name") |
| 74 | for category in category_list: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 75 | help_articles = frappe.get_all( |
| 76 | "Help Article", |
| 77 | fields="*", |
| 78 | filters={"category": category.name, "published": 1}, |
Ankush Menat | c2470cc | 2024-03-27 14:18:01 +0530 | [diff] [blame] | 79 | order_by="creation desc", |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 80 | limit=5, |
| 81 | ) |
Afshan | 8546b71 | 2020-06-17 16:16:59 +0530 | [diff] [blame] | 82 | if help_articles: |
| 83 | help_aricles_per_caetgory = { |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 84 | "category": category, |
| 85 | "articles": help_articles, |
Afshan | 8546b71 | 2020-06-17 16:16:59 +0530 | [diff] [blame] | 86 | } |
| 87 | help_article_list.append(help_aricles_per_caetgory) |
Ankush Menat | 4551d7d | 2021-08-19 13:41:10 +0530 | [diff] [blame] | 88 | return help_article_list |