blob: 90f5e9152fa2c59d31428e42665b223b49c15613 [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 Doshi0bf801a2013-02-15 18:27:05 +053069 if not arg:
Anand Doshi20f63da2012-03-08 12:10:51 +053070 arg = {}
71 arg.update(webnotes.form_dict)
Anand Doshi0bf801a2013-02-15 18:27:05 +053072
73 if isinstance(arg, basestring):
74 import json
75 arg = json.loads(arg)
76
Rushabh Mehtafdea9662012-02-27 18:03:54 +053077 from webnotes.model.doc import Document
Anand Doshifedfd892012-03-30 12:29:06 +053078 d = Document('Comment')
Rushabh Mehta39b5b062012-12-11 16:28:33 +053079 d.parenttype = arg.get("parenttype")
Rushabh Mehtafdea9662012-02-27 18:03:54 +053080 d.comment = arg['txt']
81 d.comment_docname = arg['contact']
82 d.comment_doctype = 'Message'
83 d.save()
Anand Doshi82042f12012-04-06 17:54:17 +053084
85 import webnotes.utils
86 if webnotes.utils.cint(arg.get('notify')):
87 notify(arg)
Rushabh Mehtaef29e552012-02-27 18:41:11 +053088
89@webnotes.whitelist()
90def delete(arg=None):
Anand Doshifedfd892012-03-30 12:29:06 +053091 webnotes.conn.sql("""delete from `tabComment` where name=%s""",
Rushabh Mehtaef29e552012-02-27 18:41:11 +053092 webnotes.form_dict['name']);
Anand Doshi82042f12012-04-06 17:54:17 +053093
94def notify(arg=None):
Anand Doshi0bf801a2013-02-15 18:27:05 +053095 from webnotes.utils import cstr, get_fullname
Anand Doshi13dc9f32013-02-13 23:42:48 +053096 from startup import get_url
97
Anand Doshi0bf801a2013-02-15 18:27:05 +053098 fn = get_fullname(webnotes.user.name) or webnotes.user.name
99
Anand Doshib6a0f782012-12-07 19:10:16 +0530100 url = get_url()
Anand Doshi0bf801a2013-02-15 18:27:05 +0530101
Anand Doshid6284562012-12-07 19:28:27 +0530102 message = '''You have a message from <b>%s</b>:
Anand Doshieb55df02012-03-08 12:06:27 +0530103
Anand Doshib6a0f782012-12-07 19:10:16 +0530104 %s
Anand Doshi82042f12012-04-06 17:54:17 +0530105
Anand Doshib6a0f782012-12-07 19:10:16 +0530106 To answer, please login to your erpnext account at \
107 <a href=\"%s\" target='_blank'>%s</a>
108 ''' % (fn, arg['txt'], url, url)
Anand Doshi82042f12012-04-06 17:54:17 +0530109
Anand Doshi0bf801a2013-02-15 18:27:05 +0530110 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 Doshi82042f12012-04-06 17:54:17 +0530114
115 from webnotes.utils.email_lib import sendmail
Anand Doshi0bf801a2013-02-15 18:27:05 +0530116 sendmail(recipient, sender, message, arg.get("subject") or "You have a message from %s" % (fn,))
Anand Doshi13dc9f32013-02-13 23:42:48 +0530117