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.

load.py 3.8 KiB

13 vuotta sitten
13 vuotta sitten
12 vuotta sitten
12 vuotta sitten
12 vuotta sitten
13 vuotta sitten
13 vuotta sitten
13 vuotta sitten
12 vuotta sitten
12 vuotta sitten
12 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. doclist = []
  37. # single
  38. doclist = load_single_doc(doctype, name, user or webnotes.session.user)
  39. webnotes.response['docs'] = doclist
  40. @webnotes.whitelist()
  41. def getdoctype(doctype, with_parent=False, cached_timestamp=None):
  42. """load doctype"""
  43. import webnotes.model.doctype
  44. import webnotes.model.meta
  45. doclist = []
  46. # with parent (called from report builder)
  47. if with_parent:
  48. parent_dt = webnotes.model.meta.get_parent_dt(doctype)
  49. if parent_dt:
  50. doclist = webnotes.model.doctype.get(parent_dt, processed=True)
  51. webnotes.response['parent_dt'] = parent_dt
  52. if not doclist:
  53. doclist = webnotes.model.doctype.get(doctype, processed=True)
  54. if cached_timestamp and doclist[0].modified==cached_timestamp:
  55. return "use_cache"
  56. webnotes.response['docs'] = doclist
  57. def load_single_doc(dt, dn, user):
  58. """load doc and call onload methods"""
  59. # ----- REPLACE BY webnotes.client.get ------
  60. if not dn: dn = dt
  61. if not webnotes.conn.exists(dt, dn):
  62. return None
  63. try:
  64. dl = webnotes.bean(dt, dn).doclist
  65. # add file list
  66. add_file_list(dt, dn, dl)
  67. add_comments(dt, dn, dl)
  68. add_assignments(dt, dn, dl)
  69. except Exception, e:
  70. webnotes.errprint(webnotes.utils.getTraceback())
  71. webnotes.msgprint('Did not load.')
  72. raise e
  73. if dl and not dn.startswith('_'):
  74. webnotes.user.update_recent(dt, dn)
  75. return dl
  76. def add_file_list(dt, dn, dl):
  77. file_list = {}
  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. file_list[f.file_url or f.file_name] = f.name
  82. if file_list:
  83. dl[0].file_list = json.dumps(file_list)
  84. def add_comments(dt, dn, dl, limit=20):
  85. cl = webnotes.conn.sql("""select name, comment, comment_by, creation from `tabComment`
  86. where comment_doctype=%s and comment_docname=%s
  87. order by creation desc limit %s""" % ('%s','%s', limit), (dt, dn), as_dict=1)
  88. dl[0].fields["__comments"] = json.dumps(cl)
  89. def add_assignments(dt, dn, dl):
  90. cl = webnotes.conn.sql_list("""select owner from `tabToDo`
  91. where reference_type=%(doctype)s and reference_name=%(name)s
  92. order by modified desc limit 5""", {
  93. "doctype": dt,
  94. "name": dn
  95. })
  96. dl[0].fields["__assign_to"] = json.dumps(cl)