seitime-frappe/webnotes/core/page/messages/messages.py
2014-01-30 14:57:48 +05:30

101 lines
No EOL
3.2 KiB
Python

# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
# MIT License. See license.txt
from __future__ import unicode_literals
import webnotes
from webnotes.core.doctype.notification_count.notification_count import delete_notification_count_for
@webnotes.whitelist()
def get_list(arg=None):
"""get list of messages"""
webnotes.form_dict['limit_start'] = int(webnotes.form_dict['limit_start'])
webnotes.form_dict['limit_page_length'] = int(webnotes.form_dict['limit_page_length'])
webnotes.form_dict['user'] = webnotes.session['user']
# set all messages as read
webnotes.conn.begin()
webnotes.conn.sql("""UPDATE `tabComment`
set docstatus = 1 where comment_doctype in ('My Company', 'Message')
and comment_docname = %s
""", webnotes.user.name)
delete_notification_count_for("Messages")
webnotes.conn.commit()
if webnotes.form_dict['contact'] == webnotes.session['user']:
# return messages
return webnotes.conn.sql("""select * from `tabComment`
where (owner=%(contact)s
or comment_docname=%(user)s
or (owner=comment_docname and ifnull(parenttype, "")!="Assignment"))
and comment_doctype ='Message'
order by creation desc
limit %(limit_start)s, %(limit_page_length)s""", webnotes.local.form_dict, as_dict=1)
else:
return webnotes.conn.sql("""select * from `tabComment`
where (owner=%(contact)s and comment_docname=%(user)s)
or (owner=%(user)s and comment_docname=%(contact)s)
or (owner=%(contact)s and comment_docname=%(contact)s)
and comment_doctype ='Message'
order by creation desc
limit %(limit_start)s, %(limit_page_length)s""", webnotes.local.form_dict, as_dict=1)
@webnotes.whitelist()
def get_active_users(arg=None):
return webnotes.conn.sql("""select name,
(select count(*) from tabSessions where user=tabProfile.name
and timediff(now(), lastupdate) < time("01:00:00")) as has_session
from tabProfile
where ifnull(enabled,0)=1 and
docstatus < 2 and
ifnull(user_type, '')!='Website User' and
name not in ('Administrator', 'Guest')
order by first_name""", as_dict=1)
@webnotes.whitelist()
def post(arg=None):
import webnotes
"""post message"""
if not arg:
arg = {}
arg.update(webnotes.local.form_dict)
if isinstance(arg, basestring):
import json
arg = json.loads(arg)
from webnotes.model.doc import Document
d = Document('Comment')
d.parenttype = arg.get("parenttype")
d.comment = arg['txt']
d.comment_docname = arg['contact']
d.comment_doctype = 'Message'
d.save()
delete_notification_count_for("Messages")
import webnotes.utils
if webnotes.utils.cint(arg.get('notify')):
notify(arg)
@webnotes.whitelist()
def delete(arg=None):
webnotes.conn.sql("""delete from `tabComment` where name=%s""",
webnotes.form_dict['name']);
def notify(arg=None):
from webnotes.utils import cstr, get_fullname, get_url
webnotes.sendmail(\
recipients=[webnotes.conn.get_value("Profile", arg["contact"], "email") or arg["contact"]],
sender= webnotes.conn.get_value("Profile", webnotes.session.user, "email"),
subject="New Message from " + get_fullname(webnotes.user.name),
message=webnotes.get_template("templates/emails/new_message.html").render({
"from": get_fullname(webnotes.user.name),
"message": arg['txt'],
"link": get_url()
})
)