Afshan | 8546b71 | 2020-06-17 16:16:59 +0530 | [diff] [blame] | 1 | from __future__ import unicode_literals |
| 2 | import frappe |
| 3 | |
| 4 | def 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 |
| 11 | |
| 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) |
| 19 | for record in (frappe.get_all("Help Article", |
| 20 | fields=["title", "content", "route", "category"], |
| 21 | filters={"name": ['not in', tuple(name_list)], "published": 1}, |
| 22 | order_by="creation desc", limit=(6-len(favorite_articles)))): |
| 23 | favorite_articles.append(record) |
| 24 | |
| 25 | context.favorite_article_list = get_favorite_articles(favorite_articles) |
| 26 | context.help_article_list = get_help_article_list() |
| 27 | |
| 28 | def 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, |
| 37 | count(t1.route) as count |
| 38 | FROM `tabHelp Article` AS t1 |
| 39 | INNER JOIN |
| 40 | `tabWeb Page View` AS t2 |
| 41 | ON t1.route = t2.path |
| 42 | WHERE t1.published = 1 |
| 43 | GROUP BY route |
| 44 | ORDER BY count DESC |
| 45 | LIMIT 6; |
| 46 | """, as_dict=True) |
| 47 | |
| 48 | def get_favorite_articles(favorite_articles): |
| 49 | favorite_article_list=[] |
| 50 | for article in favorite_articles: |
| 51 | description = frappe.utils.strip_html(article.content) |
Shivam Mishra | 64d7724 | 2020-06-17 13:18:15 +0000 | [diff] [blame] | 52 | if len(description) > 120: |
| 53 | description = description[:120] + '...' |
Afshan | 8546b71 | 2020-06-17 16:16:59 +0530 | [diff] [blame] | 54 | 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 | |
| 63 | def 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) |
| 74 | return help_article_list |