From 715f846ebedd0bd0ae5e57cb2ba1cc16421e8113 Mon Sep 17 00:00:00 2001 From: "david.beal@akretion.com" Date: Fri, 11 Jul 2014 15:19:17 +0200 Subject: [PATCH 1/6] [ADD] tree_view_record_id module --- tree_view_record_id/__init__.py | 11 ++++++ tree_view_record_id/__openerp__.py | 53 +++++++++++++++++++++++++++ tree_view_record_id/orm.py | 57 ++++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 tree_view_record_id/__init__.py create mode 100644 tree_view_record_id/__openerp__.py create mode 100644 tree_view_record_id/orm.py diff --git a/tree_view_record_id/__init__.py b/tree_view_record_id/__init__.py new file mode 100644 index 000000000..f306fa695 --- /dev/null +++ b/tree_view_record_id/__init__.py @@ -0,0 +1,11 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# licence AGPL version 3 or later +# see licence in __openerp__.py or http://www.gnu.org/licenses/agpl-3.0.txt +# Copyright (C) 2014 Akretion (http://www.akretion.com). +# @author David BEAL +# +############################################################################## + +import orm diff --git a/tree_view_record_id/__openerp__.py b/tree_view_record_id/__openerp__.py new file mode 100644 index 000000000..dd04071eb --- /dev/null +++ b/tree_view_record_id/__openerp__.py @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- +############################################################################### +# +# Copyright (C) 2012-TODAY Akretion . +# All Rights Reserved +# @author David BEAL +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################### + +{ + 'name': 'Tree View Record Id', + 'version': '0.1', + 'category': 'Other modules', + 'sequence': 10, + 'author': 'Akretion', + 'summary': "Add id field to tree views", + 'description': """ +Add Id field to all non arborescent tree views. +Id field is the primary key of the table (Odoo model). +Arborescent views like 'Products by Category' or 'Chart of accounts' haven't this field included. + """, + 'website': 'http://www.akretion.com', + 'depends': [ + 'base', + ], + 'data': [ + ], + 'demo': [ + ], + 'installable': True, + 'auto_install': True, + 'application': False, + 'images': [ + ], + 'css': [ + ], + 'js': [ + ], + 'qweb': [ + ], +} diff --git a/tree_view_record_id/orm.py b/tree_view_record_id/orm.py new file mode 100644 index 000000000..6f6994126 --- /dev/null +++ b/tree_view_record_id/orm.py @@ -0,0 +1,57 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# licence AGPL version 3 or later +# see licence in __openerp__.py or http://www.gnu.org/licenses/agpl-3.0.txt +# Copyright (C) 2014 Akretion (http://www.akretion.com). +# @author David BEAL +# +############################################################################## + +from openerp.osv import orm +from lxml import etree +import os + +""" procedural code is executed even if the module is not installed +""" + + +module_name = os.path.basename(os.path.dirname(os.path.abspath(__file__))) + +DUMMY_MODEL = 'module.%s.installed' % module_name.replace('_', '.') + + +class DummyModel(orm.Model): + """ Allow to check if module is installed or not + in fields_view_get method to avoid code execution if not + Only executed if the module is installed + """ + _name = DUMMY_MODEL + + +fields_view_get_orginal = orm.BaseModel.fields_view_get + + +def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, + toolbar=False, submenu=False): + res = fields_view_get_orginal( + self, cr, uid, view_id=view_id, view_type=view_type, context=context, + toolbar=toolbar, submenu=submenu) + if view_type == 'tree': + compatible_tree = False + # Tree views with res['field_parent'] different from False + # looks like 'Products by Category'. + # We don't modify these views + # to avoid to break them (js error) + if res.get('field_parent', True) is False: + compatible_tree = True + if compatible_tree and DUMMY_MODEL in self.pool.models.keys(): + doc = etree.XML(res['arch']) + if doc.xpath("//tree") and len(doc.xpath("//field[@name='id']")) == 0: + node = doc.xpath("//tree")[0] + node.append(etree.Element("field", name="id", string="Id")) + res['arch'] = etree.tostring(node) + return res + + +orm.BaseModel.fields_view_get = fields_view_get From 815b08b77a92ffd3ad42f077167646a56be44711 Mon Sep 17 00:00:00 2001 From: "david.beal@akretion.com" Date: Fri, 11 Jul 2014 21:41:52 +0200 Subject: [PATCH 2/6] [IMP] define a shorter code to set 'compatible_tree' variable --- tree_view_record_id/__openerp__.py | 4 ++-- tree_view_record_id/orm.py | 4 +--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/tree_view_record_id/__openerp__.py b/tree_view_record_id/__openerp__.py index dd04071eb..d8b1bfaff 100644 --- a/tree_view_record_id/__openerp__.py +++ b/tree_view_record_id/__openerp__.py @@ -25,9 +25,9 @@ 'category': 'Other modules', 'sequence': 10, 'author': 'Akretion', - 'summary': "Add id field to tree views", + 'summary': "Adds id field to tree views", 'description': """ -Add Id field to all non arborescent tree views. +Adds Id field to all non arborescent tree views. Id field is the primary key of the table (Odoo model). Arborescent views like 'Products by Category' or 'Chart of accounts' haven't this field included. """, diff --git a/tree_view_record_id/orm.py b/tree_view_record_id/orm.py index 6f6994126..2922d9e3c 100644 --- a/tree_view_record_id/orm.py +++ b/tree_view_record_id/orm.py @@ -38,13 +38,11 @@ def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, self, cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar, submenu=submenu) if view_type == 'tree': - compatible_tree = False + compatible_tree = res.get('field_parent', True) is False # Tree views with res['field_parent'] different from False # looks like 'Products by Category'. # We don't modify these views # to avoid to break them (js error) - if res.get('field_parent', True) is False: - compatible_tree = True if compatible_tree and DUMMY_MODEL in self.pool.models.keys(): doc = etree.XML(res['arch']) if doc.xpath("//tree") and len(doc.xpath("//field[@name='id']")) == 0: From 4dcc5d5ed257814fe8d12ad46270de98b3a4786a Mon Sep 17 00:00:00 2001 From: "david.beal@akretion.com" Date: Thu, 17 Jul 2014 22:22:43 +0200 Subject: [PATCH 3/6] [FIX] switch to auto_install = False --- tree_view_record_id/__openerp__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tree_view_record_id/__openerp__.py b/tree_view_record_id/__openerp__.py index d8b1bfaff..06a0e8954 100644 --- a/tree_view_record_id/__openerp__.py +++ b/tree_view_record_id/__openerp__.py @@ -40,7 +40,7 @@ Arborescent views like 'Products by Category' or 'Chart of accounts' haven't thi 'demo': [ ], 'installable': True, - 'auto_install': True, + 'auto_install': False, 'application': False, 'images': [ ], From c9be26748531753985858544435df4fb7f11db25 Mon Sep 17 00:00:00 2001 From: "david.beal@akretion.com" Date: Sat, 19 Jul 2014 13:32:32 +0200 Subject: [PATCH 4/6] [FIX] exclude field addition to tree views buit on transient models --- tree_view_record_id/__openerp__.py | 9 ++++++--- tree_view_record_id/orm.py | 4 ++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/tree_view_record_id/__openerp__.py b/tree_view_record_id/__openerp__.py index 06a0e8954..adc922c53 100644 --- a/tree_view_record_id/__openerp__.py +++ b/tree_view_record_id/__openerp__.py @@ -27,9 +27,12 @@ 'author': 'Akretion', 'summary': "Adds id field to tree views", 'description': """ -Adds Id field to all non arborescent tree views. -Id field is the primary key of the table (Odoo model). -Arborescent views like 'Products by Category' or 'Chart of accounts' haven't this field included. +Adds Id field in all tree views of any modules/models, except: + +* Arborescent tree views like 'Products by Category', 'Chart of accounts', etc. +* Tree views (like in wizard 'Change password') built on transient models which don't have this column in their table. + +Id field is the primary key of standard sql tables defined by the orm (Odoo model). """, 'website': 'http://www.akretion.com', 'depends': [ diff --git a/tree_view_record_id/orm.py b/tree_view_record_id/orm.py index 2922d9e3c..ade6c62bc 100644 --- a/tree_view_record_id/orm.py +++ b/tree_view_record_id/orm.py @@ -43,6 +43,10 @@ def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, # looks like 'Products by Category'. # We don't modify these views # to avoid to break them (js error) + if '_transient_max_count' in self.pool[res['model']].__dict__.keys(): + # model with '_transient_max_count' key are transient model + # transient models haven't 'id' column in mostly case + compatible_tree = False if compatible_tree and DUMMY_MODEL in self.pool.models.keys(): doc = etree.XML(res['arch']) if doc.xpath("//tree") and len(doc.xpath("//field[@name='id']")) == 0: From 64e25aafd959054bcee9d997aec2d72922f73577 Mon Sep 17 00:00:00 2001 From: "david.beal@akretion.com" Date: Sun, 14 Sep 2014 16:49:52 +0200 Subject: [PATCH 5/6] [IMP] add comment for port to next version --- tree_view_record_id/orm.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tree_view_record_id/orm.py b/tree_view_record_id/orm.py index ade6c62bc..95f6bca77 100644 --- a/tree_view_record_id/orm.py +++ b/tree_view_record_id/orm.py @@ -13,6 +13,10 @@ from lxml import etree import os """ procedural code is executed even if the module is not installed + + TRY TO SWITCH to _register_hook() in the model to avoid + execution when not installed in V 8.0 version + """ From 07338ea73277176df371bc5cd9d2c671b5e7cd87 Mon Sep 17 00:00:00 2001 From: "david.beal@akretion.com" Date: Mon, 15 Sep 2014 13:21:06 +0200 Subject: [PATCH 6/6] [FIX] flake8 --- tree_view_record_id/__openerp__.py | 6 ++++-- tree_view_record_id/orm.py | 7 ++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/tree_view_record_id/__openerp__.py b/tree_view_record_id/__openerp__.py index adc922c53..c0ccb23fb 100644 --- a/tree_view_record_id/__openerp__.py +++ b/tree_view_record_id/__openerp__.py @@ -30,9 +30,11 @@ Adds Id field in all tree views of any modules/models, except: * Arborescent tree views like 'Products by Category', 'Chart of accounts', etc. -* Tree views (like in wizard 'Change password') built on transient models which don't have this column in their table. +* Tree views (like in wizard 'Change password') built on transient models + which don't have this column in their table. -Id field is the primary key of standard sql tables defined by the orm (Odoo model). +Id field is the primary key of standard sql tables +defined by the orm (Odoo model). """, 'website': 'http://www.akretion.com', 'depends': [ diff --git a/tree_view_record_id/orm.py b/tree_view_record_id/orm.py index 95f6bca77..66d34eae8 100644 --- a/tree_view_record_id/orm.py +++ b/tree_view_record_id/orm.py @@ -36,8 +36,8 @@ class DummyModel(orm.Model): fields_view_get_orginal = orm.BaseModel.fields_view_get -def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, - toolbar=False, submenu=False): +def fields_view_get(self, cr, uid, view_id=None, view_type='form', + context=None, toolbar=False, submenu=False): res = fields_view_get_orginal( self, cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar, submenu=submenu) @@ -53,7 +53,8 @@ def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, compatible_tree = False if compatible_tree and DUMMY_MODEL in self.pool.models.keys(): doc = etree.XML(res['arch']) - if doc.xpath("//tree") and len(doc.xpath("//field[@name='id']")) == 0: + if (doc.xpath("//tree") and + len(doc.xpath("//field[@name='id']"))) == 0: node = doc.xpath("//tree")[0] node.append(etree.Element("field", name="id", string="Id")) res['arch'] = etree.tostring(node)