blob: 019cd071ead38a712cb1f89ba533ef951526cac2 [file] [log] [blame]
Rushabh Mehtaaaf86ba2012-02-28 17:40:13 +05301# 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 Doshi486f9df2012-07-19 13:40:31 +053017from __future__ import unicode_literals
Rushabh Mehtafdea9662012-02-27 18:03:54 +053018import webnotes
19
20@webnotes.whitelist()
21def 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 Mehta833f0702012-12-03 11:33:08 +053027 # set all messages as read
Rushabh Mehta4f8dfd62013-02-03 22:42:05 +053028 webnotes.conn.begin()
Rushabh Mehta833f0702012-12-03 11:33:08 +053029 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 Mehta4f8dfd62013-02-03 22:42:05 +053033 webnotes.conn.commit()
Rushabh Mehta833f0702012-12-03 11:33:08 +053034
Rushabh Mehtafdea9662012-02-27 18:03:54 +053035 if webnotes.form_dict['contact'] == webnotes.session['user']:
Rushabh Mehta07a36a42012-02-27 18:17:57 +053036 # return messages
Anand Doshifedfd892012-03-30 12:29:06 +053037 return webnotes.conn.sql("""select * from `tabComment`
Rushabh Mehtab62bbc62012-10-03 18:32:10 +053038 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 Mehtafdea9662012-02-27 18:03:54 +053042 order by creation desc
43 limit %(limit_start)s, %(limit_page_length)s""", webnotes.form_dict, as_dict=1)
44 else:
Anand Doshifedfd892012-03-30 12:29:06 +053045 return webnotes.conn.sql("""select * from `tabComment`
Rushabh Mehtafdea9662012-02-27 18:03:54 +053046 where (owner=%(contact)s and comment_docname=%(user)s)
47 or (owner=%(user)s and comment_docname=%(contact)s)
Rushabh Mehtab62bbc62012-10-03 18:32:10 +053048 or (owner=%(contact)s and comment_docname=%(contact)s)
49 and comment_doctype ='Message'
Rushabh Mehtafdea9662012-02-27 18:03:54 +053050 order by creation desc
51 limit %(limit_start)s, %(limit_page_length)s""", webnotes.form_dict, as_dict=1)
52
53
54@webnotes.whitelist()
55def get_active_users(arg=None):
Rushabh Mehta6e04ef72012-09-27 18:41:46 +053056 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 Mehtab6023a42012-03-01 13:47:51 +053060 where ifnull(enabled,0)=1 and
61 docstatus < 2 and
Rushabh Mehtafdea9662012-02-27 18:03:54 +053062 name not in ('Administrator', 'Guest')
63 order by first_name""", as_dict=1)
64
65@webnotes.whitelist()
66def post(arg=None):
Rushabh Mehtaa4be4d32012-04-30 14:22:02 +053067 import webnotes
Rushabh Mehtafdea9662012-02-27 18:03:54 +053068 """post message"""
Anand Doshi20f63da2012-03-08 12:10:51 +053069 if arg:
70 import json
71 arg = json.loads(arg)
72 else:
73 arg = {}
74 arg.update(webnotes.form_dict)
Rushabh Mehtafdea9662012-02-27 18:03:54 +053075 from webnotes.model.doc import Document
Anand Doshifedfd892012-03-30 12:29:06 +053076 d = Document('Comment')
Rushabh Mehta39b5b062012-12-11 16:28:33 +053077 d.parenttype = arg.get("parenttype")
Rushabh Mehtafdea9662012-02-27 18:03:54 +053078 d.comment = arg['txt']
79 d.comment_docname = arg['contact']
80 d.comment_doctype = 'Message'
81 d.save()
Anand Doshi82042f12012-04-06 17:54:17 +053082
83 import webnotes.utils
84 if webnotes.utils.cint(arg.get('notify')):
85 notify(arg)
Rushabh Mehtaef29e552012-02-27 18:41:11 +053086
87@webnotes.whitelist()
88def delete(arg=None):
Anand Doshifedfd892012-03-30 12:29:06 +053089 webnotes.conn.sql("""delete from `tabComment` where name=%s""",
Rushabh Mehtaef29e552012-02-27 18:41:11 +053090 webnotes.form_dict['name']);
Anand Doshi82042f12012-04-06 17:54:17 +053091
92def notify(arg=None):
93 from webnotes.utils import cstr
Anand Doshi13dc9f32013-02-13 23:42:48 +053094 from startup import get_url
95
Anand Doshi82042f12012-04-06 17:54:17 +053096 fn = webnotes.conn.sql('select first_name, last_name from tabProfile where name=%s', webnotes.user.name)[0]
97 if fn[0] or f[1]:
98 fn = cstr(fn[0]) + (fn[0] and ' ' or '') + cstr(fn[1])
99 else:
100 fn = webnotes.user.name
101
Anand Doshib6a0f782012-12-07 19:10:16 +0530102 url = get_url()
Anand Doshid6284562012-12-07 19:28:27 +0530103 message = '''You have a message from <b>%s</b>:
Anand Doshieb55df02012-03-08 12:06:27 +0530104
Anand Doshib6a0f782012-12-07 19:10:16 +0530105 %s
Anand Doshi82042f12012-04-06 17:54:17 +0530106
Anand Doshib6a0f782012-12-07 19:10:16 +0530107 To answer, please login to your erpnext account at \
108 <a href=\"%s\" target='_blank'>%s</a>
109 ''' % (fn, arg['txt'], url, url)
Anand Doshi82042f12012-04-06 17:54:17 +0530110
Anand Doshi82042f12012-04-06 17:54:17 +0530111 sender = webnotes.user.name!='Administrator' and webnotes.user.name or 'support+admin_post@erpnext.com'
112
113 from webnotes.utils.email_lib import sendmail
Anand Doshid6284562012-12-07 19:28:27 +0530114 sendmail([arg['contact']], sender, message, "You have a message from %s" % (fn,))
Anand Doshi13dc9f32013-02-13 23:42:48 +0530115