~mikel-martin/+junk/openerp-web-rb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
###############################################################################
#
# Copyright (C) 2007-TODAY Tiny ERP Pvt Ltd. All Rights Reserved.
#
# $Id$
#
# Developed by Tiny (http://openerp.com) and Axelor (http://axelor.com).
#
# The OpenERP web client is distributed under the "OpenERP Public License".
# It's based on Mozilla Public License Version (MPL) 1.1 with following
# restrictions:
#
# -   All names, links and logos of Tiny, Open ERP and Axelor must be
#     kept as in original distribution without any changes in all software
#     screens, especially in start-up page and the software header, even if
#     the application source code has been changed or updated or code has been
#     added.
#
# -   All distributions of the software must keep source code with OEPL.
#
# -   All integrations to any other software must keep source code with OEPL.
#
# If you need commercial licence to remove this kind of restriction please
# contact us.
#
# You can see the MPL licence at: http://www.mozilla.org/MPL/MPL-1.1.html
#
###############################################################################

import re

from openerp.tools import expose
from openerp.tools import redirect
from openerp.tools import validate
from openerp.tools import error_handler

import cherrypy

from openerp import rpc
from openerp import tools
from openerp import common
from openerp import icons

from openerp import widgets as tw

from openerp.controllers.base import SecuredController
from openerp.controllers.form import default_error_handler
from openerp.utils import TinyDict

from openerp import validators

import form

class Wizard(SecuredController):

    def execute(self, params):

        action = params.name
        model = params.model
        state = params.state
        datas = params.datas

        form = None
        buttons = []

        if model:
            action = model.replace('wizard.', '', 1)
        else:
            model = 'wizard.' + action

        params.name = action
        params.model = model
        params.view_mode = []

        if 'form' not in datas:
            datas['form'] = {}

        wiz_id = params.wiz_id or rpc.session.execute('wizard', 'create', action)

        while state != 'end':

            ctx = rpc.session.context.copy()
            ctx.update(datas.get('context' or {}) or {})

            res = rpc.session.execute('wizard', 'execute', wiz_id, datas, state, ctx)

            if 'datas' in res:
                datas['form'].update(res['datas'])
            else:
                res['datas'] = {}

            if res['type']=='form':
                
                fields = res['fields']
                form_values = {}

                for f in fields:
                    if 'value' in fields[f]:
                        form_values[f] = fields[f]['value']
                        
                    if f in datas['form'] and fields[f]['type'] == "one2many":
                        datas['form'][f] = [(1, d, {}) for d in datas['form'][f]]

                form_values.update(datas['form'])

                datas['form'] = form_values

                res['datas'].update(datas['form'])
                
                params.is_wizard = True
                params.view_mode = ['form']
                params.view_type = 'form'
                params.views = {'form': res}
                
                # keep track of datas and some other required information
                params.hidden_fields = [tw.form.Hidden(name='_terp_datas', default=ustr(datas)),
                                        tw.form.Hidden(name='_terp_state2', default=state),
                                        tw.form.Hidden(name='_terp_wiz_id', default=wiz_id)]
                
                form = tw.form_view.ViewForm(params, name="view_form", action="/wizard/action")

                buttons = []
                for x in res.get('state', []):
                    x = list(x)
                    x[1] = re.sub('_(?!_)', '', x[1]) # remove mnemonic
                    
                    if len(x) >= 3:
                        x[2] = icons.get_icon(x[2])
                        
                    buttons.append(tuple(x))

                params.state = state
                target = getattr(cherrypy.request, '_terp_view_target', None)
                
                return dict(form=form, buttons=buttons, show_header_footer=target!='new')

            elif res['type']=='action':
                from openerp.controllers import actions

                act_res = actions.execute(res['action'], **datas)
                if act_res:
                    return act_res

                state = res['state']

            elif res['type']=='print':
                from openerp.controllers import actions

                datas['report_id'] = res.get('report_id', False)
                if res.get('get_id_from_action', False):
                    backup_ids = datas['ids']
                    datas['ids'] = datas['form']['ids']

                return actions.execute_report(res['report'], **datas)

            elif res['type']=='state':
                state = res['state']

        raise redirect('/wizard/end')

    @expose(template="templates/wizard.mako")
    def create(self, params, tg_errors=None):

        if tg_errors:
            form = cherrypy.request.terp_form
            buttons = cherrypy.request.terp_buttons
            return dict(form=form, buttons=buttons)

        return self.execute(params)

    @expose()
    def end(self, **kw):

        if 'wizard_parent_params' in cherrypy.session:
            frm = cherrypy.session['wizard_parent_form']
            params = cherrypy.session['wizard_parent_params']

            frm = eval('cherrypy.request.app.root' + frm.replace('/', '.'))
            try:
                return frm.create(params)
            except:
                pass  

        return """<html>
            <head>
                <script type="text/javascript">
                    if (window.opener) {
                        window.opener.setTimeout("window.location.reload()", 0);
                        window.close();
                    } else {
                        window.location.href = "/";
                    }
                </script>
            </head>
            <body>
            </body>
        </html>
        """

    def get_validation_schema(self):

        kw = cherrypy.request.params
        params, datas = TinyDict.split(kw)

        params.state = params.state2

        cherrypy.request.terp_validators = {}

        res = self.execute(params)

        form = res['form']
        buttons = res.get('buttons', [])

        cherrypy.request.terp_form = form
        cherrypy.request.terp_buttons = buttons

        vals = cherrypy.request.terp_validators
        keys = vals.keys()
        for k in keys:
            if k not in kw:
                vals.pop(k)

        form.validator = validators.Schema(**vals)
        return form

    @expose()
    @validate(form=get_validation_schema)
    @error_handler(default_error_handler)
    def report(self, tg_errors=None, tg_exceptions=None, **kw):

        if tg_exceptions:
            raise tg_exceptions

        params, datas = TinyDict.split(kw)
        params.datas['form'].update(datas)

        if tg_errors:
            return self.create(params, tg_errors=tg_errors)

        return self.execute(params)

    @expose()
    @validate(form=get_validation_schema)
    @error_handler(default_error_handler)
    def action(self, tg_errors=None, tg_exceptions=None, **kw):

        if tg_exceptions:
            raise tg_exceptions

        params, datas = TinyDict.split(kw)
        params.datas['form'].update(datas)

        return self.create(params, tg_errors=tg_errors)

# vim: ts=4 sts=4 sw=4 si et