1 #----------------------------------------------------------
3 #----------------------------------------------------------
5 import StringIO,xml.dom.minidom
11 from config import config
14 # Number of imported lines between two commit (see convert_csv_import()):
18 class ConvertError(Exception):
19 def __init__(self, doc, orig_excpt):
21 self.orig = orig_excpt
24 return 'Exception:\n\t%s\nUsing file:\n%s' % (self.orig, self.d)
26 def _eval_xml(self,node, pool, cr, uid, idref):
27 if node.nodeType == node.TEXT_NODE:
28 return node.data.encode("utf8")
29 elif node.nodeType == node.ELEMENT_NODE:
30 if node.nodeName in ('field','value'):
31 t = node.getAttribute('type') or 'char'
32 if len(node.getAttribute('search')):
33 f_search = node.getAttribute("search").encode('utf-8')
34 f_model = node.getAttribute("model").encode('ascii')
35 f_use = node.getAttribute("use").encode('ascii')
36 f_name = node.getAttribute("name").encode('utf-8')
39 q = eval(f_search, idref)
40 ids = pool.get(f_model).search(cr, uid, q)
42 ids = map(lambda x: x[f_use], pool.get(f_model).read(cr, uid, ids, [f_use]))
43 _cols = pool.get(f_model)._columns
44 if (f_name in _cols) and _cols[f_name]._type=='many2many':
49 if isinstance(f_val, tuple):
52 a_eval = node.getAttribute('eval')
56 idref['ref'] = lambda x: self.id_get(cr, False, x)
61 logger = netsvc.Logger()
62 logger.notifyChannel("init", netsvc.LOG_INFO, 'could not find pytz library')
63 class pytzclass(object):
67 return eval(a_eval, idref)
69 def _process(s, idref):
70 m = re.findall('[^%]%\((.*?)\)[ds]', s)
73 idref[id]=self.id_get(cr, False, id)
75 txt = '<?xml version="1.0"?>\n'+_process("".join([i.toxml().encode("utf8") for i in node.childNodes]), idref)
76 # txt = '<?xml version="1.0"?>\n'+"".join([i.toxml().encode("utf8") for i in node.childNodes]) % idref
79 if t in ('char', 'int', 'float'):
81 for n in [i for i in node.childNodes]:
82 d+=str(_eval_xml(self,n,pool,cr,uid,idref))
92 elif t in ('list','tuple'):
94 for n in [i for i in node.childNodes if (i.nodeType == i.ELEMENT_NODE and i.nodeName=='value')]:
95 res.append(_eval_xml(self,n,pool,cr,uid,idref))
99 elif node.nodeName=="getitem":
100 for n in [i for i in node.childNodes if (i.nodeType == i.ELEMENT_NODE)]:
101 res=_eval_xml(self,n,pool,cr,uid,idref)
104 elif node.getAttribute('type') in ("int", "list"):
105 return res[int(node.getAttribute('index'))]
107 return res[node.getAttribute('index').encode("utf8")]
108 elif node.nodeName=="function":
110 a_eval = node.getAttribute('eval')
112 idref['ref'] = lambda x: self.id_get(cr, False, x)
113 args = eval(a_eval, idref)
114 for n in [i for i in node.childNodes if (i.nodeType == i.ELEMENT_NODE)]:
115 args.append(_eval_xml(self,n, pool, cr, uid, idref))
116 model = pool.get(node.getAttribute('model'))
117 method = node.getAttribute('name')
118 res = getattr(model, method)(cr, uid, *args)
121 escape_re = re.compile(r'(?<!\\)/')
123 return x.replace('\\/', '/')
125 class xml_import(object):
126 def _tag_delete(self, cr, rec, data_node=None):
127 d_model = rec.getAttribute("model")
128 d_search = rec.getAttribute("search")
129 ids = self.pool.get(d_model).search(cr,self.uid,eval(d_search))
131 self.pool.get(d_model).unlink(cr, self.uid, ids)
132 #self.pool.get('ir.model.data')._unlink(cr, self.uid, d_model, ids, direct=True)
135 def _tag_report(self, cr, rec, data_node=None):
137 for dest,f in (('name','string'),('model','model'),('report_name','name')):
138 res[dest] = rec.getAttribute(f).encode('utf8')
139 assert res[dest], "Attribute %s of report is empty !" % (f,)
140 for field,dest in (('rml','report_rml'),('xml','report_xml'),('xsl','report_xsl')):
141 if rec.hasAttribute(field):
142 res[dest] = rec.getAttribute(field).encode('utf8')
143 if rec.hasAttribute('auto'):
144 res['auto'] = eval(rec.getAttribute('auto'))
145 if rec.hasAttribute('header'):
146 res['header'] = eval(rec.getAttribute('header'))
147 xml_id = rec.getAttribute('id').encode('utf8')
148 id = self.pool.get('ir.model.data')._update(cr, self.uid, "ir.actions.report.xml", self.module, res, xml_id, mode=self.mode)
149 self.idref[xml_id] = id
150 if not rec.hasAttribute('menu') or eval(rec.getAttribute('menu')):
151 keyword = str(rec.getAttribute('keyword') or 'client_print_multi')
152 keys = [('action',keyword),('res_model',res['model'])]
153 value = 'ir.actions.report.xml,'+str(id)
154 replace = rec.hasAttribute('replace') and rec.getAttribute("replace")
155 self.pool.get('ir.model.data').ir_set(cr, self.uid, 'action', keyword, res['name'], [res['model']], value, replace=replace, isobject=True)
158 def _tag_function(self, cr, rec, data_node=None):
159 _eval_xml(self,rec, self.pool, cr, self.uid, self.idref)
162 def _tag_wizard(self, cr, rec, data_node=None):
163 string = rec.getAttribute("string").encode('utf8')
164 model = rec.getAttribute("model").encode('utf8')
165 name = rec.getAttribute("name").encode('utf8')
166 xml_id = rec.getAttribute('id').encode('utf8')
167 multi = rec.hasAttribute('multi') and eval(rec.getAttribute('multi'))
168 res = {'name': string, 'wiz_name': name, 'multi':multi}
170 id = self.pool.get('ir.model.data')._update(cr, self.uid, "ir.actions.wizard", self.module, res, xml_id, mode=self.mode)
171 self.idref[xml_id] = id
173 if not rec.hasAttribute('menu') or eval(rec.getAttribute('menu')):
174 keyword = str(rec.getAttribute('keyword') or 'client_action_multi')
175 keys = [('action',keyword),('res_model',model)]
176 value = 'ir.actions.wizard,'+str(id)
177 replace = rec.hasAttribute('replace') and rec.getAttribute("replace")
178 self.pool.get('ir.model.data').ir_set(cr, self.uid, 'action', keyword, string, [model], value, replace=replace, isobject=True)
181 def _tag_act_window(self, cr, rec, data_node=None):
182 name = rec.hasAttribute('name') and rec.getAttribute('name').encode('utf-8')
183 xml_id = rec.getAttribute('id').encode('utf8')
184 type = rec.hasAttribute('type') and rec.getAttribute('type').encode('utf-8') or 'ir.actions.act_window'
186 if rec.hasAttribute('view'):
187 view_id = self.id_get(cr, 'ir.actions.act_window', rec.getAttribute('view').encode('utf-8'))
188 domain = rec.hasAttribute('domain') and rec.getAttribute('domain').encode('utf-8')
189 context = rec.hasAttribute('context') and rec.getAttribute('context').encode('utf-8') or '{}'
190 res_model = rec.getAttribute('res_model').encode('utf-8')
191 src_model = rec.hasAttribute('src_model') and rec.getAttribute('src_model').encode('utf-8')
192 view_type = rec.hasAttribute('view_type') and rec.getAttribute('view_type').encode('utf-8') or 'form'
193 view_mode = rec.hasAttribute('view_mode') and rec.getAttribute('view_mode').encode('utf-8') or 'tree,form'
194 usage = rec.hasAttribute('usage') and rec.getAttribute('usage').encode('utf-8')
196 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 }
198 id = self.pool.get('ir.model.data')._update(cr, self.uid, 'ir.actions.act_window', self.module, res, xml_id, mode=self.mode)
199 self.idref[xml_id] = id
202 keyword = 'client_action_relate'
203 keys = [('action', keyword), ('res_model', res_model)]
204 value = 'ir.actions.act_window,'+str(id)
205 replace = rec.hasAttribute('replace') and rec.getAttribute('replace')
206 self.pool.get('ir.model.data').ir_set(cr, self.uid, 'action', keyword, xml_id, [src_model], value, replace=replace, isobject=True)
209 def _tag_ir_set(self, cr, rec, data_node=None):
210 if not self.mode=='init':
213 for field in [i for i in rec.childNodes if (i.nodeType == i.ELEMENT_NODE and i.nodeName=="field")]:
214 f_name = field.getAttribute("name").encode('utf-8')
215 f_val = _eval_xml(self,field,self.pool, cr, self.uid, self.idref)
217 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))
220 def _tag_workflow(self, cr, rec, data_node=None):
222 model = str(rec.getAttribute('model'))
223 wf_service = netsvc.LocalService("workflow")
224 wf_service.trg_validate(self.uid, model,
225 self.id_get(cr, model, rec.getAttribute('ref')),
226 str(rec.getAttribute('action')), cr)
229 def _tag_menuitem(self, cr, rec, data_node=None):
230 rec_id = rec.getAttribute("id").encode('ascii')
231 m_l = map(escape, escape_re.split(rec.getAttribute("name").encode('utf8')))
233 for idx, menu_elem in enumerate(m_l):
235 cr.execute('select id from ir_ui_menu where parent_id=%d and name=%s', (pid, menu_elem))
237 cr.execute('select id from ir_ui_menu where parent_id is null and name=%s', (menu_elem,))
240 # we are at the last menu element/level (it's a leaf)
241 values = {'parent_id': pid,'name':menu_elem}
243 if rec.hasAttribute('action'):
244 a_action = rec.getAttribute('action').encode('utf8')
245 a_type = rec.getAttribute('type').encode('utf8') or 'act_window'
247 "act_window": 'STOCK_NEW',
248 "report.xml": 'STOCK_PASTE',
249 "wizard": 'STOCK_EXECUTE',
251 values['icon'] = icons.get(a_type,'STOCK_NEW')
252 if a_type=='act_window':
253 a_id = self.id_get(cr, 'ir.actions.%s'% a_type, a_action)
254 cr.execute('select view_type,view_mode,name from ir_act_window where id=%d', (int(a_id),))
255 action_type,action_mode,action_name = cr.fetchone()
256 if action_type=='tree':
257 values['icon'] = 'STOCK_INDENT'
258 elif action_mode and action_mode.startswith('tree'):
259 values['icon'] = 'STOCK_JUSTIFY_FILL'
260 elif action_mode and action_mode.startswith('graph'):
261 values['icon'] = 'terp-account'
262 if not values['name']:
263 values['name'] = action_name
264 if rec.hasAttribute('sequence'):
265 values['sequence'] = int(rec.getAttribute('sequence'))
266 if rec.hasAttribute('icon'):
267 values['icon'] = str(rec.getAttribute('icon'))
268 if rec.hasAttribute('groups'):
269 g_names = rec.getAttribute('groups').split(',')
271 for group in g_names:
272 g_ids.extend(self.pool.get('res.groups').search(cr, self.uid, [('name', '=', group)]))
273 values['groups_id'] = [(6, 0, g_ids)]
274 xml_id = rec.getAttribute('id').encode('utf8')
275 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)
277 # the menuitem already exists
279 xml_id = idx==len(m_l)-1 and rec.getAttribute('id').encode('utf8')
281 npid = self.pool.get('ir.model.data')._update_dummy(cr, self.uid, 'ir.ui.menu', self.module, xml_id, idx==len(m_l)-1)
283 print 'Menu Error', self.module, xml_id, idx==len(m_l)-1
285 # the menuitem does't exist but we are in branch (not a leaf)
286 pid = self.pool.get('ir.ui.menu').create(cr, self.uid, {'parent_id' : pid, 'name' : menu_elem})
288 self.idref[rec_id] = pid
290 if rec.hasAttribute('action') and pid:
291 a_action = rec.getAttribute('action').encode('utf8')
292 a_type = rec.getAttribute('type').encode('utf8') or 'act_window'
293 a_id = self.id_get(cr, 'ir.actions.%s' % a_type, a_action)
294 action = "ir.actions.%s,%d" % (a_type, a_id)
295 self.pool.get('ir.model.data').ir_set(cr, self.uid, 'action', 'tree_but_open', 'Menuitem', [('ir.ui.menu', int(pid))], action, True, True)
296 return ('ir.ui.menu', pid)
298 def _tag_record(self, cr, rec, data_node=None):
299 rec_model = rec.getAttribute("model").encode('ascii')
300 model = self.pool.get(rec_model)
301 assert model, "The model %s does not exist !" % (rec_model,)
302 rec_id = rec.getAttribute("id").encode('ascii')
304 # if not rec_id and not data_node.getAttribute('noupdate'):
305 # print "Warning", rec_model
307 if data_node.getAttribute('noupdate') and not self.mode == 'init':
308 # check if the xml record has an id string
310 id = self.pool.get('ir.model.data')._update_dummy(cr, self.uid, rec_model, self.module, rec_id)
311 # check if the resource already existed at the last update
313 # if it existed, we don't update the data, but we need to
314 # know the id of the existing record anyway
315 self.idref[rec_id] = id
318 # if the resource didn't exist
319 if rec.getAttribute("forcecreate"):
320 # we want to create it, so we let the normal "update" behavior happen
323 # otherwise do nothing
326 # otherwise it is skipped
330 for field in [i for i in rec.childNodes if (i.nodeType == i.ELEMENT_NODE and i.nodeName=="field")]:
331 #TODO: most of this code is duplicated above (in _eval_xml)...
332 f_name = field.getAttribute("name").encode('utf-8')
333 f_ref = field.getAttribute("ref").encode('ascii')
334 f_search = field.getAttribute("search").encode('utf-8')
335 f_model = field.getAttribute("model").encode('ascii')
336 if not f_model and model._columns.get(f_name,False):
337 f_model = model._columns[f_name]._obj
338 f_use = field.getAttribute("use").encode('ascii') or 'id'
342 q = eval(f_search, self.idref)
344 assert f_model, 'Define an attribute model="..." in your .XML file !'
345 f_obj = self.pool.get(f_model)
346 # browse the objects searched
347 s = f_obj.browse(cr, self.uid, f_obj.search(cr, self.uid, q))
348 # column definitions of the "local" object
349 _cols = self.pool.get(rec_model)._columns
350 # if the current field is many2many
351 if (f_name in _cols) and _cols[f_name]._type=='many2many':
352 f_val = [(6, 0, map(lambda x: x[f_use], s))]
354 # otherwise (we are probably in a many2one field),
355 # take the first element of the search
361 f_val = self.id_get(cr, f_model, f_ref)
363 f_val = _eval_xml(self,field, self.pool, cr, self.uid, self.idref)
364 if model._columns.has_key(f_name):
365 if isinstance(model._columns[f_name], osv.fields.integer):
368 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 )
370 self.idref[rec_id] = id
373 def id_get(self, cr, model, id_str):
374 if id_str in self.idref:
375 return self.idref[id_str]
378 mod,id_str = id_str.split('.')
379 result = self.pool.get('ir.model.data')._get_id(cr, self.uid, mod, id_str)
380 return self.pool.get('ir.model.data').read(cr, self.uid, [result], ['res_id'])[0]['res_id']
382 def parse(self, xmlstr):
383 d = xml.dom.minidom.parseString(xmlstr)
384 de = d.documentElement
385 for n in [i for i in de.childNodes if (i.nodeType == i.ELEMENT_NODE and i.nodeName=="data")]:
386 for rec in n.childNodes:
387 if rec.nodeType == rec.ELEMENT_NODE:
388 if rec.nodeName in self._tags:
390 self._tags[rec.nodeName](self.cr, rec, n)
393 logger = netsvc.Logger()
394 logger.notifyChannel("init", netsvc.LOG_INFO, '\n'+rec.toxml())
400 def __init__(self, cr, module, idref, mode):
405 self.pool = pooler.get_pool(cr.dbname)
406 # self.pool = osv.osv.FakePool(module)
409 'menuitem': self._tag_menuitem,
410 'record': self._tag_record,
411 'report': self._tag_report,
412 'wizard': self._tag_wizard,
413 'delete': self._tag_delete,
414 'ir_set': self._tag_ir_set,
415 'function': self._tag_function,
416 'workflow': self._tag_workflow,
417 'act_window': self._tag_act_window,
426 def convert_csv_import(cr, module, fname, csvcontent, idref={}, mode='init'):
429 model = ('.'.join(fname.split('.')[:-1]).split('-'))[0]
430 #model = fname.split('.')[0].replace('_', '.')
432 #remove folder path from model
433 head, model = os.path.split(model)
435 pool = pooler.get_pool(cr.dbname)
436 # pool = osv.osv.FakePool(module)
438 input=StringIO.StringIO(csvcontent)
439 reader = csv.reader(input, quotechar='"', delimiter=',')
440 fields = reader.next()
445 datas.append( map(lambda x:x.decode('utf8').encode('utf8'), line))
446 if len(datas) > COMMIT_STEP:
447 pool.get(model).import_data(cr, uid, fields, datas)
452 pool.get(model).import_data(cr, uid, fields, datas)
458 def convert_xml_import(cr, module, xmlstr, idref={}, mode='init'):
459 obj = xml_import(cr, module, idref, mode)
464 def convert_xml_export(res):
466 pool=pooler.get_pool(cr.dbname)
467 cr=pooler.db.cursor()
469 d = xml.dom.minidom.getDOMImplementation().createDocument(None, "terp", None)
470 de = d.documentElement
471 data=d.createElement("data")
473 de.appendChild(d.createTextNode('Some textual content.'))