bugfixed for menu disable
[openerp-client.git] / bin / modules / action / main.py
blob244e90412d5b52d3834caaf254b78b4b866d2ff8
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # Copyright (c) 2004-2008 TINY SPRL. (http://tiny.be) All Rights Reserved.
6 # $Id$
8 # WARNING: This program as such is intended to be used by professional
9 # programmers who take the whole responsability of assessing all potential
10 # consequences resulting from its eventual inadequacies and bugs
11 # End users who are looking for a ready-to-use solution with commercial
12 # garantees and support are strongly adviced to contract a Free Software
13 # Service Company
15 # This program is Free Software; you can redistribute it and/or
16 # modify it under the terms of the GNU General Public License
17 # as published by the Free Software Foundation; either version 2
18 # of the License, or (at your option) any later version.
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 ##############################################################################
31 import os, time, base64, datetime
33 import service
34 import rpc
36 import wizard
37 import printer
39 import common
40 import tools
41 from widget.view.form_gtk.many2one import dialog
43 class main(service.Service):
44 def __init__(self, name='action.main'):
45 service.Service.__init__(self, name)
47 def exec_report(self, name, data, context={}):
48 datas = data.copy()
49 ids = datas['ids']
50 del datas['ids']
51 if not ids:
52 ids = rpc.session.rpc_exec_auth('/object', 'execute', datas['model'], 'search', [])
53 if ids == []:
54 common.message(_('Nothing to print!'))
55 return False
56 datas['id'] = ids[0]
57 ctx = rpc.session.context.copy()
58 ctx.update(context)
59 report_id = rpc.session.rpc_exec_auth('/report', 'report', name, ids, datas, ctx)
60 state = False
61 attempt = 0
62 while not state:
63 val = rpc.session.rpc_exec_auth('/report', 'report_get', report_id)
64 if not val:
65 return False
66 state = val['state']
67 if not state:
68 time.sleep(1)
69 attempt += 1
70 if attempt>200:
71 common.message(_('Printing aborted, too long delay !'))
72 return False
73 printer.print_data(val)
74 return True
76 def execute(self, act_id, datas, type=None, context={}):
77 ctx = rpc.session.context.copy()
78 ctx.update(context)
79 if type is None:
80 res = rpc.session.rpc_exec_auth('/object', 'execute', 'ir.actions.actions', 'read', act_id, ['type'], ctx)
81 if not (res and len(res)):
82 raise Exception, 'ActionNotFound'
83 type=res['type']
85 res = rpc.session.rpc_exec_auth('/object', 'execute', type, 'read', act_id, False, ctx)
86 self._exec_action(res,datas,context)
88 def _exec_action(self, action, datas, context={}):
89 if 'type' not in action:
90 return
91 if action['type']=='ir.actions.act_window':
92 for key in ('res_id', 'res_model', 'view_type', 'view_mode',
93 'limit', 'auto_refresh'):
94 datas[key] = action.get(key, datas.get(key, None))
96 if datas['limit'] is None or datas['limit'] == 0:
97 datas['limit'] = 80
99 view_ids=False
100 if action.get('views', []):
101 if isinstance(action['views'],list):
102 view_ids=[x[0] for x in action['views']]
103 datas['view_mode']=",".join([x[1] for x in action['views']])
104 else:
105 # view_ids=[(action['view_type']=='tree') and 1 or False,(action['view_type']=='form') and 1 or False]
106 if action.get('view_id', False):
107 view_ids=[action['view_id'][0]]
108 elif action.get('view_id', False):
109 view_ids=[action['view_id'][0]]
111 if not action.get('domain', False):
112 action['domain']='[]'
113 ctx = {'active_id': datas.get('id',False), 'active_ids': datas.get('ids',[])}
114 ctx.update(tools.expr_eval(action.get('context','{}'), ctx.copy()))
115 ctx.update(context)
117 a = ctx.copy()
118 a['time'] = time
119 a['datetime'] = datetime
120 domain = tools.expr_eval(action['domain'], a)
122 if datas.get('domain', False):
123 domain.append(datas['domain'])
124 if action.get('target', False)=='new':
125 dia = dialog(datas['res_model'], window=datas.get('window',None), domain=domain, context=ctx, view_ids=view_ids,target=True, view_type=datas.get('view_mode', 'tree').split(','))
126 if dia.dia.get_has_separator():
127 dia.dia.set_has_separator(False)
128 dia.run()
129 dia.destroy()
130 else:
131 obj = service.LocalService('gui.window')
132 obj.create(view_ids, datas['res_model'], datas['res_id'], domain,
133 action['view_type'], datas.get('window',None), ctx,
134 datas['view_mode'], name=action.get('name', False),
135 limit=datas['limit'], auto_refresh=datas['auto_refresh'])
137 elif action['type']=='ir.actions.server':
138 ctx = context.copy()
139 ctx.update({'active_id': datas.get('id',False), 'active_ids': datas.get('ids',[])})
140 res = rpc.session.rpc_exec_auth('/object', 'execute', 'ir.actions.server', 'run', [action['id']], ctx)
141 if res:
142 self._exec_action(res, datas, context)
144 elif action['type']=='ir.actions.wizard':
145 win=None
146 if 'window' in datas:
147 win=datas['window']
148 del datas['window']
149 wizard.execute(action['wiz_name'], datas, parent=win, context=context)
151 elif action['type']=='ir.actions.report.custom':
152 if 'window' in datas:
153 win=datas['window']
154 del datas['window']
155 datas['report_id'] = action['report_id']
156 self.exec_report('custom', datas)
158 elif action['type']=='ir.actions.report.xml':
159 if 'window' in datas:
160 win=datas['window']
161 del datas['window']
162 self.exec_report(action['report_name'], datas)
164 elif action['type']=='ir.actions.act_url':
165 tools.launch_browser(action.get('url',''))
167 def exec_keyword(self, keyword, data={}, adds={}, context={}, warning=True):
168 actions = None
169 if 'id' in data:
170 try:
171 id = data.get('id', False)
172 actions = rpc.session.rpc_exec_auth('/object', 'execute',
173 'ir.values', 'get', 'action', keyword,
174 [(data['model'], id)], False, rpc.session.context)
175 actions = map(lambda x: x[2], actions)
176 except rpc.rpc_exception, e:
177 # common.error(_('Error: ')+str(e.type), e.message, e.data)
178 return False
180 keyact = {}
181 for action in actions:
182 keyact[action['name']] = action
183 keyact.update(adds)
185 res = common.selection(_('Select your action'), keyact)
186 if res:
187 (name,action) = res
188 self._exec_action(action, data, context=context)
189 return (name, action)
190 return False
192 main()
194 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: