Mar 7, 2013

OpenERP 7, hack fields.property insert property_account_income property_account_expense to product_template

OpenERP 7, hacking fields.property to insert property_account_income  and  property_account_expense to product_template.

just a little concept you should know is about "fields.property", it is a function, so you can not inserting as commonly you do with xml-rpc, because you can not casting an object into it.

its very different with many2many connection, such as taxes_id or supplier_taxes_id, to set your product has it, just try setup one product and you can use a simple sql as below :


select * product_taxes_rel (product_id, tax_id)

the output 

#1;1
#204;1

so you just need to run :

insert into product_taxes_rel(prod_id, tax_id) select id, '1' from product_template where id not in (1,204)

then 
select * from product_supplier_taxes_rel

the output 
#1;2
#204;2

you just need run : 

insert into product_supplier_taxes_rel(prod_id, tax_id) select id, '2' from product_template where id not in (1,204)


well, lets back to inserting property, to inserting those properties, you need try setup one product, then look out on your database, it seem as below :


select * from ir_property where name in ('property_account_income', 'property_account_expense')

the output 

27;1;"2013-02-05 14:27:05.3211";"2013-02-05 14:27:05.3211";1;"";;"property_account_expense";;"many2one";1;1940;"";"";"account.account,307";"product.template,1"
28;1;"2013-02-05 14:27:05.3211";"2013-02-05 14:27:05.3211";1;"";;"property_account_income";;"many2one";1;1938;"";"";"account.account,308";"product.template,1"



there are two ways to inserting
A. Direct SQL. 

  • you need to create simple hack_property.py
  • run it and cast the output to sql-file, just like python hack_property.py > sql-file.sql
  • run the sql


here is the example of hack_propery.py


import xmlrpclib




username = "username"

pwd = "password"
dbname = "yourdb"

sock_common = xmlrpclib.ServerProxy("http://localhost:8069/xmlrpc/common")
uid = sock_common.login(dbname, username, pwd)
sock = xmlrpclib.ServerProxy("http://localhost:8069/xmlrpc/object")

results = sock.execute(dbname, uid, pwd, 'product.template', 'search', [])
for i in results:
    sql = """ insert into ir_property (create_uid, create_date, write_date, write_uid, name, type, company_id, fields_id, value_reference, res_id)    select '1', LOCALTIMESTAMP, LOCALTIMESTAMP,'1', 'property_account_expense', 'many2one', '1', '1940', 'account.account,307', 'product.template,"""
    sql = sql + str(i) + "';"
    print sql
    sql = """ insert into ir_property (create_uid, create_date, write_date, write_uid, name, type, company_id, fields_id, value_reference, res_id)    select '1', LOCALTIMESTAMP, LOCALTIMESTAMP,'1', 'property_account_income', 'many2one', '1', '1938', 'account.account,308', 'product.template,"""
    sql = sql + str(i) + "';"
    print sql



your sql-file should be like this : 


 insert into ir_property (create_uid, create_date, write_date, write_uid, name, type, company_id, fields_id, value_reference, res_id)    select '1', LOCALTIMESTAMP, LOCALTIMESTAMP,'1', 'property_account_expense', 'many2one', '1', '1940', 'account.account,307', 'product.template,1';
 insert into ir_property (create_uid, create_date, write_date, write_uid, name, type, company_id, fields_id, value_reference, res_id)    select '1', LOCALTIMESTAMP, LOCALTIMESTAMP,'1', 'property_account_income', 'many2one', '1', '1938', 'account.account,308', 'product.template,1';
 insert into ir_property (create_uid, create_date, write_date, write_uid, name, type, company_id, fields_id, value_reference, res_id)    select '1', LOCALTIMESTAMP, LOCALTIMESTAMP,'1', 'property_account_expense', 'many2one', '1', '1940', 'account.account,307', 'product.template,2';
 insert into ir_property (create_uid, create_date, write_date, write_uid, name, type, company_id, fields_id, value_reference, res_id)    select '1', LOCALTIMESTAMP, LOCALTIMESTAMP,'1', 'property_account_income', 'many2one', '1', '1938', 'account.account,308', 'product.template,2';



B. Pure xml-rpc 

in this step, you need to look the structure of fields.property, you need to copy this creation style


        prop = obj.pool.get('ir.property')
            return prop.create(cr, uid, {
                'name': propdef.name,
                'value': id_val,
                'res_id': obj._name+','+str(id),
                'company_id': cid,
                'fields_id': def_id,
                'type': self._type,
            }, context=context)

thats all, its already update thousands data of mine...  :D

after running those script, you will see that all of product are proper now..



Read more ...