Odoo Web Controller

Source: Internet
Author: User
Tags deprecated wrappers

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
  • Route --string or array. The route part, that would determine which HTTP requests would match the decorated method. Can is a single string or an array of strings. See Werkzeug's routing documentation for the format of the route expression (http://werkzeug.pocoo.org/docs/routing/).
  • type -the type of request, can be ‘http‘ or ‘json‘ .
  • Auth --

    The type of authentication method, can on the following:

    • user: The user must is authenticated and the current request would perform using the rights of the user.
    • public: The user may or is not being authenticated. If She isn ' t, the current request would perform using the shared public user.
    • none: The method is always active and even if there is no database. Mainly used by the framework and authentication modules. There request code won't has any facilities to access the database nor has any configuration indicating the current D Atabase nor the current user.
  • methods --A sequence of HTTP methods this route applies to. If not specified, all methods is allowed.
  • cors -the access-control-allow-origin cors Directive value.

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

Mappingof 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

MappingOf the context values for the current request

session

A OpenERPSession holding the HTTP session data for the current HTTP session

cr

CursorInitialized 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(dataheaders=Nonecookies=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(templateqcontext=Nonelazy=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

    • methodis ignored
    • paramsMust 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.ResponseIn 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.