您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

assign_to.py 4.8 KiB

13 年前
13 年前
13 年前
13 年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. # Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. #
  3. # MIT License (MIT)
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a
  6. # copy of this software and associated documentation files (the "Software"),
  7. # to deal in the Software without restriction, including without limitation
  8. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. # and/or sell copies of the Software, and to permit persons to whom the
  10. # Software is furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. # OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. #
  22. from __future__ import unicode_literals
  23. """assign/unassign to ToDo"""
  24. import webnotes
  25. @webnotes.whitelist()
  26. def get(args=None):
  27. """get assigned to"""
  28. if not args:
  29. args = webnotes.form_dict
  30. return webnotes.conn.sql("""select owner from `tabToDo`
  31. where reference_type=%(doctype)s and reference_name=%(name)s
  32. order by modified desc limit 5""", args, as_dict=1)
  33. @webnotes.whitelist()
  34. def add(args=None):
  35. """add in someone's to do list"""
  36. if not args:
  37. args = webnotes.form_dict
  38. if webnotes.conn.sql("""select owner from `tabToDo`
  39. where reference_type=%(doctype)s and reference_name=%(name)s
  40. and owner=%(assign_to)s""", args):
  41. webnotes.msgprint("Already in todo")
  42. return
  43. else:
  44. from webnotes.model.doc import Document
  45. from webnotes.utils import nowdate
  46. d = Document("ToDo")
  47. d.owner = args['assign_to']
  48. d.reference_type = args['doctype']
  49. d.reference_name = args['name']
  50. d.description = args['description']
  51. d.priority = args.get('priority', 'Medium')
  52. d.date = args.get('date', nowdate())
  53. d.assigned_by = args.get('assigned_by', webnotes.user.name)
  54. d.save(1)
  55. # notify
  56. if not args.get("no_notification"):
  57. notify_assignment(d.assigned_by, d.owner, d.reference_type, d.reference_name, action='ASSIGN', notify=args.get('notify'))
  58. # update feeed
  59. try:
  60. import home
  61. from webnotes.utils import get_fullname
  62. home.make_feed('Assignment', d.reference_type, d.reference_name, webnotes.session['user'],
  63. '[%s] Assigned to %s' % (d.priority, get_fullname(d.owner)), '#C78F58')
  64. except ImportError, e:
  65. pass
  66. return get(args)
  67. @webnotes.whitelist()
  68. def remove(doctype, name, assign_to):
  69. """remove from todo"""
  70. res = webnotes.conn.sql("""\
  71. select assigned_by, owner, reference_type, reference_name from `tabToDo`
  72. where reference_type=%(doctype)s and reference_name=%(name)s
  73. and owner=%(assign_to)s""", locals())
  74. webnotes.conn.sql("""delete from `tabToDo`
  75. where reference_type=%(doctype)s and reference_name=%(name)s
  76. and owner=%(assign_to)s""", locals())
  77. # clear assigned_to if field exists
  78. from webnotes.model.meta import has_field
  79. if has_field(doctype, "assigned_to"):
  80. webnotes.conn.set_value(doctype, name, "assigned_to", None)
  81. if res and res[0]: notify_assignment(res[0][0], res[0][1], res[0][2], res[0][3])
  82. return get({"doctype": doctype, "name": name})
  83. def notify_assignment(assigned_by, owner, doc_type, doc_name, action='CLOSE', notify=0):
  84. """
  85. Notify assignee that there is a change in assignment
  86. """
  87. if not (assigned_by and owner and doc_type and doc_name): return
  88. # self assignment / closing - no message
  89. if assigned_by==owner:
  90. return
  91. from webnotes.boot import get_fullnames
  92. user_info = get_fullnames()
  93. # Search for email address in description -- i.e. assignee
  94. from webnotes.utils import get_url_to_form
  95. assignment = get_url_to_form(doc_type, doc_name, label="%s: %s" % (doc_type, doc_name))
  96. if action=='CLOSE':
  97. if owner == webnotes.session.get('user'):
  98. arg = {
  99. 'contact': assigned_by,
  100. 'txt': "The task %s, that you assigned to %s, has been \
  101. closed." % (assignment,
  102. user_info.get(owner, {}).get('fullname'))
  103. }
  104. else:
  105. arg = {
  106. 'contact': assigned_by,
  107. 'txt': "The task %s, that you assigned to %s, \
  108. has been closed by %s." % (assignment,
  109. user_info.get(owner, {}).get('fullname'),
  110. user_info.get(webnotes.session.get('user'),
  111. {}).get('fullname'))
  112. }
  113. else:
  114. arg = {
  115. 'contact': owner,
  116. 'txt': "A new task, %s, has been assigned to you by %s." \
  117. % (assignment,
  118. user_info.get(webnotes.session.get('user'), {}).get('fullname')),
  119. 'notify': notify
  120. }
  121. arg["parenttype"] = "Assignment"
  122. from utilities.page.messages import messages
  123. import json
  124. messages.post(json.dumps(arg))