blob: 9627d99e215565fb7fb19b5a67ef0408a946abb0 [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
27 if webnotes.form_dict['contact'] == webnotes.session['user']:
Rushabh Mehta07a36a42012-02-27 18:17:57 +053028 # set all messages as read
Anand Doshifedfd892012-03-30 12:29:06 +053029 webnotes.conn.sql("""UPDATE `tabComment`
Rushabh Mehta07a36a42012-02-27 18:17:57 +053030 set docstatus = 1 where comment_doctype in ('My Company', 'Message')
31 and comment_docname = %s
32 """, webnotes.user.name)
33
34 # return messages
Anand Doshifedfd892012-03-30 12:29:06 +053035 return webnotes.conn.sql("""select * from `tabComment`
Rushabh Mehtafdea9662012-02-27 18:03:54 +053036 where (owner=%(contact)s or comment_docname=%(user)s)
37 and comment_doctype in ('My Company', 'Message')
38 order by creation desc
39 limit %(limit_start)s, %(limit_page_length)s""", webnotes.form_dict, as_dict=1)
40 else:
Anand Doshifedfd892012-03-30 12:29:06 +053041 return webnotes.conn.sql("""select * from `tabComment`
Rushabh Mehtafdea9662012-02-27 18:03:54 +053042 where (owner=%(contact)s and comment_docname=%(user)s)
43 or (owner=%(user)s and comment_docname=%(contact)s)
44 and comment_doctype in ('My Company', 'Message')
45 order by creation desc
46 limit %(limit_start)s, %(limit_page_length)s""", webnotes.form_dict, as_dict=1)
47
48
49@webnotes.whitelist()
50def get_active_users(arg=None):
51 return webnotes.conn.sql("""select name from tabProfile
Rushabh Mehtab6023a42012-03-01 13:47:51 +053052 where ifnull(enabled,0)=1 and
53 docstatus < 2 and
Rushabh Mehtafdea9662012-02-27 18:03:54 +053054 name not in ('Administrator', 'Guest')
55 order by first_name""", as_dict=1)
56
57@webnotes.whitelist()
58def post(arg=None):
Rushabh Mehtaa4be4d32012-04-30 14:22:02 +053059 import webnotes
Rushabh Mehtafdea9662012-02-27 18:03:54 +053060 """post message"""
Anand Doshi20f63da2012-03-08 12:10:51 +053061 if arg:
62 import json
63 arg = json.loads(arg)
64 else:
65 arg = {}
66 arg.update(webnotes.form_dict)
Rushabh Mehtafdea9662012-02-27 18:03:54 +053067 from webnotes.model.doc import Document
Anand Doshifedfd892012-03-30 12:29:06 +053068 d = Document('Comment')
Rushabh Mehtafdea9662012-02-27 18:03:54 +053069 d.comment = arg['txt']
70 d.comment_docname = arg['contact']
71 d.comment_doctype = 'Message'
72 d.save()
Anand Doshi82042f12012-04-06 17:54:17 +053073
74 import webnotes.utils
75 if webnotes.utils.cint(arg.get('notify')):
76 notify(arg)
Rushabh Mehtaef29e552012-02-27 18:41:11 +053077
78@webnotes.whitelist()
79def delete(arg=None):
Anand Doshifedfd892012-03-30 12:29:06 +053080 webnotes.conn.sql("""delete from `tabComment` where name=%s""",
Rushabh Mehtaef29e552012-02-27 18:41:11 +053081 webnotes.form_dict['name']);
Anand Doshi82042f12012-04-06 17:54:17 +053082
83def notify(arg=None):
84 from webnotes.utils import cstr
85 fn = webnotes.conn.sql('select first_name, last_name from tabProfile where name=%s', webnotes.user.name)[0]
86 if fn[0] or f[1]:
87 fn = cstr(fn[0]) + (fn[0] and ' ' or '') + cstr(fn[1])
88 else:
89 fn = webnotes.user.name
90
91 message = '''A new comment has been posted on your page by %s:
Anand Doshieb55df02012-03-08 12:06:27 +053092
Anand Doshi82042f12012-04-06 17:54:17 +053093 <b>Comment:</b> %s
94
95 To answer, please login to your erpnext account!
96
97 <a href='https://signin.erpnext.com'>https://signin.erpnext.com</a>
98 ''' % (fn, arg['txt'])
99
100 from webnotes.model.code import get_obj
101 note = get_obj('Notification Control')
102 email_msg = note.prepare_message({
103 'type': 'New Comment',
104 'message': message
105 })
106
107 sender = webnotes.user.name!='Administrator' and webnotes.user.name or 'support+admin_post@erpnext.com'
108
109 from webnotes.utils.email_lib import sendmail
110 sendmail([arg['contact']], sender, email_msg, fn + ' has posted a new comment')