KERNEL : Data loading : id in csv files are now handled like id on xml record.

bzr revid: bch-7e208314be3c1b2c9a88e766964578e338ecf47b
This commit is contained in:
bch
2007-07-05 07:37:05 +00:00
parent 414f359775
commit 496221cf3d
3 changed files with 20 additions and 20 deletions
+1 -1
View File
@@ -213,7 +213,7 @@ def load_module_graph(cr, graph, status={}):
name, ext = os.path.splitext(xml)
logger.notifyChannel('init', netsvc.LOG_INFO, 'addon:%s:loading %s' % (m, xml))
if ext == '.csv':
tools.convert_csv_import(cr, m, os.path.basename(xml), tools.file_open(opj(m, xml)).read(), idref)
tools.convert_csv_import(cr, m, os.path.basename(xml), tools.file_open(opj(m, xml)).read(), idref, noupdate=True)
else:
tools.convert_xml_import(cr, m, tools.file_open(opj(m, xml)).read(), idref)
cr.execute('update ir_module_module set demo=%s where name=%s', (True, package.name))
+12 -10
View File
@@ -571,15 +571,16 @@ class orm(object):
datas += self.__export_row(cr, uid, row, fields, [], context)
return datas
def import_data(self, cr, uid, fields, datas, context={}):
def import_data(self, cr, uid, fields, datas, mode='init', current_module=None, noupdate=False, context={}):
fields = map(lambda x: x.split('/'), fields)
logger = netsvc.Logger()
def process_liness(self, datas, prefix, fields_def, position=0):
line = datas[position]
row = {}
translate = {}
todo = []
warning = ''
data_id= False
#
# Import normal fields
#
@@ -587,6 +588,9 @@ class orm(object):
if i>=len(line):
raise 'Please check that all your lines have %d cols.' % (len(fields),)
field = fields[i]
if field == ["id"]:
data_id= line[i]
continue
if (len(field)==len(prefix)+1) and field[len(prefix)].endswith(':id'):
res_id = False
if line[i]:
@@ -623,7 +627,6 @@ class orm(object):
if key==line[i]: #val==line[i] if from the client !
res = key
if line[i] and not res:
logger = netsvc.Logger()
logger.notifyChannel("import", netsvc.LOG_WARNING, "key '%s' not found in selection field '%s'" %(line[i], field[len(prefix)]))
elif fields_def[field[len(prefix)]]['type']=='many2one':
res = False
@@ -634,7 +637,7 @@ class orm(object):
if not res and line[i]:
warning += ('Relation not found: '+line[i]+' on '+relation + ' !\n')
if not res:
print 'Relation not found: '+line[i]+' on '+relation + ' !\n'
logger.notifyChannel("import",netsvc.LOG_WARNING,'Relation not found: '+line[i]+' on '+relation + ' !\n')
elif fields_def[field[len(prefix)]]['type']=='many2many':
res = []
if line[i]:
@@ -652,8 +655,6 @@ class orm(object):
elif (prefix==field[0:len(prefix)]):
if field[0] not in todo:
todo.append(field[len(prefix)])
#
# Import one2many fields
#
@@ -686,21 +687,22 @@ class orm(object):
for i in range(max(nbrmax,1)):
#if datas:
datas.pop(0)
result = [row, nbrmax, warning, translate]
result = (row, nbrmax, warning, translate, data_id)
return result
fields_def = self.fields_get(cr, uid, context=context)
done = 0
while len(datas):
(res,other,warning,translate) = process_liness(self, datas, [], fields_def)
(res,other,warning,translate,data_id) = process_liness(self, datas, [], fields_def)
try:
id=self.create(cr, uid, res, context)
id= self.pool.get('ir.model.data')._update(cr,uid,self._name, current_module, res, xml_id=data_id, mode=mode, noupdate=noupdate)
for lang in translate:
context2=context.copy()
context2['lang']=lang
self.write(cr, uid, [id], translate[lang], context2)
except Exception, e:
print e
logger.notifyChannel("import",netsvc.LOG_ERROR, e)
cr.rollback()
return (-1, res, e[0], warning)
done += 1
+7 -9
View File
@@ -445,33 +445,31 @@ class xml_import(object):
# delimiter: ,
# encoding: UTF8
#
def convert_csv_import(cr, module, fname, csvcontent, idref={}, mode='init'):
if mode != 'init':
return
def convert_csv_import(cr, module, fname, csvcontent, idref={}, mode='init', noupdate=False):
model = ('.'.join(fname.split('.')[:-1]).split('-'))[0]
#model = fname.split('.')[0].replace('_', '.')
#remove folder path from model
head, model = os.path.split(model)
pool = pooler.get_pool(cr.dbname)
# pool = osv.osv.FakePool(module)
input=StringIO.StringIO(csvcontent)
reader = csv.reader(input, quotechar='"', delimiter=',')
fields = reader.next()
if not (mode == 'init' or 'id' in fields):
return
uid = 1
datas = []
for line in reader:
datas.append( map(lambda x:x.decode('utf8').encode('utf8'), line))
if len(datas) > COMMIT_STEP:
pool.get(model).import_data(cr, uid, fields, datas)
pool.get(model).import_data(cr, uid, fields, datas,mode, module,noupdate)
cr.commit()
datas=[]
if datas:
pool.get(model).import_data(cr, uid, fields, datas)
pool.get(model).import_data(cr, uid, fields, datas,mode, module,noupdate)
cr.commit()
#