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.
 
 
 
 
 
 

149 lines
4.9 KiB

  1. # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
  2. # MIT License. See license.txt
  3. from __future__ import unicode_literals
  4. """assign/unassign to ToDo"""
  5. import webnotes
  6. from webnotes import _
  7. from webnotes.utils import cint
  8. from webnotes.widgets.form.load import get_docinfo
  9. def get(args=None):
  10. """get assigned to"""
  11. if not args:
  12. args = webnotes.local.form_dict
  13. get_docinfo(args.get("doctype"), args.get("name"))
  14. return webnotes.conn.sql_list("""select owner from `tabToDo`
  15. where reference_type=%(doctype)s and reference_name=%(name)s and status="Open"
  16. order by modified desc limit 5""", args)
  17. @webnotes.whitelist()
  18. def add(args=None):
  19. """add in someone's to do list"""
  20. if not args:
  21. args = webnotes.local.form_dict
  22. if webnotes.conn.sql("""select owner from `tabToDo`
  23. where reference_type=%(doctype)s and reference_name=%(name)s and status="Open"
  24. and owner=%(assign_to)s""", args):
  25. webnotes.msgprint("Already in todo", raise_exception=True)
  26. return
  27. else:
  28. from webnotes.utils import nowdate
  29. d = webnotes.bean({
  30. "doctype":"ToDo",
  31. "owner": args['assign_to'],
  32. "reference_type": args['doctype'],
  33. "reference_name": args['name'],
  34. "description": args.get('description'),
  35. "priority": args.get("priority", "Medium"),
  36. "status": "Open",
  37. "date": args.get('date', nowdate()),
  38. "assigned_by": args.get('assigned_by', webnotes.user.name),
  39. }).insert(ignore_permissions=True).doc
  40. # set assigned_to if field exists
  41. from webnotes.model.meta import has_field
  42. if has_field(args['doctype'], "assigned_to"):
  43. webnotes.conn.set_value(args['doctype'], args['name'], "assigned_to", args['assign_to'])
  44. try:
  45. if cint(args.get("restrict")):
  46. from webnotes.core.page.user_properties import user_properties
  47. user_properties.add(args['assign_to'], args['doctype'], args['name'])
  48. webnotes.msgprint(_("Restriction added"))
  49. except webnotes.PermissionError:
  50. webnotes.throw("{cannot}: {user}, {_for}: {doctype} {_and}: {name}".format(cannot=_("You cannot restrict User"),
  51. user=args['assign_to'], _for=_("for DocType"), doctype=_(args['doctype']), _and=_("and Name"),
  52. name=args['name']))
  53. # notify
  54. if not args.get("no_notification"):
  55. notify_assignment(d.assigned_by, d.owner, d.reference_type, d.reference_name, action='ASSIGN', description=args.get("description"), notify=args.get('notify'))
  56. # update feeed
  57. try:
  58. from erpnext.home import make_feed
  59. from webnotes.utils import get_fullname
  60. make_feed('Assignment', d.reference_type, d.reference_name, webnotes.session['user'],
  61. '[%s] Assigned to %s' % (d.priority, get_fullname(d.owner)), '#C78F58')
  62. except ImportError, e:
  63. pass
  64. return get(args)
  65. @webnotes.whitelist()
  66. def remove(doctype, name, assign_to):
  67. """remove from todo"""
  68. todo = webnotes.bean("ToDo", {"reference_type":doctype, "reference_name":name, "owner":assign_to, "status":"Open"})
  69. todo.doc.status = "Closed"
  70. todo.save(ignore_permissions=True)
  71. # clear assigned_to if field exists
  72. from webnotes.model.meta import has_field
  73. if has_field(doctype, "assigned_to"):
  74. webnotes.conn.set_value(doctype, name, "assigned_to", None)
  75. notify_assignment(todo.doc.assigned_by, todo.doc.owner, todo.doc.reference_type, todo.doc.reference_name)
  76. return get({"doctype": doctype, "name": name})
  77. def clear(doctype, name):
  78. for assign_to in webnotes.conn.sql_list("""select owner from `tabToDo`
  79. where reference_type=%(doctype)s and reference_name=%(name)s""", locals()):
  80. remove(doctype, name, assign_to)
  81. def notify_assignment(assigned_by, owner, doc_type, doc_name, action='CLOSE',
  82. description=None, notify=0):
  83. """
  84. Notify assignee that there is a change in assignment
  85. """
  86. if not (assigned_by and owner and doc_type and doc_name): return
  87. # self assignment / closing - no message
  88. if assigned_by==owner:
  89. return
  90. from webnotes.boot import get_fullnames
  91. user_info = get_fullnames()
  92. # Search for email address in description -- i.e. assignee
  93. from webnotes.utils import get_url_to_form
  94. assignment = get_url_to_form(doc_type, doc_name, label="%s: %s" % (doc_type, doc_name))
  95. if action=='CLOSE':
  96. if owner == webnotes.session.get('user'):
  97. arg = {
  98. 'contact': assigned_by,
  99. 'txt': "The task %s, that you assigned to %s, has been \
  100. closed." % (assignment,
  101. user_info.get(owner, {}).get('fullname'))
  102. }
  103. else:
  104. arg = {
  105. 'contact': assigned_by,
  106. 'txt': "The task %s, that you assigned to %s, \
  107. has been closed by %s." % (assignment,
  108. user_info.get(owner, {}).get('fullname'),
  109. user_info.get(webnotes.session.get('user'),
  110. {}).get('fullname'))
  111. }
  112. else:
  113. arg = {
  114. 'contact': owner,
  115. 'txt': "A new task, %s, has been assigned to you by %s. %s" \
  116. % (assignment,
  117. user_info.get(webnotes.session.get('user'), {}).get('fullname'),
  118. description and ("<p>Description: " + description + "</p>") or ""),
  119. 'notify': notify
  120. }
  121. arg["parenttype"] = "Assignment"
  122. from webnotes.core.page.messages import messages
  123. import json
  124. messages.post(json.dumps(arg))