add some property and cofiguration migration

This commit is contained in:
Àngel Àlvarez 2020-01-09 14:16:39 +01:00
parent 578e980d0e
commit 7bbe179d3d

View file

@ -8,6 +8,7 @@ CONFIG.update_etc(config_file)
from trytond.transaction import Transaction
from trytond.pool import Pool
from datetime import timedelta
import logging
Pool.start()
@ -29,7 +30,7 @@ with Transaction().start(dbname, 0, context=context):
user = user_obj.search([('login', '=', 'admin')], limit=1)[0]
user_id = user.id
def get_property_value(field_name, company_id):
def get_property_value(field_name, company_id, default=True):
query = """
select
p.value
@ -37,18 +38,21 @@ def get_property_value(field_name, company_id):
ir_model_field f
where p.field = f.id
and f.name = '%s'
and res is null
""" % (field_name)
if default:
query += ' and res is null'
if company_id:
query += " and company = %s" % company_id
else:
query += " and company is null"
cursor.execute(query)
results = cursor.fetchone()
result = results and results[0]
if not result:
return
return int(result.split(',')[1])
return result.split(',')[1]
# Change account_configuration
with Transaction().start(dbname, 0, context=context):
@ -89,7 +93,7 @@ with Transaction().start(dbname, 0, context=context):
# Given property's weak consistency, the account set as default
# may no longer exist
accounts = Account.search([('id', '=', value)], limit=1)
accounts = Account.search([('id', '=', int(value))], limit=1)
if not accounts:
continue
a, = accounts
@ -100,7 +104,7 @@ with Transaction().start(dbname, 0, context=context):
if 'payable' in field and not a.type.payable:
a.type.payable = True
a.type.save()
setattr(accountConfig, mapping[field], value)
setattr(accountConfig, mapping[field], int(value))
asset_sequence = Sequence.search([
('code','=', 'account.asset'), ('company', '=', company.id)])
@ -114,51 +118,82 @@ with Transaction().start(dbname, 0, context=context):
accountConfig.save()
Transaction().commit()
#with Transaction().start(dbname, 0, context=context):
# Company = pool.get('company.company')
# Model = pool.get('ir.model')
# Field = pool.get('ir.model.field')
#
# cursor = Transaction().connection.cursor()
#
# models = Model.search([
# ('model', 'like', '%_configuration'),
# ])
# for company in Company.search([]):
# with Transaction().set_context(company=company.id):
# save = {}
# for model in models:
# try:
# ToSave = pool.get(model.model)
# except:
# continue
# if model in save:
# to_save = save[model]
# else:
# toSave = ToSave(1)
# save[model] = toSave
# for field in Field.search([('model', '=', model)]):
# if field.name in ['id', 'create_uid', 'create_date', 'write_uid', 'write_date']:
# continue
# query = 'select * from ir_property where field = %s and company = %s;' % (field.id, company.id)
# cursor.execute(query)
# results = cursor.fetchone()
# if results:
# value = results[5]
# if field.ttype in ['many2many', 'one2many']:
# continue
# elif field.ttype == 'many2one' and value:
# model, _id = value.split(',')
# ValueModel = pool.get(model)
# valueModel = ValueModel(_id)
# setattr(toSave, field.name, valueModel)
# else:
# if value and value.startswith(','):
# setattr(toSave, field.name, value.split(',')[1])
# else:
# setattr(toSave, field.name, value)
# for model, toSave in save.items():
# toSave.save()
# Transaction().commit()
with Transaction().start(dbname, 0, context=context):
mapping = {
'party_lang': 'party_lang',
}
logger.info('Done')
PartyConfiguration = pool.get('party.configuration')
cursor = Transaction().connection.cursor()
for company in Company.search([]):
with Transaction().set_context(company=company.id):
partyConfig = PartyConfiguration(1)
for field in ('party_lang', ):
value = get_property_value(field, company.id, default=False)
if not value:
continue
setattr(partyConfig, mapping[field], int(value))
partyConfig.save()
Transaction().commit()
with Transaction().start(dbname, 0, context=context):
mapping = {
'sale_invoice_method': 'sale_invoice_method',
'sale_shipment_method': 'sale_shipment_method',
'sale_sequence': 'sale_sequence'
}
SaleConfiguration = pool.get('sale.configuration')
cursor = Transaction().connection.cursor()
for company in Company.search([]):
with Transaction().set_context(company=company.id):
saleConfig = SaleConfiguration(1)
for field in mapping.keys():
value = get_property_value(field, company.id, default=False)
if not value:
continue
if field == 'sale_sequence':
value = int(value)
print("Sale:", field, value)
setattr(saleConfig, mapping[field], value)
saleConfig.save()
Transaction().commit()
with Transaction().start(dbname, 0, context=context):
StockConfiguration = pool.get('stock.configuration')
cursor = Transaction().connection.cursor()
for company in Company.search([]):
with Transaction().set_context(company=company.id):
stockConfig = StockConfiguration(1)
stockConfig.valued_origin = True
stockConfig.save()
Transaction().commit()
with Transaction().start(dbname, 0, context=context):
mapping = {
'purchase_invoice_method': 'purchase_invoice_method',
'supply_period': 'supply_period',
'purchase_sequence': 'purchase_sequence'
}
PurchaseConfiguration = pool.get('purchase.configuration')
cursor = Transaction().connection.cursor()
for company in Company.search([]):
with Transaction().set_context(company=company.id):
purchaseConfig = PurchaseConfiguration(1)
for field in mapping.keys():
value = get_property_value(field, company.id, default=False)
if not value:
continue
if field == 'supply_period':
value = timedelta(days=eval(value))
if field == 'purchase_sequence':
value = int(value)
print("Purchae:", field, value)
setattr(purchaseConfig, mapping[field], value)
purchaseConfig.save()
Transaction().commit()
logger.info('Done')