Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

138 řádky
4.3 KiB

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