1 #----------------------------------------------------------
3 #----------------------------------------------------------
5 import StringIO,xml.dom.minidom
13 from config import config
16 # Number of imported lines between two commit (see convert_csv_import()):
20 class ConvertError(Exception):
21 def __init__(self, doc, orig_excpt):
23 self.orig = orig_excpt
26 return 'Exception:\n\t%s\nUsing file:\n%s' % (self.orig, self.d)
28 def _eval_xml(self,node, pool, cr, uid, idref):
29 if node.nodeType == node.TEXT_NODE:
30 return node.data.encode("utf8")
31 elif node.nodeType == node.ELEMENT_NODE:
32 if node.nodeName in ('field','value'):
33 t = node.getAttribute('type') or 'char'
34 if len(node.getAttribute('search')):
35 f_search = node.getAttribute("search").encode('utf-8')
36 f_model = node.getAttribute("model").encode('ascii')
37 f_use = node.getAttribute("use").encode('ascii')
38 f_name = node.getAttribute("name").encode('utf-8')
41 q = eval(f_search, idref)
42 ids = pool.get(f_model).search(cr, uid, q)
44 ids = map(lambda x: x[f_use], pool.get(f_model).read(cr, uid, ids, [f_use]))
45 _cols = pool.get(f_model)._columns
46 if (f_name in _cols) and _cols[f_name]._type=='many2many':
51 if isinstance(f_val, tuple):
54 a_eval = node.getAttribute('eval')
59 idref['version'] = release.version.rsplit('.', 1)[0]
60 idref['ref'] = lambda x: self.id_get(cr, False, x)
64 logger = netsvc.Logger()
65 logger.notifyChannel("init", netsvc.LOG_INFO, 'could not find pytz library')
66 class pytzclass(object):
70 return eval(a_eval, idref)
72 def _process(s, idref):
73 m = re.findall('[^%]%\((.*?)\)[ds]', s)
76 idref[id]=self.id_get(cr, False, id)
78 txt = '<?xml version="1.0"?>\n'+_process("".join([i.toxml().encode("utf8") for i in node.childNodes]), idref)
79 # txt = '<?xml version="1.0"?>\n'+"".join([i.toxml().encode("utf8") for i in node.childNodes]) % idref
82 if t in ('char', 'int', 'float'):
84 for n in [i for i in node.childNodes]:
85 d+=str(_eval_xml(self,n,pool,cr,uid,idref))
95 elif t in ('list','tuple'):
97 for n in [i for i in node.childNodes if (i.nodeType == i.ELEMENT_NODE and i.nodeName=='value')]:
98 res.append(_eval_xml(self,n,pool,cr,uid,idref))
102 elif node.nodeName=="getitem":
103 for n in [i for i in node.childNodes if (i.nodeType == i.ELEMENT_NODE)]:
104 res=_eval_xml(self,n,pool,cr,uid,idref)
107 elif node.getAttribute('type') in ("int", "list"):
108 return res[int(node.getAttribute('index'))]
110 return res[node.getAttribute('index').encode("utf8")]
111 elif node.nodeName=="function":
113 a_eval = node.getAttribute('eval')
115 idref['ref'] = lambda x: self.id_get(cr, False, x)
116 args = eval(a_eval, idref)
117 for n in [i for i in node.childNodes if (i.nodeType == i.ELEMENT_NODE)]:
118 args.append(_eval_xml(self,n, pool, cr, uid, idref))
119 model = pool.get(node.getAttribute('model'))
120 method = node.getAttribute('name')
121 res = getattr(model, method)(cr, uid, *args)
124 escape_re = re.compile(r'(?<!\\)/')
126 return x.replace('\\/', '/')
128 class xml_import(object):
130 def _test_xml_id(self, xml_id):
133 base, id = xml_id.split('.')
135 self.logger.notifyChannel('init', netsvc.LOG_ERROR, 'id: %s is to long (max: 64)'%xml_id)
136 def _tag_delete(self, cr, rec, data_node=None):
137 d_model = rec.getAttribute("model")
138 d_search = rec.getAttribute("search")
139 ids = self.pool.get(d_model).search(cr,self.uid,eval(d_search))
141 self.pool.get(d_model).unlink(cr, self.uid, ids)
142 #self.pool.get('ir.model.data')._unlink(cr, self.uid, d_model, ids, direct=True)
145 def _tag_report(self, cr, rec, data_node=None):
147 for dest,f in (('name','string'),('model','model'),('report_name','name')):
148 res[dest] = rec.getAttribute(f).encode('utf8')
149 assert res[dest], "Attribute %s of report is empty !" % (f,)
150 for field,dest in (('rml','report_rml'),('xml','report_xml'),('xsl','report_xsl')):
151 if rec.hasAttribute(field):
152 res[dest] = rec.getAttribute(field).encode('utf8')
153 if rec.hasAttribute('auto'):
154 res['auto'] = eval(rec.getAttribute('auto'))
155 if rec.hasAttribute('sxw'):
156 sxw_content = misc.file_open(rec.getAttribute('sxw')).read()
157 res['report_sxw_content'] = sxw_content
158 if rec.hasAttribute('header'):
159 res['header'] = eval(rec.getAttribute('header'))
160 res['multi'] = rec.hasAttribute('multi') and eval(rec.getAttribute('multi'))
161 xml_id = rec.getAttribute('id').encode('utf8')
162 self._test_xml_id(xml_id)
163 id = self.pool.get('ir.model.data')._update(cr, self.uid, "ir.actions.report.xml", self.module, res, xml_id, mode=self.mode)
164 self.idref[xml_id] = id
165 if not rec.hasAttribute('menu') or eval(rec.getAttribute('menu')):
166 keyword = str(rec.getAttribute('keyword') or 'client_print_multi')
167 keys = [('action',keyword),('res_model',res['model'])]
168 value = 'ir.actions.report.xml,'+str(id)
169 replace = rec.hasAttribute('replace') and rec.getAttribute("replace")
170 self.pool.get('ir.model.data').ir_set(cr, self.uid, 'action', keyword, res['name'], [res['model']], value, replace=replace, isobject=True, xml_id=xml_id)
173 def _tag_function(self, cr, rec, data_node=None):
174 _eval_xml(self,rec, self.pool, cr, self.uid, self.idref)
177 def _tag_wizard(self, cr, rec, data_node=None):
178 string = rec.getAttribute("string").encode('utf8')
179 model = rec.getAttribute("model").encode('utf8')
180 name = rec.getAttribute("name").encode('utf8')
181 xml_id = rec.getAttribute('id').encode('utf8')
182 self._test_xml_id(xml_id)
183 multi = rec.hasAttribute('multi') and eval(rec.getAttribute('multi'))
184 res = {'name': string, 'wiz_name': name, 'multi':multi}
186 id = self.pool.get('ir.model.data')._update(cr, self.uid, "ir.actions.wizard", self.module, res, xml_id, mode=self.mode)
187 self.idref[xml_id] = id
189 if not rec.hasAttribute('menu') or eval(rec.getAttribute('menu')):
190 keyword = str(rec.getAttribute('keyword') or 'client_action_multi')
191 keys = [('action',keyword),('res_model',model)]
192 value = 'ir.actions.wizard,'+str(id)
193 replace = rec.hasAttribute('replace') and rec.getAttribute("replace")
194 self.pool.get('ir.model.data').ir_set(cr, self.uid, 'action', keyword, string, [model], value, replace=replace, isobject=True, xml_id=xml_id)
197 def _tag_act_window(self, cr, rec, data_node=None):
198 name = rec.hasAttribute('name') and rec.getAttribute('name').encode('utf-8')
199 xml_id = rec.getAttribute('id').encode('utf8')
200 self._test_xml_id(xml_id)
201 type = rec.hasAttribute('type') and rec.getAttribute('type').encode('utf-8') or 'ir.actions.act_window'
203 if rec.hasAttribute('view'):
204 view_id = self.id_get(cr, 'ir.actions.act_window', rec.getAttribute('view').encode('utf-8'))
205 domain = rec.hasAttribute('domain') and rec.getAttribute('domain').encode('utf-8')
206 context = rec.hasAttribute('context') and rec.getAttribute('context').encode('utf-8') or '{}'
207 res_model = rec.getAttribute('res_model').encode('utf-8')
208 src_model = rec.hasAttribute('src_model') and rec.getAttribute('src_model').encode('utf-8')
209 view_type = rec.hasAttribute('view_type') and rec.getAttribute('view_type').encode('utf-8') or 'form'
210 view_mode = rec.hasAttribute('view_mode') and rec.getAttribute('view_mode').encode('utf-8') or 'tree,form'
211 usage = rec.hasAttribute('usage') and rec.getAttribute('usage').encode('utf-8')
213 res = {'name': name, 'type': type, 'view_id': view_id, 'domain': domain, 'context': context, 'res_model': res_model, 'src_model': src_model, 'view_type': view_type, 'view_mode': view_mode, 'usage': usage }
215 id = self.pool.get('ir.model.data')._update(cr, self.uid, 'ir.actions.act_window', self.module, res, xml_id, mode=self.mode)
216 self.idref[xml_id] = id
219 keyword = 'client_action_relate'
220 keys = [('action', keyword), ('res_model', res_model)]
221 value = 'ir.actions.act_window,'+str(id)
222 replace = rec.hasAttribute('replace') and rec.getAttribute('replace')
223 self.pool.get('ir.model.data').ir_set(cr, self.uid, 'action', keyword, xml_id, [src_model], value, replace=replace, isobject=True, xml_id=xml_id)
226 def _tag_ir_set(self, cr, rec, data_node=None):
227 if not self.mode=='init':
230 for field in [i for i in rec.childNodes if (i.nodeType == i.ELEMENT_NODE and i.nodeName=="field")]:
231 f_name = field.getAttribute("name").encode('utf-8')
232 f_val = _eval_xml(self,field,self.pool, cr, self.uid, self.idref)
234 self.pool.get('ir.model.data').ir_set(cr, self.uid, res['key'], res['key2'], res['name'], res['models'], res['value'], replace=res.get('replace',True), isobject=res.get('isobject', False), meta=res.get('meta',None))
237 def _tag_workflow(self, cr, rec, data_node=None):
238 model = str(rec.getAttribute('model'))
239 wf_service = netsvc.LocalService("workflow")
240 wf_service.trg_validate(self.uid, model,
241 self.id_get(cr, model, rec.getAttribute('ref')),
242 str(rec.getAttribute('action')), cr)
245 def _tag_menuitem(self, cr, rec, data_node=None):
246 rec_id = rec.getAttribute("id").encode('ascii')
247 self._test_xml_id(rec_id)
248 m_l = map(escape, escape_re.split(rec.getAttribute("name").encode('utf8')))
250 for idx, menu_elem in enumerate(m_l):
252 cr.execute('select id from ir_ui_menu where parent_id=%d and name=%s', (pid, menu_elem))
254 cr.execute('select id from ir_ui_menu where parent_id is null and name=%s', (menu_elem,))
257 # we are at the last menu element/level (it's a leaf)
258 values = {'parent_id': pid,'name':menu_elem}
260 if rec.hasAttribute('action'):
261 a_action = rec.getAttribute('action').encode('utf8')
262 a_type = rec.getAttribute('type').encode('utf8') or 'act_window'
264 "act_window": 'STOCK_NEW',
265 "report.xml": 'STOCK_PASTE',
266 "wizard": 'STOCK_EXECUTE',
268 values['icon'] = icons.get(a_type,'STOCK_NEW')
269 if a_type=='act_window':
270 a_id = self.id_get(cr, 'ir.actions.%s'% a_type, a_action)
271 cr.execute('select view_type,view_mode,name,view_id from ir_act_window where id=%d', (int(a_id),))
272 action_type,action_mode,action_name,view_id = cr.fetchone()
274 cr.execute('SELECT type FROM ir_ui_view WHERE id=%d', (int(view_id),))
275 action_mode, = cr.fetchone()
276 cr.execute('SELECT view_mode FROM ir_act_window_view WHERE act_window_id=%d ORDER BY sequence LIMIT 1', (int(a_id),))
278 action_mode, = cr.fetchone()
279 if action_type=='tree':
280 values['icon'] = 'STOCK_INDENT'
281 elif action_mode and action_mode.startswith('tree'):
282 values['icon'] = 'STOCK_JUSTIFY_FILL'
283 elif action_mode and action_mode.startswith('graph'):
284 values['icon'] = 'terp-account'
285 if not values['name']:
286 values['name'] = action_name
287 if rec.hasAttribute('sequence'):
288 values['sequence'] = int(rec.getAttribute('sequence'))
289 if rec.hasAttribute('icon'):
290 values['icon'] = str(rec.getAttribute('icon'))
291 if rec.hasAttribute('groups'):
292 g_names = rec.getAttribute('groups').split(',')
294 for group in g_names:
295 g_ids.extend(self.pool.get('res.groups').search(cr, self.uid, [('name', '=', group)]))
296 values['groups_id'] = [(6, 0, g_ids)]
297 xml_id = rec.getAttribute('id').encode('utf8')
298 self._test_xml_id(xml_id)
299 pid = self.pool.get('ir.model.data')._update(cr, self.uid, 'ir.ui.menu', self.module, values, xml_id, idx==len(m_l)-1, mode=self.mode, res_id=res and res[0] or False)
301 # the menuitem already exists
303 xml_id = idx==len(m_l)-1 and rec.getAttribute('id').encode('utf8')
305 npid = self.pool.get('ir.model.data')._update_dummy(cr, self.uid, 'ir.ui.menu', self.module, xml_id, idx==len(m_l)-1)
307 print 'Menu Error', self.module, xml_id, idx==len(m_l)-1
309 # the menuitem does't exist but we are in branch (not a leaf)
310 pid = self.pool.get('ir.ui.menu').create(cr, self.uid, {'parent_id' : pid, 'name' : menu_elem})
312 self.idref[rec_id] = pid
314 if rec.hasAttribute('action') and pid:
315 a_action = rec.getAttribute('action').encode('utf8')
316 a_type = rec.getAttribute('type').encode('utf8') or 'act_window'
317 a_id = self.id_get(cr, 'ir.actions.%s' % a_type, a_action)
318 action = "ir.actions.%s,%d" % (a_type, a_id)
319 self.pool.get('ir.model.data').ir_set(cr, self.uid, 'action', 'tree_but_open', 'Menuitem', [('ir.ui.menu', int(pid))], action, True, True, xml_id=rec_id)
320 return ('ir.ui.menu', pid)
322 def _tag_assert(self, cr, rec, data_node=None):
323 rec_model = rec.getAttribute("model").encode('ascii')
324 model = self.pool.get(rec_model)
325 assert model, "The model %s does not exist !" % (rec_model,)
326 rec_id = rec.getAttribute("id").encode('ascii')
327 self._test_xml_id(rec_id)
328 id = self.id_get(cr, False, rec_id)
329 if data_node.getAttribute('noupdate') and not self.mode == 'init':
330 for test in [i for i in rec.childNodes if (i.nodeType == i.ELEMENT_NODE and i.nodeName=="test")]:
331 f_expr = test.getAttribute("expr").encode('utf-8')
333 def __getitem__(self2, key):
334 return getattr(model.browse(cr, self.uid, id), key)
336 def _tag_record(self, cr, rec, data_node=None):
337 rec_model = rec.getAttribute("model").encode('ascii')
338 model = self.pool.get(rec_model)
339 assert model, "The model %s does not exist !" % (rec_model,)
340 rec_id = rec.getAttribute("id").encode('ascii')
341 self._test_xml_id(rec_id)
343 # if not rec_id and not data_node.getAttribute('noupdate'):
344 # print "Warning", rec_model
346 if data_node.getAttribute('noupdate') and not self.mode == 'init':
347 # check if the xml record has an id string
349 id = self.pool.get('ir.model.data')._update_dummy(cr, self.uid, rec_model, self.module, rec_id)
350 # check if the resource already existed at the last update
352 # if it existed, we don't update the data, but we need to
353 # know the id of the existing record anyway
354 self.idref[rec_id] = id
357 # if the resource didn't exist
358 if rec.getAttribute("forcecreate"):
359 # we want to create it, so we let the normal "update" behavior happen
362 # otherwise do nothing
365 # otherwise it is skipped
369 for field in [i for i in rec.childNodes if (i.nodeType == i.ELEMENT_NODE and i.nodeName=="field")]:
370 #TODO: most of this code is duplicated above (in _eval_xml)...
371 f_name = field.getAttribute("name").encode('utf-8')
372 f_ref = field.getAttribute("ref").encode('ascii')
373 f_search = field.getAttribute("search").encode('utf-8')
374 f_model = field.getAttribute("model").encode('ascii')
375 if not f_model and model._columns.get(f_name,False):
376 f_model = model._columns[f_name]._obj
377 f_use = field.getAttribute("use").encode('ascii') or 'id'
381 q = eval(f_search, self.idref)
383 assert f_model, 'Define an attribute model="..." in your .XML file !'
384 f_obj = self.pool.get(f_model)
385 # browse the objects searched
386 s = f_obj.browse(cr, self.uid, f_obj.search(cr, self.uid, q))
387 # column definitions of the "local" object
388 _cols = self.pool.get(rec_model)._columns
389 # if the current field is many2many
390 if (f_name in _cols) and _cols[f_name]._type=='many2many':
391 f_val = [(6, 0, map(lambda x: x[f_use], s))]
393 # otherwise (we are probably in a many2one field),
394 # take the first element of the search
400 f_val = self.id_get(cr, f_model, f_ref)
402 f_val = _eval_xml(self,field, self.pool, cr, self.uid, self.idref)
403 if model._columns.has_key(f_name):
404 if isinstance(model._columns[f_name], osv.fields.integer):
407 id = self.pool.get('ir.model.data')._update(cr, self.uid, rec_model, self.module, res, rec_id or False, not data_node.getAttribute('noupdate'), noupdate=data_node.getAttribute('noupdate'), mode=self.mode )
409 self.idref[rec_id] = id
412 def id_get(self, cr, model, id_str):
413 if id_str in self.idref:
414 return self.idref[id_str]
417 mod,id_str = id_str.split('.')
418 result = self.pool.get('ir.model.data')._get_id(cr, self.uid, mod, id_str)
419 return self.pool.get('ir.model.data').read(cr, self.uid, [result], ['res_id'])[0]['res_id']
421 def parse(self, xmlstr):
422 d = xml.dom.minidom.parseString(xmlstr)
423 de = d.documentElement
424 for n in [i for i in de.childNodes if (i.nodeType == i.ELEMENT_NODE and i.nodeName=="data")]:
425 for rec in n.childNodes:
426 if rec.nodeType == rec.ELEMENT_NODE:
427 if rec.nodeName in self._tags:
429 self._tags[rec.nodeName](self.cr, rec, n)
431 self.logger.notifyChannel("init", netsvc.LOG_INFO, '\n'+rec.toxml())
437 def __init__(self, cr, module, idref, mode):
438 self.logger = netsvc.Logger()
443 self.pool = pooler.get_pool(cr.dbname)
444 # self.pool = osv.osv.FakePool(module)
447 'menuitem': self._tag_menuitem,
448 'record': self._tag_record,
449 'assert': self._tag_assert,
450 'report': self._tag_report,
451 'wizard': self._tag_wizard,
452 'delete': self._tag_delete,
453 'ir_set': self._tag_ir_set,
454 'function': self._tag_function,
455 'workflow': self._tag_workflow,
456 'act_window': self._tag_act_window,
465 def convert_csv_import(cr, module, fname, csvcontent, idref=None, mode='init', noupdate=False):
468 model = ('.'.join(fname.split('.')[:-1]).split('-'))[0]
469 #remove folder path from model
470 head, model = os.path.split(model)
472 pool = pooler.get_pool(cr.dbname)
474 input=StringIO.StringIO(csvcontent)
475 reader = csv.reader(input, quotechar='"', delimiter=',')
476 fields = reader.next()
478 if not (mode == 'init' or 'id' in fields):
484 datas.append( map(lambda x:x.decode('utf8').encode('utf8'), line))
485 if len(datas) > COMMIT_STEP:
486 pool.get(model).import_data(cr, uid, fields, datas,mode, module,noupdate)
491 pool.get(model).import_data(cr, uid, fields, datas,mode, module,noupdate)
497 def convert_xml_import(cr, module, xmlstr, idref=None, mode='init'):
500 obj = xml_import(cr, module, idref, mode)
505 def convert_xml_export(res):
507 pool=pooler.get_pool(cr.dbname)
508 cr=pooler.db.cursor()
510 d = xml.dom.minidom.getDOMImplementation().createDocument(None, "terp", None)
511 de = d.documentElement
512 data=d.createElement("data")
514 de.appendChild(d.createTextNode('Some textual content.'))