Files
tryton/modules/dashboard/ir.py
T

134 lines
4.6 KiB
Python
Raw Normal View History

2014-12-02 12:55:19 +01:00
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
2010-08-19 22:12:19 +02:00
import math
2021-12-11 17:58:42 +01:00
2010-08-19 22:12:19 +02:00
from lxml import etree
2021-12-11 17:58:42 +01:00
2010-08-24 20:56:04 +02:00
from trytond.cache import Cache
2012-09-24 22:17:54 +02:00
from trytond.pool import Pool, PoolMeta
2021-12-11 17:58:42 +01:00
from trytond.transaction import Transaction
2012-09-24 22:17:54 +02:00
2009-12-23 00:04:42 +01:00
2018-06-26 19:11:47 +02:00
class View(metaclass=PoolMeta):
2012-09-24 22:17:54 +02:00
__name__ = 'ir.ui.view'
_dashboard_cache = Cache('ir.ui.view.dashboard_id')
2009-12-23 00:04:42 +01:00
2012-09-24 22:17:54 +02:00
@staticmethod
def dashboard_id():
2009-12-23 00:04:42 +01:00
'''
Return the database id of view_dashboard
'''
2012-09-24 22:17:54 +02:00
ModelData = Pool().get('ir.model.data')
models_data = ModelData.search([
('fs_id', '=', 'view_dashboard'),
('module', '=', 'dashboard'),
], limit=1)
if not models_data:
2009-12-23 00:04:42 +01:00
return 0
2012-09-24 22:17:54 +02:00
model_data, = models_data
2009-12-23 00:04:42 +01:00
return model_data.db_id
2012-09-24 22:17:54 +02:00
@staticmethod
def _dashboard_element_action(action):
2009-12-23 00:04:42 +01:00
'''
Return etree Element for the given dashboard action.
'''
return etree.Element('action', {
2012-09-24 22:17:54 +02:00
'name': str(action.act_window.id),
})
2009-12-23 00:04:42 +01:00
2012-09-24 22:17:54 +02:00
@classmethod
def dashboard_view(cls, arch):
2009-12-23 00:04:42 +01:00
'''
Add action to view arch of dashboard
'''
2012-09-24 22:17:54 +02:00
User = Pool().get('res.user')
2009-12-23 00:04:42 +01:00
tree = etree.fromstring(arch)
root = tree.getroottree().getroot()
2012-09-24 22:17:54 +02:00
user = User(Transaction().user)
2009-12-23 00:04:42 +01:00
if user.dashboard_layout == 'square':
root.set('col', str(int(math.ceil(math.sqrt(
len(user.dashboard_actions))))))
for action in user.dashboard_actions:
2012-09-24 22:17:54 +02:00
root.append(cls._dashboard_element_action(action))
2009-12-23 00:04:42 +01:00
elif user.dashboard_layout == 'stack_right':
group = None
root.set('col', '2')
for action in user.dashboard_actions:
2012-09-24 22:17:54 +02:00
element = cls._dashboard_element_action(action)
2009-12-23 00:04:42 +01:00
if group is None:
root.append(element)
group = etree.Element('group', {
2012-03-05 12:46:01 +01:00
'col': '1',
'yexpand': '1',
'yfill': '1',
})
2009-12-23 00:04:42 +01:00
root.append(group)
else:
group.append(element)
elif user.dashboard_layout == 'stack_left':
root.set('col', '2')
group = etree.Element('group', {
2012-03-05 12:46:01 +01:00
'col': '1',
'yexpand': '1',
'yfill': '1',
})
2009-12-23 00:04:42 +01:00
root.append(group)
first = True
for action in user.dashboard_actions:
2012-09-24 22:17:54 +02:00
element = cls._dashboard_element_action(action)
2009-12-23 00:04:42 +01:00
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', {
2012-03-05 12:46:01 +01:00
'col': str(len(user.dashboard_actions) - 1),
'xexpand': '1',
})
2009-12-23 00:04:42 +01:00
root.append(group)
first = True
for action in user.dashboard_actions:
2012-09-24 22:17:54 +02:00
element = cls._dashboard_element_action(action)
2009-12-23 00:04:42 +01:00
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', {
2012-03-05 12:46:01 +01:00
'col': str(len(user.dashboard_actions) - 1),
'xexpand': '1',
})
2009-12-23 00:04:42 +01:00
first = True
for action in user.dashboard_actions:
2012-09-24 22:17:54 +02:00
element = cls._dashboard_element_action(action)
2009-12-23 00:04:42 +01:00
if first:
first = False
root.append(element)
else:
group.append(element)
root.append(group)
2017-03-08 09:42:01 +01:00
arch = etree.tostring(tree, encoding='utf-8').decode('utf-8')
2009-12-23 00:04:42 +01:00
return arch
2012-09-24 22:17:54 +02:00
@classmethod
def read(cls, ids, fields_names):
res = super(View, cls).read(ids, fields_names)
2010-08-19 22:12:19 +02:00
if Transaction().user == 0:
2009-12-23 00:04:42 +01:00
return res
2012-09-24 22:17:54 +02:00
dashboard_id = cls.dashboard_id()
2009-12-23 00:04:42 +01:00
if not dashboard_id:
# Restart the cache
2012-10-10 01:03:07 +02:00
cls._dashboard_cache.clear()
2009-12-23 00:04:42 +01:00
if fields_names is None \
or 'arch' in fields_names:
2012-09-24 22:17:54 +02:00
if dashboard_id in ids:
2009-12-23 00:04:42 +01:00
for res2 in res:
if res2['id'] == dashboard_id:
2012-09-24 22:17:54 +02:00
res2['arch'] = cls.dashboard_view(res2['arch'])
2009-12-23 00:04:42 +01:00
return res