Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

98 lignes
2.8 KiB

  1. # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
  2. # MIT License. See license.txt
  3. import webnotes
  4. import webnotes.handler
  5. import webnotes.client
  6. import webnotes.widgets.reportview
  7. from webnotes.utils.response import build_response, report_error
  8. def handle():
  9. """
  10. /api/method/{methodname} will call a whitelisted method
  11. /api/resource/{doctype} will query a table
  12. examples:
  13. ?fields=["name", "owner"]
  14. ?filters=[["Task", "name", "like", "%005"]]
  15. ?limit_start=0
  16. ?limit_page_length=20
  17. /api/resource/{doctype}/{name} will point to a resource
  18. GET will return doclist
  19. POST will insert
  20. PUT will update
  21. DELETE will delete
  22. /api/resource/{doctype}/{name}?run_method={method} will run a whitelisted controller method
  23. """
  24. parts = webnotes.request.path[1:].split("/")
  25. call = doctype = name = None
  26. if len(parts) > 1:
  27. call = parts[1]
  28. if len(parts) > 2:
  29. doctype = parts[2]
  30. if len(parts) > 3:
  31. name = parts[3]
  32. try:
  33. if call=="method":
  34. webnotes.local.form_dict.cmd = doctype
  35. webnotes.handler.handle()
  36. return
  37. elif call=="resource":
  38. if "run_method" in webnotes.local.form_dict:
  39. bean = webnotes.bean(doctype, name)
  40. if webnotes.local.request.method=="GET":
  41. if not bean.has_permission("read"):
  42. webnotes.throw("No Permission", webnotes.PermissionError)
  43. webnotes.local.response.update({"data": bean.run_method(webnotes.local.form_dict.run_method,
  44. **webnotes.local.form_dict)})
  45. if webnotes.local.request.method=="POST":
  46. if not bean.has_permission("write"):
  47. webnotes.throw("No Permission", webnotes.PermissionError)
  48. webnotes.local.response.update({"data":bean.run_method(webnotes.local.form_dict.run_method,
  49. **webnotes.local.form_dict)})
  50. webnotes.conn.commit()
  51. else:
  52. if name:
  53. if webnotes.local.request.method=="GET":
  54. webnotes.local.response.update({
  55. "doclist": webnotes.client.get(doctype,
  56. name)})
  57. if webnotes.local.request.method=="POST":
  58. webnotes.local.response.update({
  59. "doclist": webnotes.client.insert(webnotes.local.form_dict.doclist)})
  60. webnotes.conn.commit()
  61. if webnotes.local.request.method=="PUT":
  62. webnotes.local.response.update({
  63. "doclist":webnotes.client.save(webnotes.local.form_dict.doclist)})
  64. webnotes.conn.commit()
  65. if webnotes.local.request.method=="DELETE":
  66. webnotes.client.delete(doctype, name)
  67. webnotes.local.response.message = "ok"
  68. elif doctype:
  69. if webnotes.local.request.method=="GET":
  70. webnotes.local.response.update({
  71. "data": webnotes.call(webnotes.widgets.reportview.execute,
  72. doctype, **webnotes.local.form_dict)})
  73. else:
  74. raise Exception("Bad API")
  75. else:
  76. raise Exception("Bad API")
  77. except Exception, e:
  78. report_error(500)
  79. build_response()