25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

201 lines
6.1 KiB

  1. # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
  2. # License: MIT. See LICENSE
  3. """assign/unassign to ToDo"""
  4. import frappe
  5. from frappe import _
  6. from frappe.desk.form.document_follow import follow_document
  7. from frappe.desk.doctype.notification_log.notification_log import enqueue_create_notification,\
  8. get_title, get_title_html
  9. import frappe.utils
  10. import frappe.share
  11. import json
  12. class DuplicateToDoError(frappe.ValidationError): pass
  13. def get(args=None):
  14. """get assigned to"""
  15. if not args:
  16. args = frappe.local.form_dict
  17. return frappe.get_all("ToDo", fields=["allocated_to as owner", "name"], filters={
  18. "reference_type": args.get("doctype"),
  19. "reference_name": args.get("name"),
  20. "status": ("!=", "Cancelled")
  21. }, limit=5)
  22. @frappe.whitelist()
  23. def add(args=None):
  24. """add in someone's to do list
  25. args = {
  26. "assign_to": [],
  27. "doctype": ,
  28. "name": ,
  29. "description": ,
  30. "assignment_rule":
  31. }
  32. """
  33. if not args:
  34. args = frappe.local.form_dict
  35. users_with_duplicate_todo = []
  36. shared_with_users = []
  37. for assign_to in frappe.parse_json(args.get("assign_to")):
  38. filters = {
  39. "reference_type": args['doctype'],
  40. "reference_name": args['name'],
  41. "status": "Open",
  42. "allocated_to": assign_to
  43. }
  44. if frappe.get_all("ToDo", filters=filters):
  45. users_with_duplicate_todo.append(assign_to)
  46. else:
  47. from frappe.utils import nowdate
  48. if not args.get('description'):
  49. args['description'] = _('Assignment for {0} {1}').format(args['doctype'], args['name'])
  50. d = frappe.get_doc({
  51. "doctype": "ToDo",
  52. "allocated_to": assign_to,
  53. "reference_type": args['doctype'],
  54. "reference_name": args['name'],
  55. "description": args.get('description'),
  56. "priority": args.get("priority", "Medium"),
  57. "status": "Open",
  58. "date": args.get('date', nowdate()),
  59. "assigned_by": args.get('assigned_by', frappe.session.user),
  60. 'assignment_rule': args.get('assignment_rule')
  61. }).insert(ignore_permissions=True)
  62. # set assigned_to if field exists
  63. if frappe.get_meta(args['doctype']).get_field("assigned_to"):
  64. frappe.db.set_value(args['doctype'], args['name'], "assigned_to", assign_to)
  65. doc = frappe.get_doc(args['doctype'], args['name'])
  66. # if assignee does not have permissions, share
  67. if not frappe.has_permission(doc=doc, user=assign_to):
  68. frappe.share.add(doc.doctype, doc.name, assign_to)
  69. shared_with_users.append(assign_to)
  70. # make this document followed by assigned user
  71. follow_document(args['doctype'], args['name'], assign_to)
  72. # notify
  73. notify_assignment(d.assigned_by, d.allocated_to, d.reference_type, d.reference_name, action='ASSIGN',
  74. description=args.get("description"))
  75. if shared_with_users:
  76. user_list = format_message_for_assign_to(shared_with_users)
  77. frappe.msgprint(_("Shared with the following Users with Read access:{0}").format(user_list, alert=True))
  78. if users_with_duplicate_todo:
  79. user_list = format_message_for_assign_to(users_with_duplicate_todo)
  80. frappe.msgprint(_("Already in the following Users ToDo list:{0}").format(user_list, alert=True))
  81. return get(args)
  82. @frappe.whitelist()
  83. def add_multiple(args=None):
  84. if not args:
  85. args = frappe.local.form_dict
  86. docname_list = json.loads(args['name'])
  87. for docname in docname_list:
  88. args.update({"name": docname})
  89. add(args)
  90. def close_all_assignments(doctype, name):
  91. assignments = frappe.db.get_all('ToDo', fields=['allocated_to'], filters =
  92. dict(reference_type = doctype, reference_name = name, status=('!=', 'Cancelled')))
  93. if not assignments:
  94. return False
  95. for assign_to in assignments:
  96. set_status(doctype, name, assign_to.allocated_to, status="Closed")
  97. return True
  98. @frappe.whitelist()
  99. def remove(doctype, name, assign_to):
  100. return set_status(doctype, name, assign_to, status="Cancelled")
  101. def set_status(doctype, name, assign_to, status="Cancelled"):
  102. """remove from todo"""
  103. try:
  104. todo = frappe.db.get_value("ToDo", {"reference_type":doctype,
  105. "reference_name":name, "allocated_to":assign_to, "status": ('!=', status)})
  106. if todo:
  107. todo = frappe.get_doc("ToDo", todo)
  108. todo.status = status
  109. todo.save(ignore_permissions=True)
  110. notify_assignment(todo.assigned_by, todo.allocated_to, todo.reference_type, todo.reference_name)
  111. except frappe.DoesNotExistError:
  112. pass
  113. # clear assigned_to if field exists
  114. if frappe.get_meta(doctype).get_field("assigned_to") and status=="Cancelled":
  115. frappe.db.set_value(doctype, name, "assigned_to", None)
  116. return get({"doctype": doctype, "name": name})
  117. def clear(doctype, name):
  118. '''
  119. Clears assignments, return False if not assigned.
  120. '''
  121. assignments = frappe.db.get_all('ToDo', fields=['allocated_to'], filters =
  122. dict(reference_type = doctype, reference_name = name))
  123. if not assignments:
  124. return False
  125. for assign_to in assignments:
  126. set_status(doctype, name, assign_to.allocated_to, "Cancelled")
  127. return True
  128. def notify_assignment(assigned_by, allocated_to, doc_type, doc_name, action='CLOSE',
  129. description=None):
  130. """
  131. Notify assignee that there is a change in assignment
  132. """
  133. if not (assigned_by and allocated_to and doc_type and doc_name):
  134. return
  135. # return if self assigned or user disabled
  136. if assigned_by == allocated_to or not frappe.db.get_value('User', allocated_to, 'enabled'):
  137. return
  138. # Search for email address in description -- i.e. assignee
  139. user_name = frappe.get_cached_value('User', frappe.session.user, 'full_name')
  140. title = get_title(doc_type, doc_name)
  141. description_html = "<div>{0}</div>".format(description) if description else None
  142. if action == 'CLOSE':
  143. subject = _('Your assignment on {0} {1} has been removed by {2}')\
  144. .format(frappe.bold(doc_type), get_title_html(title), frappe.bold(user_name))
  145. else:
  146. user_name = frappe.bold(user_name)
  147. document_type = frappe.bold(doc_type)
  148. title = get_title_html(title)
  149. subject = _('{0} assigned a new task {1} {2} to you').format(user_name, document_type, title)
  150. notification_doc = {
  151. 'type': 'Assignment',
  152. 'document_type': doc_type,
  153. 'subject': subject,
  154. 'document_name': doc_name,
  155. 'from_user': frappe.session.user,
  156. 'email_content': description_html
  157. }
  158. enqueue_create_notification(allocated_to, notification_doc)
  159. def format_message_for_assign_to(users):
  160. return "<br><br>" + "<br>".join(users)