~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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
###############################################################################
#
# 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
#
###############################################################################

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

import pkg_resources
import cherrypy

from openerp import rpc
from openerp import common

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

from openerp import widgets as tw

from form import Form


#rpc.session = rpc.RPCSession('localhost', '8070', 'socket', storage=cherrypy.session)

class State(Form):

    path = '/workflow/state'    # mapping from root

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

        params.path = self.path
        params.function = 'create_state'

        if params.id and cherrypy.request.path_info == self.path + '/view':
            params.load_counter = 2

        params.hidden_fields = [tw.form.Hidden(name='wkf_id', default=params.wkf_id)]
        form = self.create_form(params, tg_errors)

        field = form.screen.widget.get_widgets_by_name('wkf_id')[0]
        field.set_value(params.wkf_id or False)
        field.readonly = True

        vals = getattr(cherrypy.request, 'terp_validators', {})
        vals['wkf_id'] = tw.validators.Int()

        hide = []

        hide += form.screen.widget.get_widgets_by_name('out_transitions')
        hide += form.screen.widget.get_widgets_by_name('in_transitions')
        hide += form.screen.widget.get_widgets_by_name('', kind=tw.form.Separator)

        for w in hide:
            w.visible = False

        return dict(form=form, params=params)

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

        params, data = TinyDict.split(kw)

        if not params.model:
            params.update(kw)

        params.view_mode = ['form']
        params.view_type = 'form'
        params.editable = True

        return self.create(params)

    @expose(methods=('POST',))
    def delete(self, id, **kw):

        error_msg = None
        proxy = rpc.RPCProxy('workflow.activity')
        res_act = proxy.unlink(int(id))

        if not res_act:
            error_msg = _('Could not delete state')

        return dict(error = error_msg)

    @expose('json')
    def get_info(self, id, **kw):

        proxy_act = rpc.RPCProxy('workflow.activity')
        search_act = proxy_act.search([('id', '=', int(id))], 0, 0, 0, rpc.session.context)
        data = proxy_act.read(search_act, [], rpc.session.context)

        return dict(data=data[0])

class Connector(Form):

    path = '/workflow/connector'    # mapping from root

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

        params.path = self.path
        params.function = 'update_connection'

        if params.id and cherrypy.request.path_info == self.path + '/view':
            params.load_counter = 2

        params.hidden_fields = [tw.form.Hidden(name='act_from', default=params.start),
                                tw.form.Hidden(name='act_to', default=params.end)]

        form = self.create_form(params, tg_errors)

        field_act_from = form.screen.widget.get_widgets_by_name('act_from')[0]
        field_act_from.set_value(params.start or False)
        field_act_from.readonly = True

        field_act_to = form.screen.widget.get_widgets_by_name('act_to')[0]
        field_act_to.set_value(params.end or False)
        field_act_to.readonly = True

        vals = getattr(cherrypy.request, 'terp_validators', {})
        vals['act_from'] = tw.validators.Int()
        vals['act_to'] = tw.validators.Int()

        return dict(form=form, params=params)

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

        params, data = TinyDict.split(kw)

        if not params.model:
            params.update(kw)

        params.view_mode = ['form']
        params.view_type = 'form'
        params.editable = True

        return self.create(params)

    @expose('json', methods=('POST',))
    def delete(self, id, **kw):

        error_msg = None
        proxy = rpc.RPCProxy('workflow.transition')
        res_tr = proxy.unlink(int(id))

        if not res_tr:
            error_msg = _('Could not delete state')

        return dict(error=error_msg)

    @expose('json', methods=('POST',))
    def auto_create(self, act_from, act_to, **kw):

        proxy_tr = rpc.RPCProxy('workflow.transition')
        id = proxy_tr.create({'act_from': act_from, 'act_to': act_to})
        data = proxy_tr.read(id, [], rpc.session.context);

        if id>0:
            return dict(flag=True,data=data)
        else:
            return dict(flag=False)

    @expose('json')
    def get_info(self, id, **kw):

        proxy_tr = rpc.RPCProxy('workflow.transition')
        search_tr = proxy_tr.search([('id', '=', int(id))], 0, 0, 0, rpc.session.context)
        data = proxy_tr.read(search_tr, [], rpc.session.context)

        return dict(data=data[0])

    @expose('json', methods=('POST',))
    def change_ends(self, id, field, value):

        proxy_tr = rpc.RPCProxy('workflow.transition')
        id = proxy_tr.write([int(id)], {field: int(value)}, rpc.session.context)
        return dict()


class Workflow(Form):

    path = '/workflow'

    @expose(template="templates/workflow.mako")
    def index(self, model, id=None):

        proxy = rpc.RPCProxy("workflow")
        if id:
            ids = proxy.search([('id', '=', id)], 0, 0, 0, rpc.session.context)
        else:
            ids = proxy.search([('osv', '=', model)], 0, 0, 0, rpc.session.context)

        if not ids:
            raise common.message(_('No workflow associated!'))

        wkf = proxy.read(ids, [], rpc.session.context)[0]
        return dict(wkf=wkf)

    @expose('json')
    def get_info(self, id, **kw):

        proxy = rpc.RPCProxy("workflow")
        search_ids = proxy.search([('id', '=' , int(id))], 0, 0, 0, rpc.session.context)
        graph_search = proxy.graph_get(search_ids[0], (140, 180), rpc.session.context)

        nodes = graph_search['nodes']
        transitions = graph_search['transitions']

        connectors = {}
        list_tr = [];

        for tr in transitions:
            list_tr.append(tr)
            t = connectors.setdefault(tr,{})
            t['id'] = tr
            t['s_id'] = transitions[tr][0]
            t['d_id'] = transitions[tr][1]

        proxy_tr = rpc.RPCProxy("workflow.transition")
        search_trs = proxy_tr.search([('id', 'in', list_tr)], 0, 0, 0, rpc.session.context)
        data_trs = proxy_tr.read(search_trs, ['signal', 'condition', 'act_from', 'act_to'], rpc.session.context)

        for tr in data_trs:
            t = connectors.get(tr['id'])
            t['signal'] = tr['signal']
            t['condition'] = tr['condition']
            t['source'] = tr['act_from'][1]
            t['destination'] = tr['act_to'][1]

        proxy_act = rpc.RPCProxy("workflow.activity")
        search_acts = proxy_act.search([('wkf_id', '=', int(id))], 0, 0, 0, rpc.session.context)
        data_acts = proxy_act.read(search_acts, ['action', 'kind', 'flow_start', 'flow_stop', 'subflow_id'], rpc.session.context)

        for act in data_acts:
            n = nodes.get(str(act['id']))
            n['id'] = act['id']
            n['flow_start'] = act['flow_start']
            n['flow_stop'] = act['flow_stop']
            n['action'] = act['action']
            n['kind'] = act['kind']
            n['subflow_id'] = act['subflow_id']

        return dict(nodes=nodes,conn=connectors)

    state = State()
    connector = Connector()


class WorkflowList(SecuredController):

    @expose(template="templates/wkf_list.mako")
    def index(self, model, active=False):

        params = TinyDict()
        params.model = 'workflow'
        params.view_mode = ['tree']

        params.domain = [('osv', '=', model)]

        screen = tw.screen.Screen(params, selectable=1)
        screen.widget.pageable = False

        return dict(screen=screen, model=model, active=active)

    @expose()
    def create(self, model, **kw):

        wkf_name = kw.get('name')
        on_create = kw.get('on_create')

        if not wkf_name:
            raise redirect('/workflowlist', model=model)

        proxy = rpc.RPCProxy('workflow')
        proxy.create(dict(osv=model, name=wkf_name, on_create=on_create))

        raise redirect('/workflowlist', model=model)

    @expose()
    def delete(self, model, id):

        id = int(id)

        proxy = rpc.RPCProxy('workflow')
        proxy.unlink(id)

        raise redirect('/workflowlist', model=model)

    @expose()
    def activate(self, model, id):

        activate_id = int(id)

        proxy = rpc.RPCProxy('workflow')
        search_ids = proxy.search([('osv', '=', model)], 0, 0, 0, rpc.session.context)

        for id in search_ids:
            if id==activate_id:
                proxy.write([id], {'on_create': True})
            else:
                proxy.write([id], {'on_create': False})

        raise redirect('/workflowlist', model=model)

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