_routes = ["/api/<model>", "/api/<model>/<id>", "/api/<model>/<id>/<action>"]
@validate_token
@http.route(_routes, type="http", auth="none", methods=["PATCH"], csrf=False)
def patch(self, model=None, id=None, action=None, **payload):
"""."""
try:
_id = int(id)
except Exception as e:
return invalid_response(
"invalid object id", "invalid literal %s for id with base " % id
)
try:
record = request.env[model].sudo().search([("id", "=", _id)])
_callable = action in [
method for method in dir(record) if callable(getattr(record, method))
]
if record and _callable:
# action is a dynamic variable.
getattr(record, action)()
else:
return invalid_response(
"missing_record",
"record object with id %s could not be found or %s object has no method %s"
% (_id, model, action),
404,
)
except Exception as e:
return invalid_response("exception", e, 503)
else:
return valid_response("record %s has been successfully patched" % record.id)
原文:https://www.cnblogs.com/qianxunman/p/12664105.html