跳转至内容
  • 版块
  • 标签
  • 热门
  • 用户
  • 群组
皮肤
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • 默认(Flatly)
  • 不使用皮肤
折叠

Odoo 中文社区

  1. 主页
  2. 版块
  3. Odoo 开发与实施交流
  4. 远程访问Openerp 对象,oersted的应用实例介绍

远程访问Openerp 对象,oersted的应用实例介绍

已定时 已固定 已锁定 已移动 Odoo 开发与实施交流
8 帖子 4 发布者 10.8k 浏览
  • 从旧到新
  • 从新到旧
  • 最多赞同
登录后回复
此主题已被删除。只有拥有主题管理权限的用户可以查看。
  • digitalsatoriD 离线
    digitalsatoriD 离线
    digitalsatori 管理员
    写于 最后由 编辑
    #1

    我们知道我们可以通过xml-rpc或net-rpc来远程访问openerp服务器中的对象并对其做相应的操作,但是在实际应用中并不是非常方便,于是诞生了各种各样的封装程序以简化操作。比如:用ruby开发的 https://github.com/rvalyi/ooor ,python开发的 https://github.com/lasarux/ooop , http://pypi.python.org/pypi/oersted/1.0 等等, 最近甚至还有人通过ooop作为与openerp通讯的桥梁开发了一个叫 https://launchpad.net/zoook 的网店系统。本文介绍的是对oersted的应用。

    [b]应用介绍[/b]:

    [b]product.product 和 product.template:[/b]

    OpenERP中默认内置了product.product 和 product.template对象,product.product通过 “实例继承”([b]_inherits[/b]) 内嵌了product.template对象。在默认的情况下,系统的各个视图并不区分这两个不同的对象。当我们创建一个Product时,系统实际同时 创建了一个product.product对象和一个对应的product.template对象,在这里product.product 和 product.template的关系是一对一的。
    但是,在很多情况下我们希望product.template和product.product是一对多的关系,也就是说,比如我可以用 product.template定义某个款式的服装,然后在product.product中定义多个不同颜色,尺寸的对应的服装产品。这时候,你就要 去“激活“product模块中已经定义好的一些视图(view), 或者自己来开发一些相应的视图,或更简单点使用现成的模块:product_variant_multi

    但是对已经使用了默认方法(product.template和product.product之间是一对一的关系)创建的这些product数据该如何处理呢?

    [b]实际情况:[/b]

    客户并没有认识到openerp可以为产品配置product template, 所以在生成产品主数据的时候使用了默认的方法,这样在现有的系统里有大量类似这样的产品名称:
    产品型号XXXXX - 红色, 产品型号XXXX - 黑色 ...., 事实上对客户的需求了解后,知道他们很多时候需要针对某个产品型号的汇总统计,而不论其颜色或其他属性的差别。所以我们有必要对现有的产品主数据改变。变 为,product.template的name字段为 ‘产品型号XXXX'而product.product的variants字段的值为:类似'红色‘, '黑色‘等。但是客户有几千个产品,手动修改是不现实的,我们创建了一个简单的脚本来实现了这个产品的转换,其中使用到了oersted库
    [b] 代码:
    [/b]

    <br /> import re<br /> from oersted import OEClient<br /><br /> oeclient = OEClient(&#039;localhost&#039;)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  #创建远程客户端对象<br /> oeclient.login(&#039;dbname&#039;, &#039;admin&#039;, &#039;admin&#039;)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #登录<br /> Product = oeclient.create_browse(&#039;dbname&#039;, &#039;product.product&#039;) #获取product对象<br /> Product_Tmpl = oeclient.create_browse(&#039;dbname&#039;, &#039;product.template&#039;) #获取product_template对象<br /> prod_objs = Product.search()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #获取所有product对象实例<br /> pattern = re.compile(r&#039;([^-]+?)\s*-\s*([^-]+)$&#039;)&nbsp; &nbsp; &nbsp;  #考虑到客户在定义产品名称时‘-’周围空格的不规范使用<br /> tmpl_prod = {}<br /> unmatched = &#91;]<br /> for prod in prod_objs:<br />&nbsp; &nbsp;  if not prod.variants:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  #防止多次运行该程序对已转换对象的修改<br />&nbsp; &nbsp; &nbsp; &nbsp;  matches = pattern.match(prod.name)<br />&nbsp; &nbsp; &nbsp; &nbsp;  if matches and matches.group(1):<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  tmpl_prod.setdefault(matches.group(1), &#91;]).append(prod) #获得例如: {&#039;产品型号XXX‘: [红色产品对象, 黑色产品对象, 白色产品对象}的字典<br />&nbsp; &nbsp; &nbsp; &nbsp;  else:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  unmatched.append(prod.name)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #记录未匹配的产品名称到umatched列表中,并在后面输出,以方便手工处理<br /> for tmpl in tmpl_prod:<br />&nbsp; &nbsp;  if len(tmpl_prod[tmpl]) &gt; 1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  #如果tmpl_prod字典的值,就是那个列表[红色产品对象, 黑色产品对象]有大于1项,表示该记录需要处理<br />&nbsp; &nbsp; &nbsp; &nbsp;  old_tmpl = tmpl_prod[tmpl][0].product_tmpl_id #记录列表中第一个产品所对应的产品模板<br />&nbsp; &nbsp; &nbsp; &nbsp;  new_tmpl_id = old_tmpl.copy(old_tmpl.id)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #拷贝获得新的模板对象,这里获得该新建模板的ID<br />&nbsp; &nbsp; &nbsp; &nbsp;  new_tmpl = Product_Tmpl(new_tmpl_id)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #获得新建模板对象<br />&nbsp; &nbsp; &nbsp; &nbsp;  new_tmpl.name = tmpl&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #改变新建模板的名称为tmpl_prod字典的key值<br />&nbsp; &nbsp; &nbsp; &nbsp;  new_tmpl.active = True&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #默认的product.template对象并没有active字段,我们做了继承修改,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #目的是安全考虑保留原template, <br />&nbsp; &nbsp; &nbsp; &nbsp;  new_tmpl.save()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  #保存修改后的template<br />&nbsp; &nbsp; &nbsp; &nbsp;  for prod in tmpl_prod[tmpl]:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  prod.product_tmpl_id.active = False&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  #可以直接删除原template,这里处于安全考虑,将其设置为:inactive<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  prod.product_tmpl_id.save()<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  m = pattern.match(prod.name)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  prod.product_tmpl_id = new_tmpl&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #给product赋予新的template<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  prod.variants = m.group(2)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #给product的variants字段赋值,如:红色,黑色<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  prod.save()<br /> print unmatched&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  #输出未匹配产品名以方便后续处理<br /> 
    

    【上海先安科技】(tony AT openerp.cn)

    1 条回复 最后回复
    0
    • N 离线
      N 离线
      NewZN
      写于 最后由 编辑
      #2

      校长好贴。
      请教下,oersted 调用,比xml-rpc 调用有哪些优点?从例子的OEClient 还看不出明显优势,呵呵。

      1 条回复 最后回复
      0
      • digitalsatoriD 离线
        digitalsatoriD 离线
        digitalsatori 管理员
        写于 最后由 编辑
        #3

        老肖的提问很到位。虽然本文提到了这些封装程序是为了简化的目的,但是如何简化,优点在哪并没有强调。

        我对这些封装库优点的理解是,他们在本地重建了类似server端的browse_object对象, 并事实上扩展了browse_object的能力,比如server 端的browse object是不能进行赋值操作的。而这些封装库是可以的。 这样的对对象的操作方法更加简单也更加pythonic。

        大家有兴趣的话可以写一个基于直接xml-rpc的脚本,看看本文中推荐的方法是不是简便些。

        【上海先安科技】(tony AT openerp.cn)

        1 条回复 最后回复
        0
        • mrshellyM 离线
          mrshellyM 离线
          mrshelly
          写于 最后由 编辑
          #4

          关注....

          有 Windows 的 python 扩展包下载吗?

          1 条回复 最后回复
          0
          • ieitzybI 离线
            ieitzybI 离线
            ieitzyb
            写于 最后由 编辑
            #5

            [quote author=digitalsatori link=topic=2495.msg8254#msg8254 date=1309231357]
            事实上对客户的需求了解后,知道他们很多时候需要针对某个产品型号的汇总统计,而不论其颜色或其他属性的差别。
            [/quote]
            服装!
            要修改产品主数据,总是一个改!
            我会选择修改前端,带来更好体验,同样保留季节、款号、尺码、颜色表示单一产品信息,前端用Excel数据透视表形式,分组汇总,即可按款号(忽略同款中的不同尺码、颜色)汇总,也可按款号+颜色汇总等,更贴近办公Office体验,效果更好。
            在7.0基本上也可以不改了吧,附图:
            [attachimg=1]

            http://www.OuduPLM.com/ 苏州欧度软件,专注服装行业(鳴謝:37signals,Trello,ProcessON,重庆慧积,上海开阖)

            1 条回复 最后回复
            0
            • digitalsatoriD 离线
              digitalsatoriD 离线
              digitalsatori 管理员
              写于 最后由 编辑
              #6

              想问一下Excel的数据透视图,和楼主的oersted的应用实例介绍有什么关系? 😮

              【上海先安科技】(tony AT openerp.cn)

              1 条回复 最后回复
              0
              • ieitzybI 离线
                ieitzybI 离线
                ieitzyb
                写于 最后由 编辑
                #7

                不要介意,我只是看到业务介绍部分想到的。

                http://www.OuduPLM.com/ 苏州欧度软件,专注服装行业(鳴謝:37signals,Trello,ProcessON,重庆慧积,上海开阖)

                1 条回复 最后回复
                0

                • 登录

                • 没有帐号? 注册

                • 登录或注册以进行搜索。
                • 第一个帖子
                  最后一个帖子
                0
                • 版块
                • 标签
                • 热门
                • 用户
                • 群组