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