blob: bec0e32520ff3e2a44238a86b83a73905ee71775 [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
28 webnotes.conn.sql("""UPDATE `tabComment`
29 set docstatus = 1 where comment_doctype in ('My Company', 'Message')
30 and comment_docname = %s
31 """, webnotes.user.name)
32
Rushabh Mehtafdea9662012-02-27 18:03:54 +053033 if webnotes.form_dict['contact'] == webnotes.session['user']:
Rushabh Mehta07a36a42012-02-27 18:17:57 +053034 # return messages
Anand Doshifedfd892012-03-30 12:29:06 +053035 return webnotes.conn.sql("""select * from `tabComment`
Rushabh Mehtab62bbc62012-10-03 18:32:10 +053036 where (owner=%(contact)s
37 or comment_docname=%(user)s
38 or (owner=comment_docname and ifnull(parenttype, "")!="Assignment"))
39 and comment_doctype ='Message'
Rushabh Mehtafdea9662012-02-27 18:03:54 +053040 order by creation desc
41 limit %(limit_start)s, %(limit_page_length)s""", webnotes.form_dict, as_dict=1)
42 else:
Anand Doshifedfd892012-03-30 12:29:06 +053043 return webnotes.conn.sql("""select * from `tabComment`
Rushabh Mehtafdea9662012-02-27 18:03:54 +053044 where (owner=%(contact)s and comment_docname=%(user)s)
45 or (owner=%(user)s and comment_docname=%(contact)s)
Rushabh Mehtab62bbc62012-10-03 18:32:10 +053046 or (owner=%(contact)s and comment_docname=%(contact)s)
47 and comment_doctype ='Message'
Rushabh Mehtafdea9662012-02-27 18:03:54 +053048 order by creation desc
49 limit %(limit_start)s, %(limit_page_length)s""", webnotes.form_dict, as_dict=1)
50
51
52@webnotes.whitelist()
53def get_active_users(arg=None):
Rushabh Mehta6e04ef72012-09-27 18:41:46 +053054 return webnotes.conn.sql("""select name,
55 (select count(*) from tabSessions where user=tabProfile.name
56 and timediff(now(), lastupdate) < time("01:00:00")) as has_session
57 from tabProfile
Rushabh Mehtab6023a42012-03-01 13:47:51 +053058 where ifnull(enabled,0)=1 and
59 docstatus < 2 and
Rushabh Mehtafdea9662012-02-27 18:03:54 +053060 name not in ('Administrator', 'Guest')
61 order by first_name""", as_dict=1)
62
63@webnotes.whitelist()
64def post(arg=None):
Rushabh Mehtaa4be4d32012-04-30 14:22:02 +053065 import webnotes
Rushabh Mehtafdea9662012-02-27 18:03:54 +053066 """post message"""
Anand Doshi20f63da2012-03-08 12:10:51 +053067 if arg:
68 import json
69 arg = json.loads(arg)
70 else:
71 arg = {}
72 arg.update(webnotes.form_dict)
Rushabh Mehtafdea9662012-02-27 18:03:54 +053073 from webnotes.model.doc import Document
Anand Doshifedfd892012-03-30 12:29:06 +053074 d = Document('Comment')
Rushabh Mehta39b5b062012-12-11 16:28:33 +053075 d.parenttype = arg.get("parenttype")
Rushabh Mehtafdea9662012-02-27 18:03:54 +053076 d.comment = arg['txt']
77 d.comment_docname = arg['contact']
78 d.comment_doctype = 'Message'
79 d.save()
Anand Doshi82042f12012-04-06 17:54:17 +053080
81 import webnotes.utils
82 if webnotes.utils.cint(arg.get('notify')):
83 notify(arg)
Rushabh Mehtaef29e552012-02-27 18:41:11 +053084
85@webnotes.whitelist()
86def delete(arg=None):
Anand Doshifedfd892012-03-30 12:29:06 +053087 webnotes.conn.sql("""delete from `tabComment` where name=%s""",
Rushabh Mehtaef29e552012-02-27 18:41:11 +053088 webnotes.form_dict['name']);
Anand Doshi82042f12012-04-06 17:54:17 +053089
90def notify(arg=None):
91 from webnotes.utils import cstr
92 fn = webnotes.conn.sql('select first_name, last_name from tabProfile where name=%s', webnotes.user.name)[0]
93 if fn[0] or f[1]:
94 fn = cstr(fn[0]) + (fn[0] and ' ' or '') + cstr(fn[1])
95 else:
96 fn = webnotes.user.name
97
Anand Doshib6a0f782012-12-07 19:10:16 +053098 url = get_url()
Anand Doshid6284562012-12-07 19:28:27 +053099 message = '''You have a message from <b>%s</b>:
Anand Doshieb55df02012-03-08 12:06:27 +0530100
Anand Doshib6a0f782012-12-07 19:10:16 +0530101 %s
Anand Doshi82042f12012-04-06 17:54:17 +0530102
Anand Doshib6a0f782012-12-07 19:10:16 +0530103 To answer, please login to your erpnext account at \
104 <a href=\"%s\" target='_blank'>%s</a>
105 ''' % (fn, arg['txt'], url, url)
Anand Doshi82042f12012-04-06 17:54:17 +0530106
Anand Doshi82042f12012-04-06 17:54:17 +0530107 sender = webnotes.user.name!='Administrator' and webnotes.user.name or 'support+admin_post@erpnext.com'
108
109 from webnotes.utils.email_lib import sendmail
Anand Doshid6284562012-12-07 19:28:27 +0530110 sendmail([arg['contact']], sender, message, "You have a message from %s" % (fn,))
Anand Doshib6a0f782012-12-07 19:10:16 +0530111
112def get_url():
113 from webnotes.utils import get_request_site_address
114 url = get_request_site_address()
115 if not url or "localhost" in url:
116 subdomain = webnotes.conn.get_value("Website Settings", "Website Settings",
117 "subdomain")
118 if subdomain:
119 if "http" not in subdomain:
120 url = "http://" + subdomain
121 return url