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 | |
Rushabh Mehta | 833f070 | 2012-12-03 11:33:08 +0530 | [diff] [blame] | 27 | # 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 Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 33 | if webnotes.form_dict['contact'] == webnotes.session['user']: |
Rushabh Mehta | 07a36a4 | 2012-02-27 18:17:57 +0530 | [diff] [blame] | 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 | 39b5b06 | 2012-12-11 16:28:33 +0530 | [diff] [blame] | 75 | d.parenttype = arg.get("parenttype") |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 76 | d.comment = arg['txt'] |
| 77 | d.comment_docname = arg['contact'] |
| 78 | d.comment_doctype = 'Message' |
| 79 | d.save() |
Anand Doshi | 82042f1 | 2012-04-06 17:54:17 +0530 | [diff] [blame] | 80 | |
| 81 | import webnotes.utils |
| 82 | if webnotes.utils.cint(arg.get('notify')): |
| 83 | notify(arg) |
Rushabh Mehta | ef29e55 | 2012-02-27 18:41:11 +0530 | [diff] [blame] | 84 | |
| 85 | @webnotes.whitelist() |
| 86 | def delete(arg=None): |
Anand Doshi | fedfd89 | 2012-03-30 12:29:06 +0530 | [diff] [blame] | 87 | webnotes.conn.sql("""delete from `tabComment` where name=%s""", |
Rushabh Mehta | ef29e55 | 2012-02-27 18:41:11 +0530 | [diff] [blame] | 88 | webnotes.form_dict['name']); |
Anand Doshi | 82042f1 | 2012-04-06 17:54:17 +0530 | [diff] [blame] | 89 | |
| 90 | def 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 Doshi | b6a0f78 | 2012-12-07 19:10:16 +0530 | [diff] [blame] | 98 | url = get_url() |
Anand Doshi | d628456 | 2012-12-07 19:28:27 +0530 | [diff] [blame] | 99 | message = '''You have a message from <b>%s</b>: |
Anand Doshi | eb55df0 | 2012-03-08 12:06:27 +0530 | [diff] [blame] | 100 | |
Anand Doshi | b6a0f78 | 2012-12-07 19:10:16 +0530 | [diff] [blame] | 101 | %s |
Anand Doshi | 82042f1 | 2012-04-06 17:54:17 +0530 | [diff] [blame] | 102 | |
Anand Doshi | b6a0f78 | 2012-12-07 19:10:16 +0530 | [diff] [blame] | 103 | To answer, please login to your erpnext account at \ |
| 104 | <a href=\"%s\" target='_blank'>%s</a> |
| 105 | ''' % (fn, arg['txt'], url, url) |
Anand Doshi | 82042f1 | 2012-04-06 17:54:17 +0530 | [diff] [blame] | 106 | |
Anand Doshi | 82042f1 | 2012-04-06 17:54:17 +0530 | [diff] [blame] | 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 |
Anand Doshi | d628456 | 2012-12-07 19:28:27 +0530 | [diff] [blame] | 110 | sendmail([arg['contact']], sender, message, "You have a message from %s" % (fn,)) |
Anand Doshi | b6a0f78 | 2012-12-07 19:10:16 +0530 | [diff] [blame] | 111 | |
| 112 | def 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 |