選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

98 行
3.1 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
  24. from webnotes import _
  25. @webnotes.whitelist()
  26. def runserverobj():
  27. """
  28. Run server objects
  29. """
  30. import webnotes.model.code
  31. from webnotes.model.bean import Bean
  32. from webnotes.utils import cint
  33. wrapper = None
  34. method = webnotes.form_dict.get('method')
  35. arg = webnotes.form_dict.get('arg')
  36. dt = webnotes.form_dict.get('doctype')
  37. dn = webnotes.form_dict.get('docname')
  38. webnotes.response["docs"] = []
  39. if dt: # not called from a doctype (from a page)
  40. if not dn: dn = dt # single
  41. so = webnotes.model.code.get_obj(dt, dn)
  42. else:
  43. wrapper = Bean()
  44. wrapper.from_compressed(webnotes.form_dict.get('docs'), dn)
  45. if not wrapper.has_read_perm():
  46. webnotes.msgprint(_("No Permission"), raise_exception = True)
  47. so = wrapper.make_obj()
  48. wrapper.check_if_latest(method="runserverobj")
  49. check_guest_access(so.doc)
  50. if so:
  51. r = webnotes.model.code.run_server_obj(so, method, arg)
  52. if r:
  53. #build output as csv
  54. if cint(webnotes.form_dict.get('as_csv')):
  55. make_csv_output(r, so.doc.doctype)
  56. else:
  57. webnotes.response['message'] = r
  58. from webnotes.widgets.form.load import add_file_list
  59. add_file_list(so.doc.doctype, so.doc.name, so.doclist)
  60. webnotes.response['docs'] += so.doclist
  61. def check_guest_access(doc):
  62. if webnotes.session['user']=='Guest' and not webnotes.conn.sql("select name from tabDocPerm where role='Guest' and parent=%s and ifnull(`read`,0)=1", doc.doctype):
  63. webnotes.msgprint("Guest not allowed to call this object")
  64. raise Exception
  65. def make_csv_output(res, dt):
  66. """send method response as downloadable CSV file"""
  67. import webnotes
  68. from cStringIO import StringIO
  69. import csv
  70. f = StringIO()
  71. writer = csv.writer(f)
  72. for r in res:
  73. row = []
  74. for v in r:
  75. if isinstance(v, basestring):
  76. v = v.encode("utf-8")
  77. row.append(v)
  78. writer.writerow(row)
  79. f.seek(0)
  80. webnotes.response['result'] = unicode(f.read(), 'utf-8')
  81. webnotes.response['type'] = 'csv'
  82. webnotes.response['doctype'] = dt.replace(' ','')