odoo web controller

Routing

openerp.http.route(route=None**kw)

Decorator marking the decorated method as being a handler for requests. The method must be part of a subclass of Controller.

Parameters

  • route -- string or array. The route part that will determine which http requests will match the decorated method. Can be a single string or an array of strings. See werkzeug‘s routing documentation for the format of 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 of the following:

    • user: The user must be authenticated and the current request will perform using the rights of the user.
    • public: The user may or may not be authenticated. If she isn‘t, the current request will perform using the shared Public user.
    • none: The method is always active, even if there is no database. Mainly used by the framework and authentication modules. There request code will not have any facilities to access the database nor have any configuration indicating the current database nor the current user.
  • methods -- A sequence of http methods this route applies to. If not specified, all methods are allowed.
  • cors -- The Access-Control-Allow-Origin cors directive value.

Request

The request object is automatically set on openerp.http.request at the start of the 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 dispatching itself has to be 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 RuntimeError if the current requests is not bound to a database.

context

Mapping of context values for the current request

session

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 will raise an exception.

debug

Indicates whether the current request is in "debug" mode

session_id

opaque identifier for the OpenERPSession instance of the current request

Deprecated since version 8.0:Use the sid attribute on session

registry

The registry to the database linked to this request. Can be None if the current request uses the none authentication.

Deprecated since version 8.0:use env

db

The database linked to this request. Can be 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 are passed to the handler method as keyword arguments.

In case of name conflict, routing parameters have priority.

The handler method‘s result can be:

  • a falsy value, in which case the HTTP response will be an HTTP 204 (No Content)
  • a werkzeug Response object, which is returned as-is
  • str or unicode, will be 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 need to create a complete response object, or the returned data will not be 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 a HTTP 404 (Not Found) response

render(templateqcontext=Nonelazy=True**kw)

Lazy render of a QWeb template.

The actual rendering of the given template will occur at 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 be 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 result and wrapped in the 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.

In addition to the werkzeug.wrappers.Response parameters, this class‘s constructor can take the following additional parameters for QWeb Lazy Rendering.

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, None to use the request‘s user (the default)

these attributes are available as parameters on the Response object and can be 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 unsets template

Controllers

Controllers need to provide extensibility, much like Model, but can‘t use the same mechanism as the pre-requisites (a database with loaded modules) may not be available yet (e.g. no database created, or no database selected).

Controllers thus provide their own extension mechanism, separate from that of models:

Controllers are created by inheriting from

class openerp.http.Controller

and defining methods decorated with route():

class MyController(openerp.http.Controller):
    @route(‘/some_url‘, auth=‘public‘)
    def handler(self):
        return stuff()

To override a controller, inherit from its class and override relevant methods, re-exposing them if necessary:

class Extension(MyController):
    @route()
    def handler(self):
        do_before()
        return super(Extension, self).handler()
  • decorating with route() is necessary to keep the method (and route) visible: if the method is redefined without decorating, it will be "unpublished"
  • the decorators of all methods are combined, if the overriding method‘s decorator has no argument all previous ones will be kept, any provided argument will override previously defined ones e.g.:
    class Restrict(MyController):
        @route(auth=‘user‘)
        def handler(self):
            return super(Restrict, self).handler()
    

    will change /some_url from public authentication to user (requiring a log-in)

时间: 2024-10-10 01:16:38

odoo web controller的相关文章

Odoo Web Service API

来自 Odoo Web服务暴露出相关的服务,路由分别是 /xmlrpc/ /xmlrpc/2/ /jsonrpc 根据 services 调用 后端对应服务的 方法method [定义 openerp\http.py 之 dispatch_rpc()],然后再将结果从python dict 转换为 xml-rpc 格式 或者 json-rpc 返回    service 对应的后端服务分别是 common, openerp.service.common db,openerp.service.db

odoo10学习笔记十二:web controller

原文地址:http://www.cnblogs.com/ygj0930/p/7151551.html 一:路由 odoo.http.route(route=None, **kw) 装饰器可以将对应方法装饰为处理对应的http请求,该方法须是Controller的子类. route -- 字符串或数组,决定哪个http请求匹配所装饰的方法,可以是单个字符串.或多个字符串的数组 type -- 请求的类型,可以是http或json auth -- 认证方法的类型,可以是以下几种: user - 必须

OpenERP|odoo Web开发

在OpenERP 7 和 Odoo 8下测试均可. 1.相关库/框架 主要:jQuery(使用1.8.3,如果使用新版本,其他jQuery插件也要升级或修改).Underscore.Qweb 其他:都在addons\web\static\lib路径下. 2.示例框架 下载(需要先安装bzr):bzr branch lp:~niv-openerp/+junk/oepetstore -r 1 下载后将路径加到OpenERP服务器的addons_path参数中,重启服务器.更新模块列表再安装. 在__

Odoo 中的 Controller

来自  Odoo处理HTTP请求的接口用的Contoller类,封装于web模块中. --------------------------------------------------------------- RequestHandler: 1. replace_request_password(args):用*替换掉request中的密码字符. 2. dispatch_rpc(service_name, method, params):处理RPC请求.service_name的值可取com

打造一款属于自己的web服务器——配置controller

这天一热,胖子的的世界就是一片凄惨啊,随便动动身子,就跟洗个澡似得,心情固然烦躁,一烦躁就很难静下心来写东西了......所以这一段没咋用心写,就稍微水点吧,同时,我又打算要减肥了!>_<!. 上一次我们介绍了session的实现,使web服务器(现在总觉得准确来说应该叫可独立部署的web框架,称不上服务器)具备了基本功能,但是仔细一想就会发现一个严重的问题:每当实现一个新的controller,那么就需要在invokController方法里边增加判断,以便url能够找到对应controll

Spring Boot 学习笔记1---初体验之3分钟启动你的Web应用

前言 早在去年就简单的使用了一下Spring Boot,当时就被其便捷的功能所震惊.但是那是也没有深入的研究,随着其在业界被应用的越来越广泛,因此决定好好地深入学习一下,将自己的学习心得在此记录,本文主要围绕以下几点进行说明: Spring Boot 简介 使用Spring Boot快速搭建一个Web应用如有不对的地方,请指正. 1. Spring Boot简介 Spring Boot是一个基于Spring的衍生框架,其主要的目的是帮助我们快速构建独立.生产级别的Spring的应用,其崇尚的理念

基于Spring+SpringMVC+Mybatis的Web系统搭建

主要的后端架构:Spring+SpringMVC+Mybatis+Shiro+Maven  IDE:IntelliJ IDEA 15.0.2 jdk:1.8.0_66 系统完整源码 https://github.com/Wellat/Factor 系统目录结构 跑起来效果 搭建步骤 1.用Idea创建maven项目 2.配置pom.xml文件,添加依赖 1 <?xml version="1.0" encoding="UTF-8"?> 2 <proj

基于Spring + Spring MVC + Mybatis + shiro 高性能web构建

一直想写这篇文章,前段时间 痴迷于JavaScript.NodeJs.AngularJS,做了大量的研究,对前后端交互有了更深层次的认识. 今天抽个时间写这篇文章,我有预感,这将是一篇很详细的文章,详细的配置,详细的注释,看起来应该很容易懂. 用最合适的技术去实现,并不断追求最佳实践.这就是架构之道. 希望这篇文章能给你们带来一些帮助,同时希望你们可以为这个项目贡献你的想法. 源码地址:https://github.com/starzou/quick4j 点击打开 看我们的项目结构: 是一个典型

java: web应用中不经意的内存泄露

前面有一篇讲解如何在spring mvc web应用中一启动就执行某些逻辑,今天无意发现如果使用不当,很容易引起内存泄露,测试代码如下: 1.定义一个类App package com.cnblogs.yjmyzz.web.controller; import java.util.Date; public class App { boolean isRun = false; public App() { isRun = true; } public void start() { while (is