25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

123 lines
3.2 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 json
  5. import webnotes
  6. import webnotes.utils
  7. import webnotes.sessions
  8. import webnotes.utils.file_manager
  9. import webnotes.widgets.form.run_method
  10. from webnotes.utils.response import build_response, report_error
  11. @webnotes.whitelist(allow_guest=True)
  12. def startup():
  13. webnotes.response.update(webnotes.sessions.get())
  14. @webnotes.whitelist()
  15. def runserverobj(arg=None):
  16. webnotes.widgets.form.run_method.runserverobj()
  17. @webnotes.whitelist(allow_guest=True)
  18. def logout():
  19. webnotes.local.login_manager.logout()
  20. @webnotes.whitelist(allow_guest=True)
  21. def web_logout():
  22. webnotes.local.login_manager.logout()
  23. webnotes.conn.commit()
  24. webnotes.repsond_as_web_page("Logged Out", """<p>You have been logged out.</p>
  25. <p><a href='index'>Back to Home</a></p>""")
  26. @webnotes.whitelist(allow_guest=True)
  27. def run_custom_method(doctype, name, custom_method):
  28. """cmd=run_custom_method&doctype={doctype}&name={name}&custom_method={custom_method}"""
  29. bean = webnotes.bean(doctype, name)
  30. controller = bean.get_controller()
  31. if getattr(controller, custom_method, webnotes._dict()).is_whitelisted:
  32. webnotes.call(getattr(controller, custom_method), **webnotes.local.form_dict)
  33. else:
  34. webnotes.throw("Not Allowed")
  35. @webnotes.whitelist()
  36. def uploadfile():
  37. try:
  38. if webnotes.form_dict.get('from_form'):
  39. try:
  40. ret = webnotes.utils.file_manager.upload()
  41. except webnotes.DuplicateEntryError, e:
  42. # ignore pass
  43. ret = None
  44. webnotes.conn.rollback()
  45. else:
  46. if webnotes.form_dict.get('method'):
  47. ret = webnotes.get_attr(webnotes.form_dict.method)()
  48. except Exception, e:
  49. webnotes.errprint(webnotes.utils.get_traceback())
  50. ret = None
  51. return ret
  52. def handle():
  53. """handle request"""
  54. cmd = webnotes.local.form_dict.cmd
  55. if cmd!='login':
  56. status_codes = {
  57. webnotes.PermissionError: 403,
  58. webnotes.AuthenticationError: 401,
  59. webnotes.DoesNotExistError: 404,
  60. webnotes.SessionStopped: 503,
  61. webnotes.OutgoingEmailError: 501
  62. }
  63. try:
  64. execute_cmd(cmd)
  65. except Exception, e:
  66. report_error(status_codes.get(e.__class__, 500))
  67. else:
  68. if webnotes.local.request.method in ("POST", "PUT") and webnotes.conn:
  69. webnotes.conn.commit()
  70. build_response()
  71. if webnotes.conn:
  72. webnotes.conn.close()
  73. if webnotes._memc:
  74. webnotes._memc.disconnect_all()
  75. def execute_cmd(cmd):
  76. """execute a request as python module"""
  77. method = get_attr(cmd)
  78. # check if whitelisted
  79. if webnotes.session['user'] == 'Guest':
  80. if (method not in webnotes.guest_methods):
  81. raise webnotes.PermissionError('Not Allowed, %s' % str(method))
  82. else:
  83. if not method in webnotes.whitelisted:
  84. webnotes._response.status_code = 403
  85. webnotes.msgprint('Not Allowed, %s' % str(method))
  86. raise webnotes.PermissionError('Not Allowed, %s' % str(method))
  87. ret = webnotes.call(method, **webnotes.form_dict)
  88. # returns with a message
  89. if ret:
  90. webnotes.response['message'] = ret
  91. # update session
  92. if "session_obj" in webnotes.local:
  93. webnotes.local.session_obj.update()
  94. def get_attr(cmd):
  95. """get method object from cmd"""
  96. if '.' in cmd:
  97. method = webnotes.get_attr(cmd)
  98. else:
  99. method = globals()[cmd]
  100. webnotes.log("method:" + cmd)
  101. return method