You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

messages.py 3.2 KiB

11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
  2. # MIT License. See license.txt
  3. from __future__ import unicode_literals
  4. import webnotes
  5. from webnotes.core.doctype.notification_count.notification_count import delete_notification_count_for
  6. @webnotes.whitelist()
  7. def get_list(arg=None):
  8. """get list of messages"""
  9. webnotes.form_dict['limit_start'] = int(webnotes.form_dict['limit_start'])
  10. webnotes.form_dict['limit_page_length'] = int(webnotes.form_dict['limit_page_length'])
  11. webnotes.form_dict['user'] = webnotes.session['user']
  12. # set all messages as read
  13. webnotes.conn.begin()
  14. webnotes.conn.sql("""UPDATE `tabComment`
  15. set docstatus = 1 where comment_doctype in ('My Company', 'Message')
  16. and comment_docname = %s
  17. """, webnotes.user.name)
  18. delete_notification_count_for("Messages")
  19. webnotes.conn.commit()
  20. if webnotes.form_dict['contact'] == webnotes.session['user']:
  21. # return messages
  22. return webnotes.conn.sql("""select * from `tabComment`
  23. where (owner=%(contact)s
  24. or comment_docname=%(user)s
  25. or (owner=comment_docname and ifnull(parenttype, "")!="Assignment"))
  26. and comment_doctype ='Message'
  27. order by creation desc
  28. limit %(limit_start)s, %(limit_page_length)s""", webnotes.local.form_dict, as_dict=1)
  29. else:
  30. return webnotes.conn.sql("""select * from `tabComment`
  31. where (owner=%(contact)s and comment_docname=%(user)s)
  32. or (owner=%(user)s and comment_docname=%(contact)s)
  33. or (owner=%(contact)s and comment_docname=%(contact)s)
  34. and comment_doctype ='Message'
  35. order by creation desc
  36. limit %(limit_start)s, %(limit_page_length)s""", webnotes.local.form_dict, as_dict=1)
  37. @webnotes.whitelist()
  38. def get_active_users(arg=None):
  39. return webnotes.conn.sql("""select name,
  40. (select count(*) from tabSessions where user=tabProfile.name
  41. and timediff(now(), lastupdate) < time("01:00:00")) as has_session
  42. from tabProfile
  43. where ifnull(enabled,0)=1 and
  44. docstatus < 2 and
  45. ifnull(user_type, '')!='Website User' and
  46. name not in ('Administrator', 'Guest')
  47. order by first_name""", as_dict=1)
  48. @webnotes.whitelist()
  49. def post(arg=None):
  50. import webnotes
  51. """post message"""
  52. if not arg:
  53. arg = {}
  54. arg.update(webnotes.local.form_dict)
  55. if isinstance(arg, basestring):
  56. import json
  57. arg = json.loads(arg)
  58. from webnotes.model.doc import Document
  59. d = Document('Comment')
  60. d.parenttype = arg.get("parenttype")
  61. d.comment = arg['txt']
  62. d.comment_docname = arg['contact']
  63. d.comment_doctype = 'Message'
  64. d.save()
  65. delete_notification_count_for("Messages")
  66. import webnotes.utils
  67. if webnotes.utils.cint(arg.get('notify')):
  68. notify(arg)
  69. @webnotes.whitelist()
  70. def delete(arg=None):
  71. webnotes.conn.sql("""delete from `tabComment` where name=%s""",
  72. webnotes.form_dict['name']);
  73. def notify(arg=None):
  74. from webnotes.utils import cstr, get_fullname, get_url
  75. webnotes.sendmail(\
  76. recipients=[webnotes.conn.get_value("Profile", arg["contact"], "email") or arg["contact"]],
  77. sender= webnotes.conn.get_value("Profile", webnotes.session.user, "email"),
  78. subject="New Message from " + get_fullname(webnotes.user.name),
  79. message=webnotes.get_template("templates/emails/new_message.html").render({
  80. "from": get_fullname(webnotes.user.name),
  81. "message": arg['txt'],
  82. "link": get_url()
  83. })
  84. )