File: ir.py

package info (click to toggle)
tryton-modules-dashboard 2.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 228 kB
  • sloc: python: 326; xml: 120; makefile: 7
file content (144 lines) | stat: -rw-r--r-- 5,215 bytes parent folder | download
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
#This file is part of Tryton.  The COPYRIGHT file at the top level of
#this repository contains the full copyright notices and license terms.
import math
from lxml import etree
from trytond.model import Model, fields
from trytond.transaction import Transaction
from trytond.cache import Cache
from trytond.pool import Pool


class View(Model):
    _name = 'ir.ui.view'

    def dashboard_id(self):
        '''
        Return the database id of view_dashboard

        :param user: the user id
        :return: an integer
        '''
        model_data_obj = Pool().get('ir.model.data')
        model_data_ids = model_data_obj.search([
            ('fs_id', '=', 'view_dashboard'),
            ('module', '=', 'dashboard'),
            ('inherit', '=', False),
            ], limit=1)
        if not model_data_ids:
            return 0
        model_data = model_data_obj.browse(model_data_ids[0])
        return model_data.db_id

    dashboard_id = Cache('ir.ui.view.dashboard_id')(dashboard_id)

    def _dashboard_element_action(self, action):
        '''
        Return etree Element for the given dashboard action.

        :param action: a BrowseRecord of dashboard.action
        :return: an etree Element
        '''
        return etree.Element('action', {
            'name': str(action.act_window.id),
            })

    def dashboard_view(self, arch):
        '''
        Add action to view arch of dashboard

        :param arch: a string with the xml arch of dashboard
        :return: a string with the new xml arch
        '''
        user_obj = Pool().get('res.user')
        tree = etree.fromstring(arch)
        root = tree.getroottree().getroot()
        user = user_obj.browse(Transaction().user)
        if user.dashboard_layout == 'square':
            root.set('col', str(int(math.ceil(math.sqrt(
                len(user.dashboard_actions))))))
            for action in user.dashboard_actions:
                root.append(self._dashboard_element_action(action))
        elif user.dashboard_layout == 'stack_right':
            group = None
            root.set('col', '2')
            for action in user.dashboard_actions:
                element = self._dashboard_element_action(action)
                if group is None:
                    root.append(element)
                    group = etree.Element('group', {
                            'col': '1',
                            'yexpand': '1',
                            'yfill': '1',
                            })
                    root.append(group)
                else:
                    group.append(element)
        elif user.dashboard_layout == 'stack_left':
            root.set('col', '2')
            group = etree.Element('group', {
                    'col': '1',
                    'yexpand': '1',
                    'yfill': '1',
                    })
            root.append(group)
            first = True
            for action in user.dashboard_actions:
                element = self._dashboard_element_action(action)
                if first:
                    first = False
                    root.append(element)
                else:
                    group.append(element)
        elif user.dashboard_layout == 'stack_top':
            root.set('col', '1')
            group = etree.Element('group', {
                    'col': str(len(user.dashboard_actions) - 1),
                    'xexpand': '1',
                    })
            root.append(group)
            first = True
            for action in user.dashboard_actions:
                element = self._dashboard_element_action(action)
                if first:
                    first = False
                    root.append(element)
                else:
                    group.append(element)
        elif user.dashboard_layout == 'stack_bottom':
            root.set('col', '1')
            group = etree.Element('group', {
                    'col': str(len(user.dashboard_actions) - 1),
                    'xexpand': '1',
                    })
            first = True
            for action in user.dashboard_actions:
                element = self._dashboard_element_action(action)
                if first:
                    first = False
                    root.append(element)
                else:
                    group.append(element)
            root.append(group)
        arch = etree.tostring(tree, encoding='utf-8')
        return arch

    def read(self, ids, fields_names=None):
        res = super(View, self).read(ids, fields_names=fields_names)
        if Transaction().user == 0:
            return res
        dashboard_id = self.dashboard_id()
        if not dashboard_id:
            # Restart the cache
            self.dashboard_id.reset()
        if fields_names is None \
                or 'arch' in fields_names:
            if isinstance(ids, (int, long)):
                if dashboard_id == ids:
                    res['arch'] = self.dashboard_view(res['arch'])
            elif dashboard_id in ids:
                for res2 in res:
                    if res2['id'] == dashboard_id:
                        res2['arch'] = self.dashboard_view(res2['arch'])
        return res

View()