~ubuntu-branches/ubuntu/trusty/openerp-client/trusty

« back to all changes in this revision

Viewing changes to bin/modules/action/main.py

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Baumann
  • Date: 2008-12-07 20:17:00 UTC
  • Revision ID: james.westby@ubuntu.com-20081207201700-a875pic3sd7xkoru
Tags: upstream-5.0.0~alpha
ImportĀ upstreamĀ versionĀ 5.0.0~alpha

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    OpenERP, Open Source Management Solution   
 
5
#    Copyright (C) 2004-2008 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 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 General Public License for more details.
 
17
#
 
18
#    You should have received a copy of the GNU General Public License
 
19
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
#
 
21
##############################################################################
 
22
 
 
23
import os, time, base64, datetime
 
24
 
 
25
import service
 
26
import rpc
 
27
 
 
28
import wizard
 
29
import printer
 
30
 
 
31
import common
 
32
import tools
 
33
from widget.view.form_gtk.many2one import dialog
 
34
 
 
35
class main(service.Service):
 
36
    def __init__(self, name='action.main'):
 
37
        service.Service.__init__(self, name)
 
38
 
 
39
    def exec_report(self, name, data, context={}):
 
40
        datas = data.copy()
 
41
        ids = datas['ids']
 
42
        del datas['ids']
 
43
        if not ids:
 
44
            ids =  rpc.session.rpc_exec_auth('/object', 'execute', datas['model'], 'search', [])
 
45
            if ids == []:
 
46
                common.message(_('Nothing to print!'))
 
47
                return False
 
48
            datas['id'] = ids[0]
 
49
        ctx = rpc.session.context.copy()
 
50
        ctx.update(context)
 
51
        report_id = rpc.session.rpc_exec_auth('/report', 'report', name, ids, datas, ctx)
 
52
        state = False
 
53
        attempt = 0
 
54
        while not state:
 
55
            val = rpc.session.rpc_exec_auth('/report', 'report_get', report_id)
 
56
            if not val:
 
57
                return False
 
58
            state = val['state']
 
59
            if not state:
 
60
                time.sleep(1)
 
61
                attempt += 1
 
62
            if attempt>200:
 
63
                common.message(_('Printing aborted, too long delay !'))
 
64
                return False
 
65
        printer.print_data(val)
 
66
        return True
 
67
 
 
68
    def execute(self, act_id, datas, type=None, context={}):
 
69
        act_id = int(act_id)
 
70
        ctx = rpc.session.context.copy()
 
71
        ctx.update(context)
 
72
        if type is None:
 
73
            res = rpc.session.rpc_exec_auth('/object', 'execute', 'ir.actions.actions', 'read', int(act_id), ['type'], ctx)
 
74
            if not (res and len(res)):
 
75
                raise Exception, 'ActionNotFound'
 
76
            type=res['type']
 
77
 
 
78
        res = rpc.session.rpc_exec_auth('/object', 'execute', type, 'read', act_id, False, ctx)
 
79
        self._exec_action(res,datas,context)
 
80
 
 
81
    def _exec_action(self, action, datas, context={}):
 
82
        if 'type' not in action:
 
83
            return
 
84
        if action['type']=='ir.actions.act_window':
 
85
            for key in ('res_id', 'res_model', 'view_type', 'view_mode',
 
86
                    'limit', 'auto_refresh'):
 
87
                datas[key] = action.get(key, datas.get(key, None))
 
88
 
 
89
            if datas['limit'] is None or datas['limit'] == 0:
 
90
                datas['limit'] = 80
 
91
 
 
92
            view_ids=False
 
93
            if action.get('views', []):
 
94
                if isinstance(action['views'],list):
 
95
                    view_ids=[x[0] for x in action['views']]
 
96
                    datas['view_mode']=",".join([x[1] for x in action['views']])
 
97
                else:
 
98
#                    view_ids=[(action['view_type']=='tree') and 1 or False,(action['view_type']=='form') and 1 or False]
 
99
                    if action.get('view_id', False):
 
100
                        view_ids=[action['view_id'][0]]
 
101
            elif action.get('view_id', False):
 
102
                view_ids=[action['view_id'][0]]
 
103
 
 
104
            if not action.get('domain', False):
 
105
                action['domain']='[]'
 
106
            ctx = context.copy()
 
107
            ctx.update( {'active_id': datas.get('id',False), 'active_ids': datas.get('ids',[])} )
 
108
            ctx.update(tools.expr_eval(action.get('context','{}'), ctx.copy()))
 
109
 
 
110
            a = ctx.copy()
 
111
            a['time'] = time
 
112
            a['datetime'] = datetime
 
113
            domain = tools.expr_eval(action['domain'], a)
 
114
 
 
115
            if datas.get('domain', False):
 
116
                domain.append(datas['domain'])
 
117
            if action.get('target', False)=='new':
 
118
                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(','))
 
119
                if dia.dia.get_has_separator():
 
120
                    dia.dia.set_has_separator(False)
 
121
                dia.run()
 
122
                dia.destroy()
 
123
            else:
 
124
                obj = service.LocalService('gui.window')
 
125
                obj.create(view_ids, datas['res_model'], datas['res_id'], domain,
 
126
                        action['view_type'], datas.get('window',None), ctx,
 
127
                        datas['view_mode'], name=action.get('name', False),
 
128
                        limit=datas['limit'], auto_refresh=datas['auto_refresh'])
 
129
 
 
130
        elif action['type']=='ir.actions.server':
 
131
            ctx = context.copy()
 
132
            ctx.update({'active_id': datas.get('id',False), 'active_ids': datas.get('ids',[])})
 
133
            res = rpc.session.rpc_exec_auth('/object', 'execute', 'ir.actions.server', 'run', [action['id']], ctx)
 
134
            if res:
 
135
                self._exec_action(res, datas, context)
 
136
 
 
137
        elif action['type']=='ir.actions.wizard':
 
138
            win=None
 
139
            if 'window' in datas:
 
140
                win=datas['window']
 
141
                del datas['window']
 
142
            wizard.execute(action['wiz_name'], datas, parent=win, context=context)
 
143
 
 
144
        elif action['type']=='ir.actions.report.custom':
 
145
            if 'window' in datas:
 
146
                win=datas['window']
 
147
                del datas['window']
 
148
            datas['report_id'] = action['report_id']
 
149
            self.exec_report('custom', datas)
 
150
 
 
151
        elif action['type']=='ir.actions.report.xml':
 
152
            if 'window' in datas:
 
153
                win=datas['window']
 
154
                del datas['window']
 
155
            self.exec_report(action['report_name'], datas)
 
156
 
 
157
        elif action['type']=='ir.actions.act_url':
 
158
            tools.launch_browser(action.get('url',''))
 
159
 
 
160
    def exec_keyword(self, keyword, data={}, adds={}, context={}, warning=True):
 
161
        actions = None
 
162
        if 'id' in data:
 
163
            try:
 
164
                id = data.get('id', False)
 
165
                actions = rpc.session.rpc_exec_auth('/object', 'execute',
 
166
                        'ir.values', 'get', 'action', keyword,
 
167
                        [(data['model'], id)], False, rpc.session.context)
 
168
                actions = map(lambda x: x[2], actions)
 
169
            except rpc.rpc_exception, e:
 
170
#               common.error(_('Error: ')+str(e.type), e.message, e.data)
 
171
                return False
 
172
 
 
173
        keyact = {}
 
174
        for action in actions:
 
175
            keyact[action['name']] = action
 
176
        keyact.update(adds)
 
177
 
 
178
        res = common.selection(_('Select your action'), keyact)
 
179
        if res:
 
180
            (name,action) = res
 
181
            self._exec_action(action, data, context=context)
 
182
            return (name, action)
 
183
        return False
 
184
 
 
185
main()
 
186
 
 
187
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
 
188