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.
 
 
 
 
 
 

123 lines
3.3 KiB

  1. # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
  2. # MIT License. See license.txt
  3. from __future__ import unicode_literals
  4. import frappe
  5. from frappe import _
  6. import frappe.utils
  7. import frappe.async
  8. import frappe.sessions
  9. import frappe.utils.file_manager
  10. import frappe.desk.form.run_method
  11. from frappe.utils.response import build_response
  12. def handle():
  13. """handle request"""
  14. cmd = frappe.local.form_dict.cmd
  15. if cmd!='login':
  16. execute_cmd(cmd)
  17. return build_response("json")
  18. def execute_cmd(cmd, from_async=False):
  19. """execute a request as python module"""
  20. for hook in frappe.get_hooks("override_whitelisted_methods", {}).get(cmd, []):
  21. # override using the first hook
  22. cmd = hook
  23. break
  24. method = get_attr(cmd)
  25. if from_async:
  26. method = method.queue
  27. is_whitelisted(method)
  28. ret = frappe.call(method, **frappe.form_dict)
  29. # returns with a message
  30. if ret:
  31. frappe.response['message'] = ret
  32. def is_whitelisted(method):
  33. # check if whitelisted
  34. if frappe.session['user'] == 'Guest':
  35. if (method not in frappe.guest_methods):
  36. frappe.msgprint(_("Not permitted"))
  37. raise frappe.PermissionError('Not Allowed, {0}'.format(method))
  38. if method not in frappe.xss_safe_methods:
  39. # strictly sanitize form_dict
  40. # escapes html characters like <> except for predefined tags like a, b, ul etc.
  41. for key, value in frappe.form_dict.items():
  42. if isinstance(value, basestring):
  43. frappe.form_dict[key] = frappe.utils.sanitize_html(value)
  44. else:
  45. if not method in frappe.whitelisted:
  46. frappe.msgprint(_("Not permitted"))
  47. raise frappe.PermissionError('Not Allowed, {0}'.format(method))
  48. @frappe.whitelist(allow_guest=True)
  49. def version():
  50. return frappe.__version__
  51. @frappe.whitelist()
  52. def runserverobj(method, docs=None, dt=None, dn=None, arg=None, args=None):
  53. frappe.desk.form.run_method.runserverobj(method, docs=docs, dt=dt, dn=dn, arg=arg, args=args)
  54. @frappe.whitelist(allow_guest=True)
  55. def logout():
  56. frappe.local.login_manager.logout()
  57. frappe.db.commit()
  58. @frappe.whitelist(allow_guest=True)
  59. def web_logout():
  60. frappe.local.login_manager.logout()
  61. frappe.db.commit()
  62. frappe.respond_as_web_page("Logged Out", """<p><a href="/index" class="text-muted">Back to Home</a></p>""")
  63. @frappe.whitelist(allow_guest=True)
  64. def run_custom_method(doctype, name, custom_method):
  65. """cmd=run_custom_method&doctype={doctype}&name={name}&custom_method={custom_method}"""
  66. doc = frappe.get_doc(doctype, name)
  67. if getattr(doc, custom_method, frappe._dict()).is_whitelisted:
  68. frappe.call(getattr(doc, custom_method), **frappe.local.form_dict)
  69. else:
  70. frappe.throw(_("Not permitted"), frappe.PermissionError)
  71. @frappe.whitelist()
  72. def uploadfile():
  73. try:
  74. if frappe.form_dict.get('from_form'):
  75. try:
  76. ret = frappe.utils.file_manager.upload()
  77. except frappe.DuplicateEntryError:
  78. # ignore pass
  79. ret = None
  80. frappe.db.rollback()
  81. else:
  82. if frappe.form_dict.get('method'):
  83. method = frappe.get_attr(frappe.form_dict.method)
  84. is_whitelisted(method)
  85. ret = method()
  86. except Exception:
  87. frappe.errprint(frappe.utils.get_traceback())
  88. ret = None
  89. return ret
  90. def get_attr(cmd):
  91. """get method object from cmd"""
  92. if '.' in cmd:
  93. method = frappe.get_attr(cmd)
  94. else:
  95. method = globals()[cmd]
  96. frappe.log("method:" + cmd)
  97. return method
  98. @frappe.whitelist()
  99. def ping():
  100. return "pong"