Rushabh Mehta | aaf86ba | 2012-02-28 17:40:13 +0530 | [diff] [blame] | 1 | # ERPNext - web based ERP (http://erpnext.com) |
| 2 | # Copyright (C) 2012 Web Notes Technologies Pvt Ltd |
| 3 | # |
| 4 | # This program is free software: you can redistribute it and/or modify |
| 5 | # it under the terms of the GNU General Public License as published by |
| 6 | # the Free Software Foundation, either version 3 of the License, or |
| 7 | # (at your option) any later version. |
| 8 | # |
| 9 | # This program is distributed in the hope that it will be useful, |
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | # GNU General Public License for more details. |
| 13 | # |
| 14 | # You should have received a copy of the GNU General Public License |
| 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | |
Anand Doshi | 486f9df | 2012-07-19 13:40:31 +0530 | [diff] [blame] | 17 | from __future__ import unicode_literals |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 18 | import webnotes |
| 19 | |
| 20 | @webnotes.whitelist() |
| 21 | def get_list(arg=None): |
| 22 | """get list of messages""" |
| 23 | webnotes.form_dict['limit_start'] = int(webnotes.form_dict['limit_start']) |
| 24 | webnotes.form_dict['limit_page_length'] = int(webnotes.form_dict['limit_page_length']) |
| 25 | webnotes.form_dict['user'] = webnotes.session['user'] |
| 26 | |
Rushabh Mehta | 833f070 | 2012-12-03 11:33:08 +0530 | [diff] [blame] | 27 | # set all messages as read |
Rushabh Mehta | 4f8dfd6 | 2013-02-03 22:42:05 +0530 | [diff] [blame] | 28 | webnotes.conn.begin() |
Rushabh Mehta | 833f070 | 2012-12-03 11:33:08 +0530 | [diff] [blame] | 29 | webnotes.conn.sql("""UPDATE `tabComment` |
| 30 | set docstatus = 1 where comment_doctype in ('My Company', 'Message') |
| 31 | and comment_docname = %s |
| 32 | """, webnotes.user.name) |
Rushabh Mehta | 4f8dfd6 | 2013-02-03 22:42:05 +0530 | [diff] [blame] | 33 | webnotes.conn.commit() |
Rushabh Mehta | 833f070 | 2012-12-03 11:33:08 +0530 | [diff] [blame] | 34 | |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 35 | if webnotes.form_dict['contact'] == webnotes.session['user']: |
Rushabh Mehta | 07a36a4 | 2012-02-27 18:17:57 +0530 | [diff] [blame] | 36 | # return messages |
Anand Doshi | fedfd89 | 2012-03-30 12:29:06 +0530 | [diff] [blame] | 37 | return webnotes.conn.sql("""select * from `tabComment` |
Rushabh Mehta | b62bbc6 | 2012-10-03 18:32:10 +0530 | [diff] [blame] | 38 | where (owner=%(contact)s |
| 39 | or comment_docname=%(user)s |
| 40 | or (owner=comment_docname and ifnull(parenttype, "")!="Assignment")) |
| 41 | and comment_doctype ='Message' |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 42 | order by creation desc |
| 43 | limit %(limit_start)s, %(limit_page_length)s""", webnotes.form_dict, as_dict=1) |
| 44 | else: |
Anand Doshi | fedfd89 | 2012-03-30 12:29:06 +0530 | [diff] [blame] | 45 | return webnotes.conn.sql("""select * from `tabComment` |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 46 | where (owner=%(contact)s and comment_docname=%(user)s) |
| 47 | or (owner=%(user)s and comment_docname=%(contact)s) |
Rushabh Mehta | b62bbc6 | 2012-10-03 18:32:10 +0530 | [diff] [blame] | 48 | or (owner=%(contact)s and comment_docname=%(contact)s) |
| 49 | and comment_doctype ='Message' |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 50 | order by creation desc |
| 51 | limit %(limit_start)s, %(limit_page_length)s""", webnotes.form_dict, as_dict=1) |
| 52 | |
| 53 | |
| 54 | @webnotes.whitelist() |
| 55 | def get_active_users(arg=None): |
Rushabh Mehta | 6e04ef7 | 2012-09-27 18:41:46 +0530 | [diff] [blame] | 56 | return webnotes.conn.sql("""select name, |
| 57 | (select count(*) from tabSessions where user=tabProfile.name |
| 58 | and timediff(now(), lastupdate) < time("01:00:00")) as has_session |
| 59 | from tabProfile |
Rushabh Mehta | b6023a4 | 2012-03-01 13:47:51 +0530 | [diff] [blame] | 60 | where ifnull(enabled,0)=1 and |
| 61 | docstatus < 2 and |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 62 | name not in ('Administrator', 'Guest') |
| 63 | order by first_name""", as_dict=1) |
| 64 | |
| 65 | @webnotes.whitelist() |
| 66 | def post(arg=None): |
Rushabh Mehta | a4be4d3 | 2012-04-30 14:22:02 +0530 | [diff] [blame] | 67 | import webnotes |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 68 | """post message""" |
Anand Doshi | 0bf801a | 2013-02-15 18:27:05 +0530 | [diff] [blame] | 69 | if not arg: |
Anand Doshi | 20f63da | 2012-03-08 12:10:51 +0530 | [diff] [blame] | 70 | arg = {} |
| 71 | arg.update(webnotes.form_dict) |
Anand Doshi | 0bf801a | 2013-02-15 18:27:05 +0530 | [diff] [blame] | 72 | |
| 73 | if isinstance(arg, basestring): |
| 74 | import json |
| 75 | arg = json.loads(arg) |
| 76 | |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 77 | from webnotes.model.doc import Document |
Anand Doshi | fedfd89 | 2012-03-30 12:29:06 +0530 | [diff] [blame] | 78 | d = Document('Comment') |
Rushabh Mehta | 39b5b06 | 2012-12-11 16:28:33 +0530 | [diff] [blame] | 79 | d.parenttype = arg.get("parenttype") |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 80 | d.comment = arg['txt'] |
| 81 | d.comment_docname = arg['contact'] |
| 82 | d.comment_doctype = 'Message' |
| 83 | d.save() |
Anand Doshi | 82042f1 | 2012-04-06 17:54:17 +0530 | [diff] [blame] | 84 | |
| 85 | import webnotes.utils |
| 86 | if webnotes.utils.cint(arg.get('notify')): |
| 87 | notify(arg) |
Rushabh Mehta | ef29e55 | 2012-02-27 18:41:11 +0530 | [diff] [blame] | 88 | |
| 89 | @webnotes.whitelist() |
| 90 | def delete(arg=None): |
Anand Doshi | fedfd89 | 2012-03-30 12:29:06 +0530 | [diff] [blame] | 91 | webnotes.conn.sql("""delete from `tabComment` where name=%s""", |
Rushabh Mehta | ef29e55 | 2012-02-27 18:41:11 +0530 | [diff] [blame] | 92 | webnotes.form_dict['name']); |
Anand Doshi | 82042f1 | 2012-04-06 17:54:17 +0530 | [diff] [blame] | 93 | |
| 94 | def notify(arg=None): |
Anand Doshi | 0bf801a | 2013-02-15 18:27:05 +0530 | [diff] [blame] | 95 | from webnotes.utils import cstr, get_fullname |
Anand Doshi | 13dc9f3 | 2013-02-13 23:42:48 +0530 | [diff] [blame] | 96 | from startup import get_url |
| 97 | |
Anand Doshi | 0bf801a | 2013-02-15 18:27:05 +0530 | [diff] [blame] | 98 | fn = get_fullname(webnotes.user.name) or webnotes.user.name |
| 99 | |
Anand Doshi | b6a0f78 | 2012-12-07 19:10:16 +0530 | [diff] [blame] | 100 | url = get_url() |
Anand Doshi | 0bf801a | 2013-02-15 18:27:05 +0530 | [diff] [blame] | 101 | |
Anand Doshi | d628456 | 2012-12-07 19:28:27 +0530 | [diff] [blame] | 102 | message = '''You have a message from <b>%s</b>: |
Anand Doshi | eb55df0 | 2012-03-08 12:06:27 +0530 | [diff] [blame] | 103 | |
Anand Doshi | b6a0f78 | 2012-12-07 19:10:16 +0530 | [diff] [blame] | 104 | %s |
Anand Doshi | 82042f1 | 2012-04-06 17:54:17 +0530 | [diff] [blame] | 105 | |
Anand Doshi | b6a0f78 | 2012-12-07 19:10:16 +0530 | [diff] [blame] | 106 | To answer, please login to your erpnext account at \ |
| 107 | <a href=\"%s\" target='_blank'>%s</a> |
| 108 | ''' % (fn, arg['txt'], url, url) |
Anand Doshi | 82042f1 | 2012-04-06 17:54:17 +0530 | [diff] [blame] | 109 | |
Anand Doshi | 0bf801a | 2013-02-15 18:27:05 +0530 | [diff] [blame] | 110 | sender = webnotes.conn.get_value("Profile", webnotes.user.name, "email") \ |
| 111 | or webnotes.user.name |
| 112 | recipient = [webnotes.conn.get_value("Profile", arg["contact"], "email") \ |
| 113 | or arg["contact"]] |
Anand Doshi | 82042f1 | 2012-04-06 17:54:17 +0530 | [diff] [blame] | 114 | |
| 115 | from webnotes.utils.email_lib import sendmail |
Anand Doshi | 0bf801a | 2013-02-15 18:27:05 +0530 | [diff] [blame] | 116 | sendmail(recipient, sender, message, arg.get("subject") or "You have a message from %s" % (fn,)) |
Anand Doshi | 13dc9f3 | 2013-02-13 23:42:48 +0530 | [diff] [blame] | 117 | |