Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

api.py 2.8 KiB

11 år sedan
11 år sedan
11 år sedan
11 år sedan
11 år sedan
11 år sedan
11 år sedan
11 år sedan
11 år sedan
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. bean.run_method(webnotes.local.form_dict.run_method, **webnotes.local.form_dict)
  44. if webnotes.local.request.method=="POST":
  45. if not bean.has_permission("write"):
  46. webnotes.throw("No Permission", webnotes.PermissionError)
  47. bean.run_method(webnotes.local.form_dict.run_method, **webnotes.local.form_dict)
  48. webnotes.conn.commit()
  49. else:
  50. if name:
  51. if webnotes.local.request.method=="GET":
  52. webnotes.local.response.update({
  53. "doclist": webnotes.client.get(doctype,
  54. name)})
  55. if webnotes.local.request.method=="POST":
  56. webnotes.local.response.update({
  57. "doclist": webnotes.client.insert(webnotes.local.form_dict.doclist)})
  58. webnotes.conn.commit()
  59. if webnotes.local.request.method=="PUT":
  60. webnotes.local.response.update({
  61. "doclist":webnotes.client.save(webnotes.local.form_dict.doclist)})
  62. webnotes.conn.commit()
  63. if webnotes.local.request.method=="DELETE":
  64. webnotes.client.delete(doctype, name)
  65. webnotes.local.response.message = "ok"
  66. elif doctype:
  67. if webnotes.local.request.method=="GET":
  68. webnotes.local.response.update({
  69. "data": webnotes.call(webnotes.widgets.reportview.execute,
  70. doctype, **webnotes.local.form_dict)})
  71. else:
  72. raise webnotes.DoesNotExistError
  73. else:
  74. raise webnotes.DoesNotExistError
  75. except webnotes.DoesNotExistError, e:
  76. report_error(404)
  77. except Exception, e:
  78. report_error(500)
  79. build_response()