Routing
openerp.http.route(route=None, **kw)
Decorator marking the decorated method as being a handler for requests. The method must is part of a subclass of Controller
.
Parameters
Request
The request object is automatically set on at the start of the openerp.http.request
request
class openerp.http.WebRequest(httprequest)
Parent class for all Odoo Web request types, mostly deals with initialization and setup of the Request object (the DISPATC Hing itself have to being handled by the subclasses)
Parameters
HttpRequest(
werkzeug.wrappers.BaseRequest
)--A wrapped Werkzeug Request object
httprequest
The original werkzeug.wrappers.Request
object provided to the request
params
Mapping
of request parameters, not generally useful as they ' re provided directly to the handler method as keyword arguments
env
The Environment
bound to current request. Raises a if the current requests are not RuntimeError
bound to a database.
context
Mapping
Of the context values for the current request
session
A OpenERPSession
holding the HTTP session data for the current HTTP session
cr
Cursor
Initialized for the current method call.
Accessing the cursor when the current request uses the none
authentication would raise an exception.
debug
Indicates whether the current request was in "Debug" mode
session_id
Opaque identifier for the instance of the current OpenERPSession
request
Deprecated since version 8.0:use the sid
attribute onsession
registry
The registry to the database linked to this request. Can is None
if the current request uses the none
authentication.
Deprecated since version 8.0:useenv
db
The database linked to this request. Can is None
if the current request uses the none
authentication.
httpsession
HTTP Session Data
Deprecated since version 8.0:use session
instead.
class openerp.http.HttpRequest(*args)
Handler for the http
request type.
Matched routing parameters, query string parameters, form parameters and files is passed to the handler method as keyword Arguments.
In the case of name conflict, routing parameters has priority.
The handler method ' s result can be:
- A falsy value, in which case the HTTP response would be is an HTTP 204 (No Content)
- A Werkzeug Response object, which is returned As-is
- A
str
or unicode
, would be is wrapped in a Response object and interpreted as HTML
make_response(data, headers=None, cookies=None)
Helper for non-html responses, or HTML responses with custom response headers or cookies.
While handlers can just return the HTML markup of a page they want to send as a string if non-html data is returned they n Eed to create a complete response object, or the returned data is not being correctly interpreted by the clients.
Parameters
- Data (
basestring
)--response body
- Headers (
[(name, value)]
)--HTTP headers to set on the response
- Cookies (
collections.Mapping
)--cookies to set on the client
not_found(description=None)
Shortcut for an HTTP 404 (not Found) response
render(template, qcontext=None, lazy=True, **kw)
Lazy render of a QWeb template.
The actual rendering of the given template would occur at and then end of the dispatching. Meanwhile, the template and/or qcontext can be altered or even replaced by a static response.
Parameters
- Template (
basestring
)--template to render
- Qcontext (
dict
)--Rendering context to use
- Lazy (
bool
)--whether the template rendering should is deferred until the last possible moment
- KW --forwarded to Werkzeug ' s Response object
class openerp.http.JsonRequest(*args)
Request handler for JSON-RPC 2 over HTTP
method
is ignored
params
Must be a JSON object (not an array) and is passed as keyword arguments to the handler method
- The handler method ' s result is returned as JSON-RPC and wrapped in the
result
json-rpc Response
Sucessful Request:
--{"JSONRPC": "2.0", "method": "Call", "params": {"context": {}, "Arg1": "Val1"}, "id": null} <--{"Jsonrpc": "2.0", "result": {"res1": "Val1"}, "id": null}
Request Producing a error:
--{"JSONRPC": "2.0", "method": "Call", "params": {"context": {}, "Arg1": "Val1"}, "id": null} <--{"Jsonrpc": "2.0", "error": {"code": 1, "message": "End user error message.", "data": {"code": " Codestring ", " Debug ":" Traceback "}}, " id ": null}
Response
class openerp.http.Response(*args, **kw)
Response object passed through controller route chain.
werkzeug.wrappers.Response
In addition to the parameters, this class ' s constructor can take the following additional parameters for QWeb Lazy R Endering.
Parameters
- Template (
basestring
)--template to render
- Qcontext (
dict
)--Rendering context to use
- UID (
int
)--User ID to use for the Ir.ui.view render call, to use the None
request ' s User (the default)
These attributes is available as parameters on the Response object and can is altered at any time before rendering
Also exposes all the attributes and methods of werkzeug.wrappers.Response
.
render()
Renders the Response ' s template, returns the result
flatten()
Forces the rendering of the response ' s template, sets the result as response body and unsetstemplate
Controllers
Controllers need to provide extensibility, much like Model
, but can ' t use the same mechanism as the pre-requisites (a data Base with loaded modules) may is available yet (e.g. no database created, or no database selected).
Controllers thus provide their own extension mechanism, separate from that of models:
Controllers is created by inheriting
class openerp.http.Controller
And defining methods decorated with route()
:
Mycontroller(openerp). HTTP. Controller@route('/some_url 'auth=' public 'handler(selfStuff ()
To override a controller, the inherit from its class and override relevant methods, re-exposing them if necessary:
Extension(mycontroller): @routehandler(selfdo_beforeSuper( Extensionself). Handler()
- Decorating
route()
with was necessary to keep the method (and route) Visible:if the method is redefined without decorating, It'll be "unpublished"
The decorators of all methods is combined, if the overriding method ' s decorator have no argument all previous ones would be Kept, any provided argument would override previously defined ones e.g.:
Restrict(mycontroller): @route(auth=' user 'handler(self Super(Restrictself). Handler()
Would change from public /some_url
authentication to user (requiring a log-in)
Odoo Web Controller