~jfb-tempo-consulting/unifield-web/jfb-int

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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
###############################################################################
#
#  Copyright (C) 2007-TODAY OpenERP SA. All Rights Reserved.
#
#  $Id$
#
#  Developed by OpenERP (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 OpenERP 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.
#
#  You can see the MPL licence at: http://www.mozilla.org/MPL/MPL-1.1.html
#
###############################################################################
import copy
import simplejson

import cherrypy
from openerp.controllers import SecuredController
from openerp.utils import rpc, TinyDict, TinyForm, TinyFormError, context_with_concurrency_info, cache
from openerp.widgets import listgrid, listgroup

from . import form
from . import wizard
from openobject.tools import expose, ast

class List(SecuredController):

    _cp_path = "/openerp/listgrid"

    @expose('json', methods=('POST',))
    def save(self, **kw):
        params, data = TinyDict.split(kw)

        error = None
        error_field = None

        id = params.id or 0
        id = (id > 0) and id or 0

        ids = params.ids or []

        model = params.parent.model
        dashboard = False
        if model == 'board.board':
            dashboard = True
            model = params.model
        if model != params.model and not params.parent.id:
            error = _("Parent record doesn't exists...")

        if error:
            return dict(error=error)

        try:
            proxy = rpc.RPCProxy(model)
            frm = TinyForm(**kw).to_python()
            data = {}
            ctx = context_with_concurrency_info(params.parent.context, params.concurrency_info)

            source = params.source
            if source and source != '_terp_list':

                data = frm.chain_get(source)

                if data is None:
                    return dict(error_field=error_field, error=error, id=id, ids=str([int(i) for i in ids]))

                if '__id' in data: data.pop('__id')
                if 'id' in data: data.pop('id')

                if not dashboard:
                    fld = source.split('/')[-1]
                    data = {fld : [(id and 1, id, data.copy())]}
                    proxy.write([params.parent.id], data, ctx)
                else:
                    data = data.copy()
                    proxy.write([id], data, ctx)

                if not id:
                    all_ids = proxy.read([params.parent.id], [fld])[0][fld]
                    new_ids = [i for i in all_ids if i not in ids]

                    ids = all_ids
                    if new_ids:
                        id = new_ids[0]
            else:
                data = frm.copy()
                if 'id' in data: data.pop('id')

                if id > 0:
                    proxy.write([id], data, ctx)
                else:
                    id = proxy.create(data, params.parent.context or {})
                    ids = [id] + ids

        except TinyFormError as e:
            error_field = e.field
            error = ustr(e)
        except Exception as e:
            error = ustr(e)

        return dict(error_field=error_field, error=error, id=id, ids=str([int(i) for i in ids]))

    @expose('json', methods=('POST',))
    def remove(self, **kw):
        params, data = TinyDict.split(kw)
        error = None
        proxy = rpc.RPCProxy(params.model)
        if params.id:

            if params.model == 'ir.ui.view_sc' and cherrypy.session.get('terp_shortcuts'):
                for sc in cherrypy.session.get('terp_shortcuts'):
                    for id in params.id:
                        if id == sc['id']:
                            cherrypy.session['terp_shortcuts'].remove(sc)

            try:
                ctx = context_with_concurrency_info(params.context, params.concurrency_info)
                if isinstance(params.id, list):
                    proxy.unlink(params.id, ctx)
                    for i in params.id:
                        params.ids.remove(i)
                else:
                    proxy.unlink([params.id], ctx)
                    params.ids.remove(params.id)

                if params.model == 'res.request':
                    ids, ids2 = rpc.RPCProxy(params.model).request_get()
                    return dict(res_ids = ids)

                return dict(ids = params.ids, count = len(params.ids))
            except Exception as e:
                error = ustr(e)

        return dict(error=error)

    @expose('json')
    def get_m2m(self, name, model, view_id, view_type, ids, disabled=False):
        view_id = ast.literal_eval(view_id) or False
        ids = ast.literal_eval(ids) or []
        view = cache.fields_view_get(model, view_id, view_type, rpc.session.context)
        m2m_view = listgrid.List(name, model, view, ids,limit=20, editable=disabled!='true', m2m=1)
        m2m_view = ustr(m2m_view.render())
        return dict(m2m_view = m2m_view)

    @expose('json', methods=('POST',))
    def remove_o2m_defaults(self, o2m_value, index):
        o2m_value = eval(o2m_value)
        o2m_value.pop(int(index))
        return dict(o2m_value=ustr(o2m_value))

    @expose('json')
    def get_o2m_defaults(self, o2m_values, model, o2m_model, name, view_type, view_id,
                         o2m_view_type, o2m_view_id, editable, limit, offset, o2m_context, o2m_domain):

        view_id = view_id or False
        o2m_view_id = ast.literal_eval(o2m_view_id) or False
        o2m_view_type = o2m_view_type or 'form'
        context = dict(ast.literal_eval(o2m_context), **rpc.session.context)
        o2m_values = simplejson.loads(o2m_values)

        for o2m in o2m_values:
            o2m['id'] = 0

        if o2m_view_id:
            view = cache.fields_view_get(o2m_model, o2m_view_id, o2m_view_type, context)
        else:
            view = cache.fields_view_get(model, view_id, view_type, rpc.session.context)
            view = view['fields'][name]['views'][o2m_view_type]

        list_view = listgrid.List(name, model, view, ids=None, domain=o2m_domain, context=context, default_data=copy.deepcopy(o2m_values), limit=20, editable= editable,o2m=1)
        view=ustr(list_view.render())
        formated_o2m_values = []
        for o2m in o2m_values:
            o2m.pop('id', None)
            formated_o2m_values.append((0, 0, o2m))

        return dict(view=view, formated_o2m_values=ustr(formated_o2m_values))

    @expose()
    def multiple_groupby(self, model, name, grp_domain, group_by, view_id, view_type, parent_group, group_level, groups, no_leaf, **kw):
        grp_domain = ast.literal_eval(grp_domain)
        view = cache.fields_view_get(model, view_id, view_type, rpc.session.context.copy())
        group_by = ast.literal_eval(group_by)
        domain = grp_domain
        params = TinyDict(**kw)
        editable = params.editable
        selectable = params.selectable or 2
        context = params.context or {}
        context.update({'group_by_no_leaf': int(no_leaf), 'group_by': group_by, '__domain': domain})
        group_level = ast.literal_eval(group_level)
        groups = ast.literal_eval(groups)

        offset = params.offset or 0
        limit = params.limit or 20
        args = {'editable': editable,
                'view_mode': ['tree', 'form', 'calendar', 'graph'],
                'nolinks': 1, 'group_by_ctx': group_by,
                'selectable': selectable,
                'multiple_group_by': True,
                'offset': offset, 'limit': limit,
                'sort_key': kw.get('sort_key'),
                'sort_order': kw.get('sort_order')}

        listgrp = listgroup.MultipleGroup(name, model, view, ids=None, domain= domain, parent_group=parent_group, group_level=group_level, groups=groups, context=context, **args)
        return listgrp.render()

    @expose('json')
    def reload_graph(self, **kw):
        params, data = TinyDict.split(kw)
        view = cache.fields_view_get(params.model, params.view_id, 'graph',params.context)

        if params.group_by_ctx:
            if isinstance(params.group_by_ctx, str):
                params.group_by_ctx = params.group_by_ctx.split('group_')[-1]
            else:
                params.group_by_ctx = [x.split('group_')[-1] for x in params.group_by_ctx]

        if params.domain is None:
            params.domain = []
        if params.search_domain:
            params.domain.extend(params.search_domain)
        if not params.group_by_ctx:
            params.ids = None
        from view_graph.widgets import _graph
        wid = _graph.Graph(model=params.model,
                           view=view,
                           view_id=params.view_id,
                           ids=params.ids, domain=params.domain,
                           view_mode = params.view_mode,
                           context=params.context,
                           group_by = params.group_by_ctx)
        view=ustr(wid.render())
        return dict(view = view)

    @expose('jsonp', methods=('POST',))
    def get(self, **kw):
        params, data = TinyDict.split(kw)

        groupby = params.get('_terp_group_by_ctx')
        if groupby and isinstance(groupby, str):
            groupby = groupby.split(',')

        if params.get('_terp_filters_context'):
            if isinstance(params.filters_context, (list, tuple)):
                for filter_ctx in params.filters_context:
                    params.context.update(filter_ctx)
            else:
                params.context.update(params.filters_context)

        params['_terp_group_by_ctx'] = groupby
        if not params.search_text:
            params.ids = None

        source = (params.source or '') and str(params.source)
        if not params.view_type == 'graph':
            params.view_type = 'form'

        if params.view_type == 'form':
            params['_terp_nodefault'] = True

        # clean defaults groupby if options available
        if params['_terp_search_data'] and params['_terp_search_data'].get('group_by_ctx',False):
            for k,v in list(params.context.items()):
                if k.startswith('search_default') and k[15:] not in params['_terp_search_data'].get('group_by_ctx',{}):
                    del params['_terp_context'][k]

        if params.get('_terp_clear') or params.get('_terp_ids_to_show'):
            params.search_domain, params.filter_domain, params.ids = [], [], []
            params.search_data = {}
            for k,v in list(params.context.items()):
                if k.startswith('search_default'):
                    del params.context[k]

            for x in params.context.get('set_by_field', []):
                try:
                    del params.context[x]
                except KeyError:
                    pass

            if 'group_by' in params.context:
                del params.context['group_by']
            params.group_by_ctx = []

        #params.forced_domain = params.domain[:]
        if source == '_terp_list':
            if not params.view_type == 'graph':
                params.view_type = 'tree'
            if params.search_domain:
                params.domain += params.search_domain

            params.domain = params.domain or []
            if params.filter_domain:
                params.domain += params.filter_domain

        if params.get('_terp_ids_to_show'):
            params.search_domain = [('id', 'in', params.get('_terp_ids_to_show'))]

        # default_get context
        current = params.chain_get(source)
        if current and params.source_default_get:
            current.context = current.context or {}
            current.context.update(params.source_default_get)

        if params.wiz_id:
            res = wizard.Wizard().execute(params)
            frm = res['form']
        else:
            params.get_source = source
            frm = form.Form().create_form(params)

        if params.view_type == 'graph':
            wid = frm.screen.widget
        else:
            wid = frm.screen.get_widgets_by_name(source, kind=listgrid.List)[0]

        ids = wid.ids
        count = wid.count

        if params.edit_inline:
            wid.edit_inline = params.edit_inline

        if params.get('_terp_clear'):
            view=ustr(frm.render())
        else:
            view=ustr(wid.render())

        server_logs = ''
        if frm.logs and frm.screen.view_type == 'tree':
            server_logs = ustr(frm.logs.render())

        concurrency_info = getattr(cherrypy.request, 'terp_concurrency_info', None)

        return dict(ids=ids, count=count, view=view, logs=server_logs, concurrency_info=concurrency_info, previously_selected=frm.screen.set_previously_selected)

    @expose('json')
    def button_action(self, **kw):
        params, data = TinyDict.split(kw)

        name = params.button_name
        btype = params.button_type
        ctx = dict((params.context or {}), **rpc.session.context)

        ids = params.id or []
        if not isinstance(ids, list):
            ids = [ids]

        model = params.model

        list_grid = params.list_grid or '_terp_list'
        try:

            if btype == 'workflow':
                res = rpc.session.execute('object', 'exec_workflow', model, name, ids)
                if isinstance(res, dict):
                    from . import actions
                    return actions.execute(res, ids=ids)
                else:
                    return dict(reload=True, list_grid=list_grid)

            elif btype == 'object':
                ctx = params.context or {}
                ctx.update(rpc.session.context.copy())
                if list_grid != '_terp_list':
                    ctx['from_list_grid'] = list_grid

                res = rpc.session.execute('object', 'execute', model, name, ids, ctx)

                if isinstance(res, dict):
                    from . import actions
                    return actions.execute(res, ids=ids)
                else:
                    return dict(reload=True, list_grid=list_grid)

            elif btype == 'action':
                from . import actions

                action_id = int(name)
                action_type = actions.get_action_type(action_id)

                if action_type == 'ir.actions.wizard':
                    cherrypy.session['wizard_parent_form'] = '/form'
                    cherrypy.session['wizard_parent_params'] = params

                # Only one ID used for now. To be adapted at some point if
                # we need to support this kind of action on multiple ids at the
                # same time.
                id_ = ids[0] if ids else False
                res = actions.execute_by_id(action_id, type=action_type, model=model, id=id_, ids=ids, context=ctx or {})

                if res:
                    return res
                else:
                    return dict(reload=True, list_grid=list_grid)

            else:
                return dict(error = "Unallowed button type")
        except Exception as e:
            return dict(error = ustr(e))

    @expose('json', methods=('POST',))
    def groupbyDrag(self, model, children, domain, level='0'):
        """ update records represented by children domain based on groupby domain

        :param model: model to update
        :param children: children records domain or children record id
        :param domain: group_by domain will be convert to values
        """
        domain = ast.literal_eval(domain)
        children = ast.literal_eval(children)
        level = ast.literal_eval(level)
        if isinstance(children, list):
            children = list(children)
        else:
            children = [('id','=',children)]
        # convert domain to values
        values = {}
        for i, d in enumerate(domain):
            if len(d) == 3 and i <= level:
                values[d[0]] = d[2]
        children_ids = rpc.RPCProxy(model).search(children)
        rpc.RPCProxy(model).write(children_ids, values)
        return {}

    @expose('json', methods=('POST',))
    def dragRow(self, **kw):
        params, data = TinyDict.split(kw)
        id = params.id
        destination_index = params.destination_index
        ids = params.ids

        ids.insert(
            destination_index,
            ids.pop(ids.index(id)))

        Model = rpc.RPCProxy(params.model)
        for index, id in enumerate(ids):
            Model.write([id], {
                'sequence': index+1
            }, rpc.session.context)
        return {}

    @expose('json')
    def count_sum(self, model, ids, sum_fields):
        selected_ids = ast.literal_eval(ids)
        sum_fields = sum_fields.split(",")
        ctx = rpc.session.context.copy()

        proxy = rpc.RPCProxy(model)
        res = proxy.read(selected_ids, sum_fields, ctx)

        total = []
        for field in sum_fields:
            total.append([])

        for i in range(len(selected_ids)):
            for k in range(len(sum_fields)):
                total[k].append(res[i][sum_fields[k]])

        total_sum = []
        for s in total:
            total_sum.append(str(sum(s)))

        return dict(sum = total_sum)

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

        params, data = TinyDict.split(kw)

        id = params.id
        ids = params.ids or []
        if id not in ids or ids.index(id) == 0:
            return dict()

        proxy = rpc.RPCProxy(params.model)
        ctx = rpc.session.context.copy()

        prev_id = ids[ids.index(id)-1]

        try:
            res = proxy.read([id, prev_id], ['sequence'], ctx)
            records = dict([(r['id'], r['sequence']) for r in res])
            cur_seq = records[id]
            prev_seq = records[prev_id]
            if cur_seq == prev_seq:
                proxy.write([prev_id], {'sequence': cur_seq + 1}, ctx)
                proxy.write([id], {'sequence': prev_seq}, ctx)
            else:
                proxy.write([id], {'sequence': prev_seq}, ctx)
                proxy.write([prev_id], {'sequence': cur_seq}, ctx)

            return dict()
        except Exception as e:
            return dict(error=str(e))

    @expose('json', methods=('POST',))
    def moveDown(self, **kw):
        params, data = TinyDict.split(kw)

        id = params.id
        ids = params.ids or []

        if id not in ids or ids.index(id) == len(ids) - 1:
            return dict()

        proxy = rpc.RPCProxy(params.model)
        ctx = rpc.session.context.copy()

        next_id = ids[ids.index(id)+1]

        try:
            res = proxy.read([id, next_id], ['sequence'], ctx)
            records = dict([(r['id'], r['sequence']) for r in res])

            cur_seq = records[id]
            next_seq = records[next_id]

            if cur_seq == next_seq:
                proxy.write([next_id], {'sequence': cur_seq + 1}, ctx)
                proxy.write([id], {'sequence': next_seq}, ctx)
            else:
                proxy.write([id], {'sequence': next_seq}, ctx)
                proxy.write([next_id], {'sequence': cur_seq}, ctx)

            return dict()
        except Exception as e:
            return dict(error=str(e))

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