Rushabh Mehta | aaf86ba | 2012-02-28 17:40:13 +0530 | [diff] [blame] | 1 | # 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 Doshi | 486f9df | 2012-07-19 13:40:31 +0530 | [diff] [blame] | 17 | from __future__ import unicode_literals |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 18 | import webnotes |
| 19 | |
| 20 | @webnotes.whitelist() |
| 21 | def 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 Mehta | 07a36a4 | 2012-02-27 18:17:57 +0530 | [diff] [blame] | 28 | # set all messages as read |
Anand Doshi | fedfd89 | 2012-03-30 12:29:06 +0530 | [diff] [blame] | 29 | webnotes.conn.sql("""UPDATE `tabComment` |
Rushabh Mehta | 07a36a4 | 2012-02-27 18:17:57 +0530 | [diff] [blame] | 30 | set docstatus = 1 where comment_doctype in ('My Company', 'Message') |
| 31 | and comment_docname = %s |
| 32 | """, webnotes.user.name) |
| 33 | |
| 34 | # return messages |
Anand Doshi | fedfd89 | 2012-03-30 12:29:06 +0530 | [diff] [blame] | 35 | return webnotes.conn.sql("""select * from `tabComment` |
Rushabh Mehta | b62bbc6 | 2012-10-03 18:32:10 +0530 | [diff] [blame] | 36 | 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 Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 40 | order by creation desc |
| 41 | limit %(limit_start)s, %(limit_page_length)s""", webnotes.form_dict, as_dict=1) |
| 42 | else: |
Anand Doshi | fedfd89 | 2012-03-30 12:29:06 +0530 | [diff] [blame] | 43 | return webnotes.conn.sql("""select * from `tabComment` |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 44 | where (owner=%(contact)s and comment_docname=%(user)s) |
| 45 | or (owner=%(user)s and comment_docname=%(contact)s) |
Rushabh Mehta | b62bbc6 | 2012-10-03 18:32:10 +0530 | [diff] [blame] | 46 | or (owner=%(contact)s and comment_docname=%(contact)s) |
| 47 | and comment_doctype ='Message' |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 48 | order by creation desc |
| 49 | limit %(limit_start)s, %(limit_page_length)s""", webnotes.form_dict, as_dict=1) |
| 50 | |
| 51 | |
| 52 | @webnotes.whitelist() |
| 53 | def get_active_users(arg=None): |
Rushabh Mehta | 6e04ef7 | 2012-09-27 18:41:46 +0530 | [diff] [blame] | 54 | 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 Mehta | b6023a4 | 2012-03-01 13:47:51 +0530 | [diff] [blame] | 58 | where ifnull(enabled,0)=1 and |
| 59 | docstatus < 2 and |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 60 | name not in ('Administrator', 'Guest') |
| 61 | order by first_name""", as_dict=1) |
| 62 | |
| 63 | @webnotes.whitelist() |
| 64 | def post(arg=None): |
Rushabh Mehta | a4be4d3 | 2012-04-30 14:22:02 +0530 | [diff] [blame] | 65 | import webnotes |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 66 | """post message""" |
Anand Doshi | 20f63da | 2012-03-08 12:10:51 +0530 | [diff] [blame] | 67 | if arg: |
| 68 | import json |
| 69 | arg = json.loads(arg) |
| 70 | else: |
| 71 | arg = {} |
| 72 | arg.update(webnotes.form_dict) |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 73 | from webnotes.model.doc import Document |
Anand Doshi | fedfd89 | 2012-03-30 12:29:06 +0530 | [diff] [blame] | 74 | d = Document('Comment') |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 75 | d.comment = arg['txt'] |
| 76 | d.comment_docname = arg['contact'] |
| 77 | d.comment_doctype = 'Message' |
| 78 | d.save() |
Anand Doshi | 82042f1 | 2012-04-06 17:54:17 +0530 | [diff] [blame] | 79 | |
| 80 | import webnotes.utils |
| 81 | if webnotes.utils.cint(arg.get('notify')): |
| 82 | notify(arg) |
Rushabh Mehta | ef29e55 | 2012-02-27 18:41:11 +0530 | [diff] [blame] | 83 | |
| 84 | @webnotes.whitelist() |
| 85 | def delete(arg=None): |
Anand Doshi | fedfd89 | 2012-03-30 12:29:06 +0530 | [diff] [blame] | 86 | webnotes.conn.sql("""delete from `tabComment` where name=%s""", |
Rushabh Mehta | ef29e55 | 2012-02-27 18:41:11 +0530 | [diff] [blame] | 87 | webnotes.form_dict['name']); |
Anand Doshi | 82042f1 | 2012-04-06 17:54:17 +0530 | [diff] [blame] | 88 | |
| 89 | def notify(arg=None): |
| 90 | from webnotes.utils import cstr |
| 91 | fn = webnotes.conn.sql('select first_name, last_name from tabProfile where name=%s', webnotes.user.name)[0] |
| 92 | if fn[0] or f[1]: |
| 93 | fn = cstr(fn[0]) + (fn[0] and ' ' or '') + cstr(fn[1]) |
| 94 | else: |
| 95 | fn = webnotes.user.name |
| 96 | |
| 97 | message = '''A new comment has been posted on your page by %s: |
Anand Doshi | eb55df0 | 2012-03-08 12:06:27 +0530 | [diff] [blame] | 98 | |
Anand Doshi | 82042f1 | 2012-04-06 17:54:17 +0530 | [diff] [blame] | 99 | <b>Comment:</b> %s |
| 100 | |
| 101 | To answer, please login to your erpnext account! |
Anand Doshi | 82042f1 | 2012-04-06 17:54:17 +0530 | [diff] [blame] | 102 | ''' % (fn, arg['txt']) |
| 103 | |
| 104 | from webnotes.model.code import get_obj |
| 105 | note = get_obj('Notification Control') |
| 106 | email_msg = note.prepare_message({ |
| 107 | 'type': 'New Comment', |
| 108 | 'message': message |
| 109 | }) |
| 110 | |
| 111 | sender = webnotes.user.name!='Administrator' and webnotes.user.name or 'support+admin_post@erpnext.com' |
| 112 | |
| 113 | from webnotes.utils.email_lib import sendmail |
| 114 | sendmail([arg['contact']], sender, email_msg, fn + ' has posted a new comment') |