From cebd697aaed22426600cf2c397773764c842d969 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Mon, 24 Sep 2012 22:17:54 +0200 Subject: [PATCH] Active Record --- __init__.py | 9 ++++++ dashboard.py | 21 ++++++------- ir.py | 83 +++++++++++++++++++++++----------------------------- res.py | 32 +++++++++++--------- 4 files changed, 75 insertions(+), 70 deletions(-) diff --git a/__init__.py b/__init__.py index 4f8bd9ddc0..a23e5e7952 100644 --- a/__init__.py +++ b/__init__.py @@ -1,6 +1,15 @@ #This file is part of Tryton. The COPYRIGHT file at the top level of #this repository contains the full copyright notices and license terms. +from trytond.pool import Pool from .dashboard import * from .res import * from .ir import * + + +def register(): + Pool.register( + DashboardAction, + User, + View, + module='dashboard', type_='model') diff --git a/dashboard.py b/dashboard.py index 56111d393c..52b830fe21 100644 --- a/dashboard.py +++ b/dashboard.py @@ -4,11 +4,12 @@ from trytond.model import ModelView, ModelSQL, fields from trytond.transaction import Transaction from trytond.backend import TableHandler +__all__ = ['DashboardAction'] + class DashboardAction(ModelSQL, ModelView): "Dashboard Action" - _name = "dashboard.action" - + __name__ = "dashboard.action" user = fields.Many2One('res.user', 'User', required=True, select=True) sequence = fields.Integer('Sequence', @@ -20,17 +21,17 @@ class DashboardAction(ModelSQL, ModelView): ('res_model', '!=', ''), ]) - def __init__(self): - super(DashboardAction, self).__init__() - self._order.insert(0, ('sequence', 'ASC')) + @classmethod + def __setup__(cls): + super(DashboardAction, cls).__setup__() + cls._order.insert(0, ('sequence', 'ASC')) - def init(self, module_name): + @classmethod + def __register__(cls, module_name): cursor = Transaction().cursor - table = TableHandler(cursor, self, module_name) + table = TableHandler(cursor, cls, module_name) - super(DashboardAction, self).init(module_name) + super(DashboardAction, cls).__register__(module_name) # Migration from 2.4: drop required on sequence table.not_null_action('sequence', action='remove') - -DashboardAction() diff --git a/ir.py b/ir.py index cd310da867..865eb1b424 100644 --- a/ir.py +++ b/ir.py @@ -2,67 +2,62 @@ #this repository contains the full copyright notices and license terms. import math from lxml import etree -from trytond.model import Model from trytond.transaction import Transaction from trytond.cache import Cache -from trytond.pool import Pool +from trytond.pool import Pool, PoolMeta + +__all__ = ['View'] +__metaclass__ = PoolMeta -class View(Model): - _name = 'ir.ui.view' +class View: + __name__ = 'ir.ui.view' + _dashboard_cache = Cache('ir.ui.view.dashboard_id') - def dashboard_id(self): + @staticmethod + def dashboard_id(): ''' 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', '=', None), - ], limit=1) - if not model_data_ids: + ModelData = Pool().get('ir.model.data') + models_data = ModelData.search([ + ('fs_id', '=', 'view_dashboard'), + ('module', '=', 'dashboard'), + ('inherit', '=', None), + ], limit=1) + if not models_data: return 0 - model_data = model_data_obj.browse(model_data_ids[0]) + model_data, = models_data return model_data.db_id - dashboard_id = Cache('ir.ui.view.dashboard_id')(dashboard_id) - - def _dashboard_element_action(self, action): + @staticmethod + def _dashboard_element_action(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), - }) + 'name': str(action.act_window.id), + }) - def dashboard_view(self, arch): + @classmethod + def dashboard_view(cls, 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') + User = Pool().get('res.user') tree = etree.fromstring(arch) root = tree.getroottree().getroot() - user = user_obj.browse(Transaction().user) + user = User(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)) + root.append(cls._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) + element = cls._dashboard_element_action(action) if group is None: root.append(element) group = etree.Element('group', { @@ -83,7 +78,7 @@ class View(Model): root.append(group) first = True for action in user.dashboard_actions: - element = self._dashboard_element_action(action) + element = cls._dashboard_element_action(action) if first: first = False root.append(element) @@ -98,7 +93,7 @@ class View(Model): root.append(group) first = True for action in user.dashboard_actions: - element = self._dashboard_element_action(action) + element = cls._dashboard_element_action(action) if first: first = False root.append(element) @@ -112,7 +107,7 @@ class View(Model): }) first = True for action in user.dashboard_actions: - element = self._dashboard_element_action(action) + element = cls._dashboard_element_action(action) if first: first = False root.append(element) @@ -122,23 +117,19 @@ class View(Model): 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) + @classmethod + def read(cls, ids, fields_names=None): + res = super(View, cls).read(ids, fields_names=fields_names) if Transaction().user == 0: return res - dashboard_id = self.dashboard_id() + dashboard_id = cls.dashboard_id() if not dashboard_id: # Restart the cache - self.dashboard_id.reset() + cls._dashboard_cache.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: + if dashboard_id in ids: for res2 in res: if res2['id'] == dashboard_id: - res2['arch'] = self.dashboard_view(res2['arch']) + res2['arch'] = cls.dashboard_view(res2['arch']) return res - -View() diff --git a/res.py b/res.py index d16899b0d3..b3aeb5089e 100644 --- a/res.py +++ b/res.py @@ -1,13 +1,16 @@ #This file is part of Tryton. The COPYRIGHT file at the top level of #this repository contains the full copyright notices and license terms. -from trytond.model import Model, fields +from trytond.model import fields from trytond.backend import TableHandler from trytond.transaction import Transaction +from trytond.pool import PoolMeta + +__all__ = ['User'] +__metaclass__ = PoolMeta -class User(Model): - _name = "res.user" - +class User: + __name__ = "res.user" dashboard_layout = fields.Selection([ ('square', 'Square'), ('stack_right', 'Stack Right'), @@ -18,22 +21,23 @@ class User(Model): dashboard_actions = fields.One2Many('dashboard.action', 'user', 'Dashboard Actions') - def __init__(self): - super(User, self).__init__() - self._preferences_fields += [ + @classmethod + def __setup__(cls): + super(User, cls).__setup__() + cls._preferences_fields += [ 'dashboard_layout', 'dashboard_actions', - ] + ] - def init(self, module_name): - super(User, self).init(module_name) + @classmethod + def __register__(cls, module_name): + super(User, cls).__register__(module_name) cursor = Transaction().cursor - table = TableHandler(cursor, self, module_name) + table = TableHandler(cursor, cls, module_name) # Migration from 1.6 table.not_null_action('dashboard_layout', action='remove') - def default_dashboard_layout(self): + @staticmethod + def default_dashboard_layout(): return 'square' - -User()