zoukankan      html  css  js  c++  java
  • OpenERP在哪储存附件?

    我们知道对OpenERP中的每个内部对象(比如:业务伙伴,采购订单,销售订单,发货单,等等)我们都可以添加任意的附件,如图片,文档,视频等。那么这些附件在OpenERP内部是如何管理的呢?

    默认情况下,这些附件在OpenERP v7中是保存在数据库中的。我们知道当附件的数量比较大时,这会严重影响数据库的性能。其实在OpenERP 中我们可以通过设置ir.config.parameter参数来使附件保存在文件系统中,具体菜单位置是:”设置-技术-参数-系统参数-ir_attachement.location” (Settings->Technical->Parameters-System parameters- ir_attachment.location)

    比如我们将ir_attachment.location设置为file:///filestore

    那么这些附件就会保存在openerp根目录/filestore下, 系统使用sha1哈希算法来创建文件名所以重复的文件在系统中并不会多占空间。

    目前只支持file:///协议,实际上我们可以很容易通过扩增模块来支持比如amazons3:///协议,这样我们就可以将附件保存在亚马逊的S3云服务了。

    数据库保存附件的模式下,数据是保存在ir_attachment.db_datas
    文件系统保存附件的模式下,文件名保存在ir_attachment.db_datas_fname

    我们尚为提供这两种模式的自动转换机制。所以,如果你设置了这个参数,那么已存在的附件仍将保存在数据库中,只有新附件会保存在文件系统中,系统会尝试访问这两个不同的位置,所以也没什么问题(先检查db_datas,然后再检查db_datas_fname)

    注:本文末尾提供的脚本可以自动将现有数据库中的附件转换到文件系统中

    如果你移除了这个参数,你需要设法将在文件系统中保存的附件存回到数据库中,因为系统就只会通过数据库来检查附件了。

    将现有数据库中的附件数据转移到文件系统中的脚本(替换URL为您的OpenERP实际访问URL地址):

    #!/usr/bin/python
     
    import xmlrpclib
     
    username = 'admin' #the user
    pwd = 'password'      #the password of the user
    dbname = 'database'    #the database
     
    # Get the uid
    sock_common = xmlrpclib.ServerProxy ('<URL>/xmlrpc/common')
    uid = sock_common.login(dbname, username, pwd)
    sock = xmlrpclib.ServerProxy('<URL>/xmlrpc/object')
     
    def migrate_attachment(att_id):
        # 1. get data
        att = sock.execute(dbname, uid, pwd, 'ir.attachment', 'read', att_id, ['datas'])
     
        data = att['datas']
     
        # Re-Write attachment
        a = sock.execute(dbname, uid, pwd, 'ir.attachment', 'write', [att_id], {'datas': data})
     
    # SELECT attachments:
    att_ids = sock.execute(dbname, uid, pwd, 'ir.attachment', 'search', [('store_fname','=',False)])
     
    cnt = len(att_ids)
    i = 0
    for id in att_ids:
        att = sock.execute(dbname, uid, pwd, 'ir.attachment', 'read', id, ['datas','parent_id'])
     
        migrate_attachment(id)
        print 'Migrated ID %d (attachment %d of %d)' % (id,i,cnt)
        i = i + 1
     
    print "done ..."

    运行这个脚本后,我们还需要清除ir_attachements表:

    update ir_attachment set db_datas = null where store_fname is not null
    vacuum (full, analyze) ir_attachment
  • 相关阅读:
    4年Java程序员十面阿里终拿下offer,评级P6+年薪30-40w无股票
    真香警告!手绘172张图解HTTP协议+703页TCP/IP协议笔记
    Git官方和创始人都推荐的Git权威指南,广度深度和实战性史无前例
    阿里“教授”总结整理手写大型网站技术架构:核心原理与案例分析
    GitHub上120K Stars国内第一的Java多线程PDF到底有什么魅力?
    霸榜GitHub必读书籍:编写高质量代码改善Java程序员的151个建议
    GitHub上260K Stars的P8架构师纯手写的Java高并发编程详解
    LeetCode每日一题:802 找到最终安全状态
    LeetCode每日一题:662二叉树最大宽度
    Springboot之security框架 登录安全验证授权流程
  • 原文地址:https://www.cnblogs.com/jacker1979/p/3719127.html
Copyright © 2011-2022 走看看