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.
 
 
 
 
 
 

124 lines
3.3 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. import webnotes, json
  5. import webnotes.model.doc
  6. import webnotes.utils
  7. @webnotes.whitelist()
  8. def getdoc(doctype, name, user=None):
  9. """
  10. Loads a doclist for a given document. This method is called directly from the client.
  11. Requries "doctype", "name" as form variables.
  12. Will also call the "onload" method on the document.
  13. """
  14. import webnotes
  15. if not (doctype and name):
  16. raise Exception, 'doctype and name required!'
  17. if not name:
  18. name = doctype
  19. if not webnotes.conn.exists(doctype, name):
  20. return []
  21. try:
  22. bean = webnotes.bean(doctype, name)
  23. bean.run_method("onload")
  24. if not bean.has_read_perm():
  25. raise webnotes.PermissionError
  26. doclist = bean.doclist
  27. # add file list
  28. get_docinfo(doctype, name)
  29. except Exception, e:
  30. webnotes.errprint(webnotes.utils.get_traceback())
  31. webnotes.msgprint('Did not load.')
  32. raise
  33. if bean and not name.startswith('_'):
  34. webnotes.user.update_recent(doctype, name)
  35. webnotes.response['docs'] = doclist
  36. @webnotes.whitelist()
  37. def getdoctype(doctype, with_parent=False, cached_timestamp=None):
  38. """load doctype"""
  39. import webnotes.model.doctype
  40. import webnotes.model.meta
  41. doclist = []
  42. # with parent (called from report builder)
  43. if with_parent:
  44. parent_dt = webnotes.model.meta.get_parent_dt(doctype)
  45. if parent_dt:
  46. doclist = webnotes.model.doctype.get(parent_dt, processed=True)
  47. webnotes.response['parent_dt'] = parent_dt
  48. if not doclist:
  49. doclist = webnotes.model.doctype.get(doctype, processed=True)
  50. webnotes.response['restrictions'] = get_restrictions(doclist)
  51. if cached_timestamp and doclist[0].modified==cached_timestamp:
  52. return "use_cache"
  53. webnotes.response['docs'] = doclist
  54. def get_docinfo(doctype, name):
  55. webnotes.response["docinfo"] = {
  56. "attachments": add_attachments(doctype, name),
  57. "comments": add_comments(doctype, name),
  58. "assignments": add_assignments(doctype, name)
  59. }
  60. def get_restrictions(meta):
  61. out = {}
  62. all_restrictions = webnotes.defaults.get_restrictions()
  63. for df in meta.get_restricted_fields(all_restrictions):
  64. out[df.options] = all_restrictions[df.options]
  65. return out
  66. def add_attachments(dt, dn):
  67. attachments = {}
  68. for f in webnotes.conn.sql("""select name, file_name, file_url from
  69. `tabFile Data` where attached_to_name=%s and attached_to_doctype=%s""",
  70. (dn, dt), as_dict=True):
  71. attachments[f.file_url or f.file_name] = f.name
  72. return attachments
  73. def add_comments(dt, dn, limit=20):
  74. cl = webnotes.conn.sql("""select name, comment, comment_by, creation from `tabComment`
  75. where comment_doctype=%s and comment_docname=%s
  76. order by creation desc limit %s""" % ('%s','%s', limit), (dt, dn), as_dict=1)
  77. return cl
  78. def add_assignments(dt, dn):
  79. cl = webnotes.conn.sql_list("""select owner from `tabToDo`
  80. where reference_type=%(doctype)s and reference_name=%(name)s and status="Open"
  81. order by modified desc limit 5""", {
  82. "doctype": dt,
  83. "name": dn
  84. })
  85. return cl
  86. @webnotes.whitelist()
  87. def get_badge_info(doctypes, filters):
  88. filters = json.loads(filters)
  89. doctypes = json.loads(doctypes)
  90. filters["docstatus"] = ["!=", 2]
  91. out = {}
  92. for doctype in doctypes:
  93. out[doctype] = webnotes.conn.get_value(doctype, filters, "count(*)")
  94. return out