-----------------
更新时间
11:17 2016-09-18 星期日    完善讲解
17:44 2016-02-17 星期三
-----------------
* 控制器Controller
  它的功能是拦截浏览器的请求和响应数据的发送

* 一个简单的控制器
   from openerp import http
   class Mymodule(http.Controller)
       @http.route('mymodule/mymodule/', auth='public')
       def index(self,**kw)
           return "Hello, world"
           #return http.request.render("mymodule.index",{'fruits':['apple','banana','pear']})
           使用模板
           "mymodule.index":指mymodule模块下的标识为index的模板
           {'fruits':['apple','banana','pear']}: 带一个字典数据给模板

对应的模板文件
    <openerp>
        <data>
           <template id="index">  <!--定义了标识为index 的模板-->
                <title>mymodule</title>
                <t t-foreach="fruits" t-as="fruit"> <!--对带过的数据进行遍历-->
                    <p><t t-esc="fruit"></p>
                </t>
           </template>
        </data>
    </openerp>

* 数据模型
  在开始安装模块时,会预定义一些数据
  如模型定义如下:
  from openerp import models,fields,api

class  Fruits(models.Model):
      _name = 'mymodule.fruits'
      name = fields.Char()

manifest定义(__openerp__.py)如下:
  'data':[
        'security/ir.model.access.csv',
        'fruits.xml'
  ],

security/ir.model.access.csv 内容如下:
  id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
  access_mymodule_fruits,access.mymodule.fruits,model__mymodule_fruits,,1,0,0,0

fruits.xml 内容如下:
  <openerp>
    <data>
        <record id="apple" model="mymodule.fruits">
            <field name="name">apple</field>
        </record>
        <record id="'banana" model="mymodule.fruits">
            <field name="name">'banana</field>
        </record>
        <record id="pear" model="mymodule.fruits">
            <field name="name">pear</field>
        </record>
    </data>
   </openerp>

* 控制器得到数据模型定义的数据
   from openerp import http
   class Mymodule(http.Controller)
       @http.route('mymodule/mymodule/', auth='public')
       def index(self,**kw)
           Fruits = http.request.env['mymodule.fruits'] #得到 mymodule.fruits 模型

return http.request.render("mymodule.index",{
                      'fruits':Fruits.search([])  #  search([]) 全部搜索出来
              })

* 路由 (URL Patterns)
    # 带参数   
    @http.route('hello/<name>') 
    def hello(self,name, **kw){
       return "Hello,{}".format(name)   
    }
   
    #指定参数是整数
    @http.route('hello/<int:identifier>') 
    def hello(self,identifier, **kw){
       return "Hello,{}".format(identifier)   
    }
   
    #指定参数是长度为5的字符串
    @http.route('hello/<string(length=5):lang>') 
    def hello(self,lang, **kw){
       return "Hello,{}".format(lang)   
    }
   
    #指定参数是指定字符串数组之一
    @http.route('hello/<any(bird,reptil,mammal):group>/<int:id>/') 
    def hello(self,group,,id, **kw){
       return "Hello,{}".format(group,id)   
    }
       
        * string(minlength=1,maxlength=None,length=None)
        * path
        * any(*items)
        * int([fixed_digits][,min][,max])
        * float([min],[,max])
        * uuid(Werkzeug0.10)
        * model(model_name)
       
    #多重路径匹配
    @http.route(['/hello/','/hello/<int:value>']) 
    def hello(self,value, **kw){
       return "Hello,{}".format(value)   
    }    
   
    #网站支持
    @http.route('/hello/', website=True)
    def hello(self, **kw):
        return http.request.render('module.hello')
    --------
    http.request.website
    http.request.lang  得到语言
    http.request.redirect(url) 跳转
       
* 验证
    #公用,不要验证可以访问
     @http.route('/hello/','public') 
      def hello(self, **kw){
       # code   
    }

#要登录才能访问,否则跳到登录页面
     @http.route('/hello/','user') 
      def hello(self, **kw){
       # code   
    }
   
    #request.uid=none
     @http.route('/hello/','none') 
      def hello(self, **kw){
       # code   
     }
    
    # 采用 GET 模式访问
     @http.route('/hello/', methods=['GET']) 
      def hello(self, **kw){
       # code   
     }
    
     # 采用 POST 模式访问
     @http.route('/hello/', methods=['POST']) 
      def hello(self, **kw){
       # code   
     }
   
   
* 请求
    class openerp.http.WebRequest(httprequest)
    封装的属性
        httprequest: werkzeug.wrappers.Request 原始的request
        params:映射的参数
        env: 当前环境
        context:上下文
        session: 会话
        cr: 数据库操作句柄
        debug:当前是否调试模式
        db:当前连接的数据库
       
    class openerp.http.HttpRequest(*args)
            make_response(data, headers=None, cookies=None)
            not_found(description=None) 404页面的响应
            render(template, qcontext=None, lazy=True, **kw) 带上模板渲染
           
    class openerp.http.JsonRequest(*args)
    返回json   
   
* 响应
    class openerp.http.Response(*args, **kw)
        *args 为
            template (basestring) -- 指定的模板名
            qcontext (dict) -- 渲染的上下文
            uid (int) -- 渲染到模板上的用户id
           
    可用的方法
        render() 渲染指定模板
        flatten() 强制渲染,没有采用模板
           
   
* 控制器
    class openerp.http.Controller
   
    class MyController(openerp.http.Controller):
    @route('/some_url', auth='public')
    def handler(self):
        return stuff()
       
    覆盖:
    class Extension(MyController):
    @route()
    def handler(self):
        do_before()
        return super(Extension, self).handler()
       
*代码分析
    大部分操作用openerp/http.py中的方法
   
    class Home(http.Controller):

#首页 跳转到/web
    @http.route('/', type='http', auth="none")
    def index(self, s_action=None, db=None, **kw):
        return http.local_redirect('/web', query=request.params, keep_hash=True)
    ....
    # 跳转
          def local_redirect(path, query=None, keep_hash=False, forward_debug=True, code=303):
          这是http.py中的,封装了 werkzeug的跳转
          def redirect(location, code=302) 这个是werkzeug中的
    #得到模型
          request.registry['模型名']
          menu_data = request.registry['ir.ui.menu'].load_menus(request.cr, request.uid, context=request.context)

(10)odoo控制器操作的更多相关文章

  1. ASP.NET Core 中文文档 第四章 MVC(4.2)控制器操作的路由

    原文:Routing to Controller Actions 作者:Ryan Nowak.Rick Anderson 翻译:娄宇(Lyrics) 校对:何镇汐.姚阿勇(Dr.Yao) ASP.NE ...

  2. thinkphp使用模块/控制器/操作访问时出现No input file specified.解决方式

    thinkphp使用 http://serverName/index.php/模块/控制器/操作 访问时,出现了 No input file specified. 的错误 解决办法: 一: 开启cgi ...

  3. threejs绕轴转,粒子系统,控制器操作等(二)

    前言:threejs系列的第二篇文章,也是一边学习一边总结: 1,一个物体绕着另一个物体转动 上一篇文中主要是物体自转,为了描述一个一个物体绕另一个物体转,这里我描述了一个月球绕地球公转,并且自转的场 ...

  4. 背水一战 Windows 10 (88) - 文件系统: 操作文件夹和文件

    [源码下载] 背水一战 Windows 10 (88) - 文件系统: 操作文件夹和文件 作者:webabcd 介绍背水一战 Windows 10 之 文件系统 创建文件夹,重命名文件夹,删除文件夹, ...

  5. Android:日常学习笔记(10)———使用LitePal操作数据库

    Android:日常学习笔记(10)———使用LitePal操作数据库 引入LitePal 什么是LitePal LitePal是一款开源的Android数据库框架,采用了对象关系映射(ORM)的模式 ...

  6. ThinkPHP3开发模式,控制器操作,配置文件,框架语法

    ThinkPHP的开发模式 tp框架有两种使用模式:开发模式(调试模式),一种是生产模式(运行模式) define('APP_DEBUG', true);  //调试模式 define('APP_DE ...

  7. (47) odoo详细操作手册

    odoo 8 详细操作手册, ERP(Odoo8.0)操作手册-v1.10(陈伟明).pdf 链接: http://pan.baidu.com/s/1hsp0bVQ 密码: r9tt 花了将近9个月时 ...

  8. 动态单链表的传统存储方式和10种常见操作-C语言实现

    顺序线性表的优点:方便存取(随机的),特点是物理位置和逻辑为主都是连续的(相邻).但是也有不足,比如:前面的插入和删除算法,需要移动大量元素,浪费时间,那么链式线性表 (简称链表) 就能解决这个问题. ...

  9. (04)odoo视图操作

    -----------------更新时间19:04 2016-09-29 星期四11:17 2016-09-18 星期日18:13 2016-04-05 星期二15:05 2016-03-14 星期 ...

随机推荐

  1. Spring MVC 详解(二)

    前端控制器 在web.xml中配置: 在springmvc.xml中配置springmvc架构三大组件(处理器映射器.适配器.视图解析器) 处理器映射器 在springmvc.xml中配置: Bean ...

  2. acdream1197 Points In Cuboid

    题目链接:http://acdream.info/problem?pid=1197 题意:给出一些点.每次给出一个长方体,问在长方体中的点的个数. 思路:kd-tree. const int N=11 ...

  3. sql 执行计划

    SQL Server执行计划的理解 要理解执行计划,怎么也得先理解,那各种各样的名词吧.鉴于自己还不是很了解.本文打算作为只写懂的,不懂的懂了才写. 在开头要先说明,第一次看执行计划要注意,SQL S ...

  4. Spring管理Bean的三种创建方式

    1.使用类构造器实例化  (90%以上都是使用这种) <bean id=“orderService" class="cn.itcast.OrderServiceBean&qu ...

  5. Scrum Meeting---Eight(2015-11-3)

    今日已完成任务和明日要做的任务 姓名 今日已完成任务 今日时间 明日计划完成任务 估计用时 董元财 数据库与客户端通讯 5h 服务器与客户端通讯测试 4h 胡亚坤 好友添加 2h 好友添加 2h 刘猛 ...

  6. nginx+nginx-rtmp-module+ffmpeg搭建流媒体服务器

    参照网址: [1]http://blog.csdn.net/redstarofsleep/article/details/45092147 [2]HLS介绍:http://www.cnblogs.co ...

  7. 【51nod】1376 最长递增子序列的数量

    数组A包含N个整数(可能包含相同的值).设S为A的子序列且S中的元素是递增的,则S为A的递增子序列.如果S的长度是所有递增子序列中最长的,则称S为A的最长递增子序列(LIS).A的LIS可能有很多个. ...

  8. MyISAM与InnoDB的索引实现

    1.MyISAM 使用B+Tree 作为索引结构,叶子节点的data存放指针,也就是记录的地址.对于主键索引和辅助索引都是一样的.2.InnoDB 也使用B+Tree作为索引结构,也别需要注意的是,对 ...

  9. JavaScript基础知识点

    本书目录 第一章:  JavaScript语言基础 第二章:  JavaScript内置对象第三章:  窗口window对象第四章:  文档document对象第五章:  表单form对象第六章:   ...

  10. MyBatis——调用存储过程

    原文:http://www.cnblogs.com/xdp-gacl/p/4270352.html 一.提出需求 查询得到男性或女性的数量, 如果传入的是0就女性否则是男性 二.准备数据库表和存储过程 ...