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