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.
 
 
 
 
 
 

131 rivejä
4.0 KiB

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