หน้า 1 จากทั้งหมด 1

โค้ดสำหรับ ย้ายไฟล์ที่เก็บไว้ใน ฐานข้อมูลมาไว้ในระบบไฟล์

โพสต์แล้ว: อังคาร 24 ก.ย. 2013 2:58 pm
โดย openerpthailand
สืบเนื่องจากกระทู้
http://www.openerpthailand.org/viewtopi ... =34&t=1381

ไฟล์เก่าๆ ทีเคยอัพโหลดขึ้นไป จะไม่ได้อยู่ ในระบบ ไฟล์ เราสามารถ ย้ายได้ด้วย โค้ดด้านล่าง

โค้ด: เลือกทั้งหมด

#!/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 ..."