[IMP] code improvement suggested by mga
[odoo/odoo.git] / addons / base_module_record / base_module_record.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 from xml.dom import minidom
23 from osv.osv import osv_pool
24 from osv import fields,osv
25 import netsvc
26 import pooler
27 import string
28 import tools
29
30 #objects_proxy = netsvc.ExportService.getService('object').__class__
31 class recording_objects_proxy(osv_pool):
32     def execute(self, *args, **argv):
33         if len(args) >= 6 and isinstance(args[5], dict):
34             _old_args = args[5].copy()
35         else:
36             _old_args = None
37         res = super(recording_objects_proxy, self).execute(*args, **argv)
38         pool = pooler.get_pool(args[0])
39         mod = pool.get('ir.module.record')
40         if mod and mod.recording:
41             if args[3] not in ('default_get','read','fields_view_get','fields_get','search','search_count','name_search','name_get','get','request_get', 'get_sc', 'unlink'):
42                 if _old_args is not None:
43                     args[5].update(_old_args)
44                     if args[5]:
45                         mod.recording_data.append(('query', args, argv,res))
46         return res
47
48     def exec_workflow(self, *args, **argv):
49         res = super(recording_objects_proxy, self).exec_workflow(*args, **argv)
50         pool = pooler.get_pool(args[0])
51         mod = pool.get('ir.module.record')
52         if mod and mod.recording:
53             mod.recording_data.append(('workflow', args, argv))
54         return res
55
56 recording_objects_proxy()
57
58 class xElement(minidom.Element):
59     """dom.Element with compact print
60     The Element in minidom has a problem: if printed, adds whitespace
61     around the text nodes. The standard will not ignore that whitespace.
62     This class simply prints the contained nodes in their compact form, w/o
63     added spaces.
64     """
65     def writexml(self, writer, indent="", addindent="", newl=""):
66         writer.write(indent)
67         minidom.Element.writexml(self, writer, indent='', addindent='', newl='')
68         writer.write(newl)
69
70 def doc_createXElement(xdoc, tagName):
71         e = xElement(tagName)
72         e.ownerDocument = xdoc
73         return e
74
75 class base_module_record(osv.osv):
76     _name = "ir.module.record"
77     _columns = {
78
79     }
80     def __init__(self, *args, **kwargs):
81         self.recording = 0
82         self.recording_data = []
83         self.depends = {}
84         super(base_module_record, self).__init__(*args, **kwargs)
85
86     # To Be Improved
87     def _create_id(self, cr, uid, model, data):
88         i = 0
89         while True:
90             try:
91                 name = filter(lambda x: x in string.letters, (data.get('name','') or '').lower())
92             except:
93                 name=''
94             val = model.replace('.','_')+'_'+name+ str(i)
95             i+=1
96             if val not in self.ids.values():
97                 break
98         return val
99
100     def _get_id(self, cr, uid, model, id):
101         if type(id)==type(()):
102             id=id[0]
103         if (model,id) in self.ids:
104             return self.ids[(model,id)], False
105         dt = self.pool.get('ir.model.data')
106         dtids = dt.search(cr, uid, [('model','=',model), ('res_id','=',id)])
107         if not dtids:
108             return None, None
109         obj = dt.browse(cr, uid, dtids[0])
110         self.depends[obj.module] = True
111         return obj.module+'.'+obj.name, obj.noupdate
112
113     def _create_record(self, cr, uid, doc, model, data, record_id, noupdate=False):
114         
115         data_pool = self.pool.get('ir.model.data')
116         model_pool = self.pool.get(model)
117         
118         record = doc.createElement('record')
119         record.setAttribute("id", record_id)
120         record.setAttribute("model", model)
121         record_list = [record]
122         lids  = data_pool.search(cr, uid, [('model','=',model)])
123         res = data_pool.read(cr, uid, lids[:1], ['module'])
124         if res:
125             self.depends[res[0]['module']]=True
126         fields = model_pool.fields_get(cr, uid)
127         for key,val in data.items():
128             if not (val or (fields[key]['type']=='boolean')):
129                 continue
130             if fields[key]['type'] in ('integer','float'):
131                 field = doc.createElement('field')
132                 field.setAttribute("name", key)
133                 field.setAttribute("eval", val and str(val) or 'False' )
134                 record.appendChild(field)
135             elif fields[key]['type'] in ('boolean',):
136                 field = doc.createElement('field')
137                 field.setAttribute("name", key)
138                 field.setAttribute("eval", val and '1' or '0' )
139                 record.appendChild(field)
140             elif fields[key]['type'] in ('many2one',):
141                 field = doc.createElement('field')
142                 field.setAttribute("name", key)
143                 if type(val) in (type(''),type(u'')):
144                     id = val
145                 else:
146                     id,update = self._get_id(cr, uid, fields[key]['relation'], val)
147                     noupdate = noupdate or update
148                 if not id:
149                     relation_pool = self.pool.get(fields[key]['relation'])
150                     
151                     field.setAttribute("model", fields[key]['relation'])
152                     fld_nm = relation_pool._rec_name
153                     name = relation_pool.read(cr, uid, val,[fld_nm])[fld_nm] or False
154                     field.setAttribute("search", str([(str(fld_nm) ,'=', name)]))
155                 else:
156                     field.setAttribute("ref", id)
157                 record.appendChild(field)
158             elif fields[key]['type'] in ('one2many',):
159                 for valitem in (val or []):
160                     if valitem[0] in (0,1):
161                         if key in model_pool._columns:
162                             fname = model_pool._columns[key]._fields_id
163                         else:
164                             fname = model_pool._inherit_fields[key][2]._fields_id
165                         valitem[2][fname] = record_id
166                         newid,update = self._get_id(cr, uid, fields[key]['relation'], valitem[1])
167                         if not newid:
168                             newid = self._create_id(cr, uid, fields[key]['relation'], valitem[2])
169                         self.ids[(fields[key]['relation'], valitem[1])] = newid
170
171                         childrecord, update = self._create_record(cr, uid, doc, fields[key]['relation'],valitem[2], newid)
172                         noupdate = noupdate or update
173                         record_list += childrecord
174                     else:
175                         pass
176             elif fields[key]['type'] in ('many2many',):
177                 res = []
178                 for valitem in (val or []):
179                     if valitem[0]==6:
180                         for id2 in valitem[2]:
181                             id,update = self._get_id(cr, uid, fields[key]['relation'], id2)
182                             self.ids[(fields[key]['relation'],id2)] = id
183                             noupdate = noupdate or update
184                             res.append(id)
185                         field = doc.createElement('field')
186                         field.setAttribute("name", key)
187                         field.setAttribute("eval", "[(6,0,["+','.join(map(lambda x: "ref('%s')" % (x,), res))+'])]')
188                         record.appendChild(field)
189             else:
190                 field = doc_createXElement(doc, 'field')
191                 field.setAttribute("name", key)
192                 field.appendChild(doc.createTextNode(val))
193                 record.appendChild(field)
194
195         return record_list, noupdate
196
197     def _create_yaml_record(self, cr, uid, model, data, record_id):
198         record={'model': model, 'id': str(record_id)}
199         
200         model_pool = self.pool.get(model)
201         data_pool = self.pool.get('ir.model.data')
202         
203         lids  = data_pool.search(cr, uid, [('model','=',model)])
204         res = data_pool.read(cr, uid, lids[:1], ['module'])
205         attrs={}
206         if res:
207             self.depends[res[0]['module']]=True
208         fields = model_pool.fields_get(cr, uid)
209         defaults={}
210         defaults[model] = model_pool.default_get(cr, uid, data)
211         for key,val in data.items():  
212             if ((key in defaults[model]) and (val ==  defaults[model][key])) and not(fields[key].get('required',False)):
213                 continue
214             if not (val or (fields[key]['type']=='boolean')):
215                 continue
216             elif fields[key]['type'] in ('boolean',):
217                 if not val:
218                     continue
219                 attrs[key] = val
220             elif fields[key]['type'] in ('integer','float'):
221                 attrs[key] = val
222             elif fields[key]['type'] in ('many2one',):
223                 if type(val) in (type(''), type(u'')):
224                     id = val
225                 else:
226                     id, update = self._get_id(cr, uid, fields[key]['relation'], val)
227                 attrs[key] = str(id)
228             elif fields[key]['type'] in ('one2many',):
229                 items=[[]]
230                 for valitem in (val or []):
231                     if valitem[0] in (0,1):
232                         if key in model_pool._columns:
233                             fname = model_pool._columns[key]._fields_id
234                         else:
235                             fname = model_pool._inherit_fields[key][2]._fields_id
236                         valitem[2][fname] = record_id
237                         newid,update = self._get_id(cr, uid, fields[key]['relation'], valitem[1])
238                         if not newid:
239                             newid = self._create_id(cr, uid, fields[key]['relation'], valitem[2])
240                         self.ids[(fields[key]['relation'], valitem[1])] = newid
241                         childrecord = self._create_yaml_record(cr, uid, fields[key]['relation'],valitem[2], newid)
242                         items[0].append(childrecord['attrs'])
243                 attrs[key] = items
244             elif fields[key]['type'] in ('many2many',):
245                 if (key in defaults[model]) and (val[0][2] ==  defaults[model][key]):
246                     continue
247                 res = []
248                 for valitem in (val or []):
249                     if valitem[0]==6:
250                         for id2 in valitem[2]:
251                             id,update = self._get_id(cr, uid, fields[key]['relation'], id2)
252                             self.ids[(fields[key]['relation'],id2)] = id
253                             res.append(str(id))
254                         m2m=[res]
255                 if m2m[0]:
256                     attrs[key] = m2m
257             else:
258                 val=val.replace('"','\'')
259                 try:
260                     attrs[key]=str(val)
261                 except:
262                     attrs[key]=tools.ustr(val)
263         record['attrs'] = attrs
264         return record
265
266     def get_copy_data(self, cr, uid, model, id, result):
267         res = []
268         obj=self.pool.get(model)
269         data=obj.read(cr, uid,[id])
270         if type(data)==type([]):
271             del data[0]['id']
272             data=data[0]
273         else:
274             del data['id']
275
276         mod_fields = obj.fields_get(cr, uid)
277         for f in filter(lambda a: isinstance(obj._columns[a], fields.function)\
278                     and (not obj._columns[a].store),obj._columns):
279             del data[f]
280
281         for key,val in data.items():
282             if result.has_key(key):
283                 continue
284             if mod_fields[key]['type'] == 'many2one':
285                 if type(data[key])==type(True) or type(data[key])==type(1):
286                     result[key]=data[key]
287                 elif not data[key]:
288                     result[key] = False
289                 else:
290                     result[key]=data[key][0]
291
292             elif mod_fields[key]['type'] in ('one2many',):
293 #                continue # due to this start stop recording will not record one2many field
294                 rel = mod_fields[key]['relation']
295                 if len(data[key]):
296                     res1=[]
297                     for rel_id in data[key]:
298                         res=[0,0]
299                         res.append(self.get_copy_data(cr, uid,rel,rel_id,{}))
300                         res1.append(res)
301                     result[key]=res1
302                 else:
303                     result[key]=data[key]
304
305             elif mod_fields[key]['type'] == 'many2many':
306                 result[key]=[(6,0,data[key])]
307
308             else:
309                 result[key]=data[key]
310         for k,v in obj._inherits.items():
311             del result[v]
312         return result
313
314     def _create_function(self, cr, uid, doc, model, name, record_id):
315         record = doc.createElement('function')
316         record.setAttribute("name", name)
317         record.setAttribute("model", model)
318         record_list = [record]
319
320         value = doc.createElement('value')
321         value.setAttribute('eval', '[ref(\'%s\')]' % (record_id, ))
322         value.setAttribute('model', model)
323
324         record.appendChild(value)
325         return record_list, False
326
327     def _generate_object_xml(self, cr, uid, rec, recv, doc, result=None):
328         record_list = []
329         noupdate = False
330         if rec[4]=='write':
331             for id in rec[5]:
332                 id,update = self._get_id(cr, uid, rec[3], id)
333                 noupdate = noupdate or update
334                 if not id:
335                     continue
336                 record,update = self._create_record(cr, uid, doc, rec[3], rec[6], id)
337                 noupdate = noupdate or update
338                 record_list += record
339
340         elif rec[4] in ('menu_create',):
341             for id in rec[5]:
342                 id,update = self._get_id(cr, uid, rec[3], id)
343                 noupdate = noupdate or update
344                 if not id:
345                     continue
346                 record,update = self._create_function(cr, uid, doc, rec[3], rec[4], id)
347                 noupdate = noupdate or update
348                 record_list += record
349
350         elif rec[3]=='create':
351             id = self._create_id(cr, uid, rec[2],rec[4])
352             record,noupdate = self._create_record(cr, uid, doc, rec[2], rec[4], id)
353             self.ids[(rec[3], result)] = id
354             record_list += record
355
356         elif rec[3]=='copy':
357             data=self.get_copy_data(cr,uid,rec[2],rec[4],rec[5])
358             copy_rec=(rec[0],rec[1],rec[2],rec[3],rec[4],data,rec[5])
359             rec=copy_rec
360             rec_data=[(self.recording_data[0][0],rec,self.recording_data[0][2],self.recording_data[0][3])]
361             self.recording_data=rec_data
362             id = self._create_id(cr, uid, rec[2],rec[5])
363             record,noupdate = self._create_record(cr, uid, doc, rec[2], rec[5], id)
364             self.ids[(rec[2], result)] = id
365             record_list += record
366
367         return record_list,noupdate
368
369     def _generate_object_yaml(self, cr, uid, rec, result=None):
370         if self.mode=="create":
371             id = self._create_id(cr, uid, rec[2],rec[4])
372             self.ids[(rec[2], result)] = id
373             record = self._create_yaml_record(cr, uid, rec[2], rec[4], id)
374             return record
375         data=self.get_copy_data(cr,uid,rec[2],rec[4],rec[5])
376         copy_rec=(rec[0],rec[1],rec[2],rec[3],rec[4],data,rec[5])
377         rec=copy_rec
378         rec_data=[(self.recording_data[0][0],rec,self.recording_data[0][2],self.recording_data[0][3])]
379         self.recording_data=rec_data
380         id = self._create_id(cr, uid, rec[2],rec[5])
381         record = self._create_yaml_record(cr, uid, str(rec[2]), rec[5], id)
382         self.ids[(rec[2], result)] = id
383         return record
384
385     def _generate_assert_xml(self, rec, doc):
386         pass
387
388     def generate_xml(self, cr, uid):
389         # Create the minidom document
390         if len(self.recording_data):
391             self.ids = {}
392             doc = minidom.Document()
393             terp = doc.createElement("openerp")
394             doc.appendChild(terp)
395             for rec in self.recording_data:
396                 if rec[0]=='workflow':
397                     rec_id,noupdate = self._get_id(cr, uid, rec[1][3], rec[1][5])
398                     if not rec_id:
399                         continue
400                     data = doc.createElement("data")
401                     terp.appendChild(data)
402                     wkf = doc.createElement('workflow')
403                     data.appendChild(wkf)
404                     wkf.setAttribute("model", rec[1][3])
405                     wkf.setAttribute("action", rec[1][4])
406                     if noupdate:
407                         data.setAttribute("noupdate", "1")
408                     wkf.setAttribute("ref", rec_id)
409                 if rec[0]=='query':
410                     res_list,noupdate = self._generate_object_xml(cr, uid, rec[1], rec[2], doc, rec[3])
411                     data = doc.createElement("data")
412                     if noupdate:
413                         data.setAttribute("noupdate", "1")
414                     if res_list:
415                         terp.appendChild(data)
416                     for res in res_list:
417                         data.appendChild(res)
418                 elif rec[0]=='assert':
419                         pass
420             return doc.toprettyxml(indent="\t").encode('utf-8')
421
422     def generate_yaml(self, cr, uid):
423         self.ids = {}
424         if len(self.recording_data):
425             strg='''import yaml
426             
427 class record(yaml.YAMLObject):
428     yaml_tag = u'!record'
429     def __init__(self, model, id=None, attrs={}):
430         self.model = model
431         self.id = id
432         self.attrs=attrs
433     def __repr__(self):
434         return '!record {model: %s, id: %s}:' % (str(self.model,), str(self.id,))
435
436 class ref(yaml.YAMLObject):
437     yaml_tag = u'!ref'
438     def __init__(self, expr="False"):
439         self.expr = expr
440     def __repr__(self):
441         return 'ref(%s)' % (str(self.expr,))
442
443 class eval(yaml.YAMLObject):
444     yaml_tag = u'!eval'
445     def __init__(self, expr="False"):
446         self.expr = expr
447     def __repr__(self):
448         return 'eval(%s)' % (str(self.expr,))
449 \n'''
450     
451             for rec in self.recording_data:
452                 if rec[1][3] == 'create':
453                     self.mode="create"
454                 elif rec[1][3] == 'copy':
455                     self.mode="copy"
456                 else:
457                     continue
458                 record= self._generate_object_yaml(cr, uid, rec[1],rec[3])
459                 strg+="object=yaml.load(unicode('''\n !record %s \n''','iso-8859-1'))"%record
460                 strg+='''
461 print object
462 attrs=yaml.dump(object.attrs, default_flow_style=False)
463 print attrs \n\n'''
464
465             import os
466             py_path = os.path.join(os.getcwd(), 'records.py')
467             txt_path = os.path.join(os.getcwd(), 'records.txt')
468             f = open(py_path, 'w')
469             f.write(strg)
470             f.close()
471             os.system('python %s > %s'%(py_path,txt_path))
472             f = open(txt_path, 'r+')
473             lines=f.readlines()
474             f.seek(0)
475             for line in lines:
476                 line=line.replace("''","'")
477                 if line.find('!record') == 0:
478                     line = "- \n" + "  " + line
479                 elif line.find('- -') != -1:
480                     line=line.replace('- -','  -')
481                     line = "    " + line
482                 else:
483                     line = "    " + line
484                 f.write(line)
485             f.close()
486             f = open(txt_path, 'r')
487             strg = ''.join(f.readlines()) 
488             f.close()
489             os.system('rm %s'%py_path)
490             os.system('rm %s'%txt_path)
491             return strg
492
493 base_module_record()
494 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
495