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ů.
 
 
 
 
 
 

95 řádky
3.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
  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. webnotes.response['docs'] += so.doclist
  59. def check_guest_access(doc):
  60. 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):
  61. webnotes.msgprint("Guest not allowed to call this object")
  62. raise Exception
  63. def make_csv_output(res, dt):
  64. """send method response as downloadable CSV file"""
  65. import webnotes
  66. from cStringIO import StringIO
  67. import csv
  68. f = StringIO()
  69. writer = csv.writer(f)
  70. for r in res:
  71. row = []
  72. for v in r:
  73. if isinstance(v, basestring):
  74. v = v.encode("utf-8")
  75. row.append(v)
  76. writer.writerow(row)
  77. f.seek(0)
  78. webnotes.response['result'] = unicode(f.read(), 'utf-8')
  79. webnotes.response['type'] = 'csv'
  80. webnotes.response['doctype'] = dt.replace(' ','')