[IMP] moved openerpweb into base.common to be renamed web.commom
[odoo/odoo.git] / addons / base_module_record / wizard / base_module_record_data.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU Affero General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU Affero General Public License for more details.
17 #
18 #    You should have received a copy of the GNU Affero General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 import wizard
24 import osv
25 import pooler
26 import time
27
28 info = '''<?xml version="1.0"?>
29 <form string="Module Recording">
30     <label string="Thanks For using Module Recorder" colspan="4" align="0.0"/>
31 </form>'''
32
33 intro_start_form = '''<?xml version="1.0"?>
34 <form string="Objects Recording">
35     <field name="check_date"/>
36     <newline/>
37     <field name="filter_cond"/>
38     <separator string="Choose objects to record" colspan="4"/>
39     <field name="objects" colspan="4" nolabel="1"/>
40     <group><field name="info_yaml"/></group>
41 </form>'''
42
43 intro_start_fields = {
44     'check_date':  {'string':"Record from Date",'type':'datetime','required':True, 'default': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S')},
45     'objects':{'string': 'Objects', 'type': 'many2many', 'relation': 'ir.model', 'help': 'List of objects to be recorded'},
46     'filter_cond':{'string':'Records only', 'type':'selection','selection':[('created','Created'),('modified','Modified'),('created_modified','Created & Modified')], 'required':True, 'default': lambda *args:'created'},
47     'info_yaml': {'string':'YAML','type':'boolean'}
48 }
49
50 exp_form = '''<?xml version="1.0"?>
51 <form string="Objects Recording">
52     <separator string="Result, paste this to your module's xml" colspan="4" />
53     <field name="res_text" nolabel="1"  colspan="4"/>
54 </form>'''
55
56 exp_fields = {
57     'res_text':  {'string':"Result",'type':'text', },
58 }
59
60 def _info_default(self, cr, uid, data, context):
61      pool = pooler.get_pool(cr.dbname)
62      mod = pool.get('ir.model')
63      list=('ir.ui.view','ir.ui.menu','ir.model','ir.model.fields','ir.model.access',\
64         'res.partner','res.partner.address','res.partner.category','workflow',\
65         'workflow.activity','workflow.transition','ir.actions.server','ir.server.object.lines')
66      data['form']['objects']=mod.search(cr,uid,[('model','in',list)])
67      cr.execute('select max(create_date) from ir_model_data')
68      c=(cr.fetchone())[0].split('.')[0]
69      c = time.strptime(c,"%Y-%m-%d %H:%M:%S")
70      sec=c.tm_sec + 1
71      c=(c[0],c[1],c[2],c[3],c[4],sec,c[6],c[7],c[8])
72      data['form']['check_date']=time.strftime("%Y-%m-%d %H:%M:%S",c)
73      return data['form']
74
75 def _record_objects(self, cr, uid, data, context):
76     check_date=data['form']['check_date']
77     filter=data['form']['filter_cond']
78     pool = pooler.get_pool(cr.dbname)
79     user=(pool.get('res.users').browse(cr,uid,uid)).login
80     mod = pool.get('ir.module.record')
81     mod_obj = pool.get('ir.model')
82     mod.recording_data = []
83
84     for id in data['form']['objects'][0][2]:
85         obj_name=(mod_obj.browse(cr,uid,id)).model
86         obj_pool=pool.get(obj_name)
87         if filter =='created':
88             search_condition =[('create_date','>',check_date)]
89         elif filter =='modified':
90             search_condition =[('write_date','>',check_date)]
91         elif filter =='created_modified':
92             search_condition =['|',('create_date','>',check_date),('write_date','>',check_date)]
93         if '_log_access' in dir(obj_pool):
94               if not (obj_pool._log_access):
95                   search_condition=[]
96               if '_auto' in dir(obj_pool):
97                   if not obj_pool._auto:
98                       continue
99         elif '_log_access' not in dir(obj_pool):
100              search_condition = []
101         search_ids=obj_pool.search(cr,uid,search_condition)
102         for s_id in search_ids:
103              args=(cr.dbname,uid,obj_name,'copy',s_id,{},context)
104              mod.recording_data.append(('query',args, {}, s_id))
105     return {'type': 'ir.actions.act_window_close'}
106
107 def _check(self, cr, uid, data, context):
108      pool = pooler.get_pool(cr.dbname)
109      mod = pool.get('ir.module.record')
110      if len(mod.recording_data):
111          if data['form']['info_yaml']:
112              return 'save_yaml'
113          else:
114              return 'info'
115      else:
116          return 'end'
117          
118 def _create_xml(self, cr, uid, data, context):
119     pool = pooler.get_pool(cr.dbname)
120     mod = pool.get('ir.module.record')
121     res_xml = mod.generate_xml(cr, uid)
122     return { 'res_text': res_xml }
123
124 def _create_yaml(self,cr,uid,data,context):
125     pool = pooler.get_pool(cr.dbname)
126     mod = pool.get('ir.module.record')
127     res_xml = mod.generate_yaml(cr, uid)
128     return { 'res_text': res_xml }
129
130 class base_module_record_objects(wizard.interface):
131     states = {
132          'init': {
133             'actions': [_info_default],
134             'result': {
135                 'type':'form',
136                 'arch':intro_start_form,
137                 'fields': intro_start_fields,
138                 'state':[
139                     ('end', 'Cancel', 'gtk-cancel'),
140                     ('record', 'Record', 'gtk-ok'),
141                 ]
142             }
143         },
144         'record': {
145             'actions': [],
146             'result': {'type':'action','action':_record_objects,'state':'check'}
147                 },
148         'check': {
149             'actions': [],
150             'result': {'type':'choice','next_state':_check}
151         },
152          'info': {
153             'actions': [ _create_xml ],
154             'result': {
155                 'type':'form',
156                 'arch': exp_form,
157                 'fields':exp_fields,
158                 'state':[
159                     ('end', 'End', 'gtk-cancel'),
160                 ]
161             },
162         },
163         'save_yaml': {
164             'actions': [ _create_yaml ],
165             'result': {
166                 'type':'form',
167                 'arch': exp_form,
168                 'fields':exp_fields,
169                 'state':[
170                     ('end', 'End', 'gtk-cancel'),
171                 ]
172             },
173         },
174          'end': {
175             'actions': [],
176             'result': {'type':'form', 'arch':info, 'fields':{}, 'state':[('end','OK')]}
177         },
178     }
179 base_module_record_objects('base_module_record.module_record_data')
180
181 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
182