From db0f88479ecdce5ae998dbdc01b81bd3240ea712 Mon Sep 17 00:00:00 2001 From: Guewen Baconnier Date: Tue, 30 Jun 2015 11:28:43 +0200 Subject: [PATCH] Hide models without an active field --- record_archiver/models/__init__.py | 1 + record_archiver/models/ir_model.py | 73 +++++++++++++++++++++++ record_archiver/models/record_lifespan.py | 1 + 3 files changed, 75 insertions(+) create mode 100644 record_archiver/models/ir_model.py diff --git a/record_archiver/models/__init__.py b/record_archiver/models/__init__.py index 17ec9c568..8637ee398 100644 --- a/record_archiver/models/__init__.py +++ b/record_archiver/models/__init__.py @@ -1,3 +1,4 @@ from . import company +from . import ir_model from . import res_config from . import record_lifespan diff --git a/record_archiver/models/ir_model.py b/record_archiver/models/ir_model.py new file mode 100644 index 000000000..a9162d1eb --- /dev/null +++ b/record_archiver/models/ir_model.py @@ -0,0 +1,73 @@ +# -*- coding: utf-8 -*- +# +# +# Authors: Guewen Baconnier +# Copyright 2015 Camptocamp SA +# +# 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 . +# +# + +from openerp.osv import orm, fields + + +class IrModel(orm.Model): + _inherit = 'ir.model' + + def _compute_has_an_active_field(self, cr, uid, ids, name, + args, context=None): + res = {} + for model_id in ids: + active_field_ids = self.pool['ir.model.fields'].search( + cr, uid, + [('model_id', '=', model_id), + ('name', '=', 'active'), + ], + limit=1, + context=context) + res[model_id] = bool(active_field_ids) + return res + + def _search_has_an_active_field(self, cr, uid, obj, name, args, + context=None): + if not len(args): + return [] + fields_model = self.pool['ir.model.fields'] + domain = [] + for field, operator, value in args: + assert field == name + active_field_ids = fields_model.search( + cr, uid, [('name', '=', 'active')], context=context) + active_fields = fields_model.read(cr, uid, active_field_ids, + fields=['model_id'], + load='_classic_write', + context=context) + model_ids = [field['model_id'] for field in active_fields] + if operator == '=': + domain.append(('id', 'in', model_ids)) + elif operator == '!=': + domain.append(('id', 'not in', model_ids)) + else: + raise AssertionError('operator %s not allowed' % operator) + return domain + + _columns = { + 'has_an_active_field': fields.function( + _compute_has_an_active_field, + fnct_search=_search_has_an_active_field, + string='Has an active field', + readonly=True, + type='boolean', + ), + } diff --git a/record_archiver/models/record_lifespan.py b/record_archiver/models/record_lifespan.py index e6b1928dd..66cd222c4 100644 --- a/record_archiver/models/record_lifespan.py +++ b/record_archiver/models/record_lifespan.py @@ -43,6 +43,7 @@ class RecordLifespan(orm.Model): 'ir.model', string='Model', required=True, + domain=[('has_an_active_field', '=', True)], ), 'model': fields.related( 'model_id', 'model',