blob: 212a2686b097fd5fa99c92bc78a9ef100a60cb0f [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
Rushabh Mehtafdea9662012-02-27 18:03:54 +053017import webnotes
18
19@webnotes.whitelist()
20def get_list(arg=None):
21 """get list of messages"""
22 webnotes.form_dict['limit_start'] = int(webnotes.form_dict['limit_start'])
23 webnotes.form_dict['limit_page_length'] = int(webnotes.form_dict['limit_page_length'])
24 webnotes.form_dict['user'] = webnotes.session['user']
25
26 if webnotes.form_dict['contact'] == webnotes.session['user']:
Rushabh Mehta07a36a42012-02-27 18:17:57 +053027 # set all messages as read
Anand Doshifedfd892012-03-30 12:29:06 +053028 webnotes.conn.sql("""UPDATE `tabComment`
Rushabh Mehta07a36a42012-02-27 18:17:57 +053029 set docstatus = 1 where comment_doctype in ('My Company', 'Message')
30 and comment_docname = %s
31 """, webnotes.user.name)
32
33 # return messages
Anand Doshifedfd892012-03-30 12:29:06 +053034 return webnotes.conn.sql("""select * from `tabComment`
Rushabh Mehtafdea9662012-02-27 18:03:54 +053035 where (owner=%(contact)s or comment_docname=%(user)s)
36 and comment_doctype in ('My Company', 'Message')
37 order by creation desc
38 limit %(limit_start)s, %(limit_page_length)s""", webnotes.form_dict, as_dict=1)
39 else:
Anand Doshifedfd892012-03-30 12:29:06 +053040 return webnotes.conn.sql("""select * from `tabComment`
Rushabh Mehtafdea9662012-02-27 18:03:54 +053041 where (owner=%(contact)s and comment_docname=%(user)s)
42 or (owner=%(user)s and comment_docname=%(contact)s)
43 and comment_doctype in ('My Company', 'Message')
44 order by creation desc
45 limit %(limit_start)s, %(limit_page_length)s""", webnotes.form_dict, as_dict=1)
46
47
48@webnotes.whitelist()
49def get_active_users(arg=None):
50 return webnotes.conn.sql("""select name from tabProfile
Rushabh Mehtab6023a42012-03-01 13:47:51 +053051 where ifnull(enabled,0)=1 and
52 docstatus < 2 and
Rushabh Mehtafdea9662012-02-27 18:03:54 +053053 name not in ('Administrator', 'Guest')
54 order by first_name""", as_dict=1)
55
56@webnotes.whitelist()
57def post(arg=None):
58 """post message"""
Anand Doshi20f63da2012-03-08 12:10:51 +053059 if arg:
60 import json
61 arg = json.loads(arg)
62 else:
63 arg = {}
64 arg.update(webnotes.form_dict)
Rushabh Mehtafdea9662012-02-27 18:03:54 +053065 from webnotes.model.doc import Document
Anand Doshifedfd892012-03-30 12:29:06 +053066 d = Document('Comment')
Rushabh Mehtafdea9662012-02-27 18:03:54 +053067 d.comment = arg['txt']
68 d.comment_docname = arg['contact']
69 d.comment_doctype = 'Message'
70 d.save()
Anand Doshi82042f12012-04-06 17:54:17 +053071
72 import webnotes.utils
73 if webnotes.utils.cint(arg.get('notify')):
74 notify(arg)
Rushabh Mehtaef29e552012-02-27 18:41:11 +053075
76@webnotes.whitelist()
77def delete(arg=None):
Anand Doshifedfd892012-03-30 12:29:06 +053078 webnotes.conn.sql("""delete from `tabComment` where name=%s""",
Rushabh Mehtaef29e552012-02-27 18:41:11 +053079 webnotes.form_dict['name']);
Anand Doshi82042f12012-04-06 17:54:17 +053080
81def notify(arg=None):
82 from webnotes.utils import cstr
83 fn = webnotes.conn.sql('select first_name, last_name from tabProfile where name=%s', webnotes.user.name)[0]
84 if fn[0] or f[1]:
85 fn = cstr(fn[0]) + (fn[0] and ' ' or '') + cstr(fn[1])
86 else:
87 fn = webnotes.user.name
88
89 message = '''A new comment has been posted on your page by %s:
Anand Doshieb55df02012-03-08 12:06:27 +053090
Anand Doshi82042f12012-04-06 17:54:17 +053091 <b>Comment:</b> %s
92
93 To answer, please login to your erpnext account!
94
95 <a href='https://signin.erpnext.com'>https://signin.erpnext.com</a>
96 ''' % (fn, arg['txt'])
97
98 from webnotes.model.code import get_obj
99 note = get_obj('Notification Control')
100 email_msg = note.prepare_message({
101 'type': 'New Comment',
102 'message': message
103 })
104
105 sender = webnotes.user.name!='Administrator' and webnotes.user.name or 'support+admin_post@erpnext.com'
106
107 from webnotes.utils.email_lib import sendmail
108 sendmail([arg['contact']], sender, email_msg, fn + ' has posted a new comment')