Rushabh Mehta | c7dbe29 | 2012-08-07 12:12:55 +0530 | [diff] [blame] | 1 | from __future__ import unicode_literals |
Anand Doshi | 82042f1 | 2012-04-06 17:54:17 +0530 | [diff] [blame] | 2 | import webnotes |
| 3 | |
| 4 | def get_unread_messages(): |
| 5 | "returns unread (docstatus-0 messages for a user)" |
| 6 | return webnotes.conn.sql("""\ |
| 7 | SELECT name, comment |
| 8 | FROM `tabComment` |
| 9 | WHERE comment_doctype IN ('My Company', 'Message') |
| 10 | AND comment_docname = %s |
| 11 | AND ifnull(docstatus,0)=0 |
| 12 | """, webnotes.user.name, as_list=1) |
| 13 | |
| 14 | def get_open_support_tickets(): |
Rushabh Mehta | c7dbe29 | 2012-08-07 12:12:55 +0530 | [diff] [blame] | 15 | """Returns a count of open support tickets""" |
Anand Doshi | 82042f1 | 2012-04-06 17:54:17 +0530 | [diff] [blame] | 16 | from webnotes.utils import cint |
| 17 | open_support_tickets = webnotes.conn.sql("""\ |
| 18 | SELECT COUNT(*) FROM `tabSupport Ticket` |
| 19 | WHERE status = 'Open'""") |
| 20 | return open_support_tickets and cint(open_support_tickets[0][0]) or 0 |
| 21 | |
Rushabh Mehta | 07c94ce | 2012-06-04 12:57:23 +0530 | [diff] [blame] | 22 | def get_open_tasks(): |
Rushabh Mehta | c7dbe29 | 2012-08-07 12:12:55 +0530 | [diff] [blame] | 23 | """Returns a count of open tasks""" |
Rushabh Mehta | 07c94ce | 2012-06-04 12:57:23 +0530 | [diff] [blame] | 24 | from webnotes.utils import cint |
| 25 | return webnotes.conn.sql("""\ |
| 26 | SELECT COUNT(*) FROM `tabTask` |
| 27 | WHERE status = 'Open'""")[0][0] |
| 28 | |
Anand Doshi | 82042f1 | 2012-04-06 17:54:17 +0530 | [diff] [blame] | 29 | def get_things_todo(): |
Rushabh Mehta | c7dbe29 | 2012-08-07 12:12:55 +0530 | [diff] [blame] | 30 | """Returns a count of incomplete todos""" |
Anand Doshi | 82042f1 | 2012-04-06 17:54:17 +0530 | [diff] [blame] | 31 | from webnotes.utils import cint |
| 32 | incomplete_todos = webnotes.conn.sql("""\ |
| 33 | SELECT COUNT(*) FROM `tabToDo` |
| 34 | WHERE IFNULL(checked, 0) = 0 |
| 35 | AND owner = %s""", webnotes.session.get('user')) |
| 36 | return incomplete_todos and cint(incomplete_todos[0][0]) or 0 |
| 37 | |
| 38 | def get_todays_events(): |
Rushabh Mehta | c7dbe29 | 2012-08-07 12:12:55 +0530 | [diff] [blame] | 39 | """Returns a count of todays events in calendar""" |
Anand Doshi | 82042f1 | 2012-04-06 17:54:17 +0530 | [diff] [blame] | 40 | from webnotes.utils import nowdate, cint |
| 41 | todays_events = webnotes.conn.sql("""\ |
| 42 | SELECT COUNT(*) FROM `tabEvent` |
| 43 | WHERE owner = %s |
| 44 | AND event_type != 'Cancel' |
| 45 | AND event_date = %s""", ( |
| 46 | webnotes.session.get('user'), nowdate())) |
| 47 | return todays_events and cint(todays_events[0][0]) or 0 |
| 48 | |
Rushabh Mehta | c7dbe29 | 2012-08-07 12:12:55 +0530 | [diff] [blame] | 49 | def get_unanswered_questions(): |
| 50 | return len(filter(lambda d: d[0]==0, |
| 51 | webnotes.conn.sql("""select (select count(*) from tabAnswer |
| 52 | where tabAnswer.question = tabQuestion.name) as answers from tabQuestion"""))) |
| 53 | |
Anand Doshi | 82042f1 | 2012-04-06 17:54:17 +0530 | [diff] [blame] | 54 | @webnotes.whitelist() |
| 55 | def get_global_status_messages(arg=None): |
| 56 | return { |
| 57 | 'unread_messages': get_unread_messages(), |
| 58 | 'open_support_tickets': get_open_support_tickets(), |
| 59 | 'things_todo': get_things_todo(), |
| 60 | 'todays_events': get_todays_events(), |
Rushabh Mehta | c7dbe29 | 2012-08-07 12:12:55 +0530 | [diff] [blame] | 61 | 'open_tasks': get_open_tasks(), |
| 62 | 'unanswered_questions': get_unanswered_questions() |
Anand Doshi | 82042f1 | 2012-04-06 17:54:17 +0530 | [diff] [blame] | 63 | } |