Commit 2b887aea authored by Titouan Soulard's avatar Titouan Soulard

erp5_action_information_api: move API content to script

parent e306930e
...@@ -119,49 +119,24 @@ class ActionInformationAPI(XMLObject): ...@@ -119,49 +119,24 @@ class ActionInformationAPI(XMLObject):
def handleRequest(self, request): def handleRequest(self, request):
portal = self.getPortalObject() portal = self.getPortalObject()
view_ref = self.getApiTypeReference()
(hyperdocument, actions) = self.getTypeInfo().getDefaultViewFor(self, view=self.getApiTypeReference())(self, portal, request)
response = request.RESPONSE
request_content_type = request.getHeader("content-type") request_content_type = request.getHeader("content-type")
if view_ref is None:
raise NotImplementedError("Cannot find matching view")
if request_content_type and request_content_type != "application/json": if request_content_type and request_content_type != "application/json":
raise NotImplementedError("Cannot serve Content-Type {}".format(request_content_type)) raise NotImplementedError("Cannot serve Content-Type {}".format(request_content_type))
response.setHeader("Content-Type", "application/json") request.RESPONSE.setHeader("Content-Type", "application/json")
if request.get("method").lower() != "post":
return hyperdocument
request_json = request.get("BODY")
if not request_json:
return hyperdocument
request_body = json.loads(request_json) raw_reponse = self.getTypeInfo().getDefaultViewFor(self, view=view_ref)(self, portal, request)
request_schema = request_body.get("$schemaDocument") return json.dumps(raw_reponse, indent=2).encode()
request_object_ref = request_body.get("reference")
if not request_schema or not request_object_ref:
return hyperdocument
found_action = ""
for schema_url in actions:
if schema_url in request_schema:
found_action = actions[schema_url]
break
if not found_action:
raise NotFound("Schema {} unavailable".format(request_schema))
portal_type = found_action.getParentValue().getId()
request_object = portal.portal_catalog.getResultValue(portal_type=portal_type, id=request_object_ref)
if not request_object:
raise NotFound("Object {} not found".format(request_object_ref))
action_output = request_object.getTypeInfo().getDefaultViewFor(request_object, view=found_action.getProperty("reference"))(request_body)
action_output["status"] = 200
return json.dumps(action_output, indent=2).encode()
def publishTraverse(self, request, name): def publishTraverse(self, request, name):
if request.method.upper() in ('PUT', 'DELETE'):
# PUT and DELETE methods, because they are handled as WebDAV before custom hooks are called
return ActionInformationAPIPublisher(self, request)
adapter = DefaultPublishTraverse(self, request) adapter = DefaultPublishTraverse(self, request)
try: try:
obj = adapter.publishTraverse(request, name) obj = adapter.publishTraverse(request, name)
......
import json import json
from zExceptions import NotFound
url = request.getURL() url = request.getURL()
base_url_absolute = portal.portal_callables.absolute_url().strip() base_url_absolute = portal.portal_callables.absolute_url().strip()
...@@ -11,7 +12,7 @@ raw_action_list = portal.portal_catalog( ...@@ -11,7 +12,7 @@ raw_action_list = portal.portal_catalog(
) )
links = [] links = []
action_dict = {} actions = {}
for item in raw_action_list: for item in raw_action_list:
# XXX: for now, expects reference to be the same as action URL # XXX: for now, expects reference to be the same as action URL
...@@ -24,8 +25,38 @@ for item in raw_action_list: ...@@ -24,8 +25,38 @@ for item in raw_action_list:
"url": url "url": url
}) })
action_dict[base_url_relative + action_url + "/getInputJSONSchema"] = item actions[base_url_relative + action_url + "/getInputJSONSchema"] = item
hyperdocument = json.dumps({ "links": links }, indent=2).encode() hyperdocument = { "links": links }
return (hyperdocument, action_dict) if request.get("method").lower() != "post":
return hyperdocument
request_json = request.get("BODY")
if not request_json:
return hyperdocument
request_body = json.loads(request_json)
request_schema = request_body.get("$schemaDocument")
request_object_ref = request_body.get("reference")
if not request_schema or not request_object_ref:
return hyperdocument
found_action = ""
for schema_url in actions:
if schema_url in request_schema:
found_action = actions[schema_url]
break
if not found_action:
raise NotFound("Schema {} unavailable".format(request_schema))
portal_type = found_action.getParentValue().getId()
request_object = portal.portal_catalog.getResultValue(portal_type=portal_type, id=request_object_ref)
if not request_object:
raise NotFound("Object {} not found".format(request_object_ref))
action_output = request_object.getTypeInfo().getDefaultViewFor(request_object, view=found_action.getProperty("reference"))(request_body)
action_output["status"] = 200
return action_output
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment