From 7d5767a7fccc598f9459252ed6a86a403f47c319 Mon Sep 17 00:00:00 2001 From: Patrick Tombez Date: Tue, 25 Sep 2018 11:20:41 +0200 Subject: [PATCH] [MIG] Update module to match V11 architecture --- configuration_helper/models/config.py | 33 ++++++++++++--------------- test_configuration_helper/models.py | 1 - 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/configuration_helper/models/config.py b/configuration_helper/models/config.py index 32668005c..10433a949 100644 --- a/configuration_helper/models/config.py +++ b/configuration_helper/models/config.py @@ -4,6 +4,7 @@ import re from odoo import api, fields, models +from inspect import getmembers class AbstractConfigSettings(models.AbstractModel): @@ -11,10 +12,6 @@ class AbstractConfigSettings(models.AbstractModel): _description = 'Abstract configuration settings' # prefix field name to differentiate fields in company with those in config _prefix = 'setting_' - # this is the class name to import in your module - # (it should be ResCompany or res_company, depends of your code) - _companyObject = None - _setup_extra_done = False company_id = fields.Many2one( 'res.company', @@ -32,22 +29,20 @@ class AbstractConfigSettings(models.AbstractModel): def _setup_base(self): cls = type(self) super(AbstractConfigSettings, self)._setup_base() - if not cls._companyObject: - return - if cls._setup_extra_done: - return - for field_key in cls._companyObject.__dict__.keys(): - field = cls._companyObject.__dict__[field_key] - if isinstance(field, fields.Field): - # allows to exclude some field - if self._filter_field(field_key): - # fields.agrs contains fields attributes - kwargs = field.args.copy() - kwargs['related'] = 'company_id.' + field_key - field_key = re.sub('^' + self._prefix, '', field_key) - self._add_field(field_key, field.new(**kwargs)) + + comp_fields = filter( + lambda f: (f[0].startswith(self._prefix) and + self._filter_field(f[0])), + getmembers(type(self.env['res.company']), + fields.Field.__instancecheck__) + ) + + for field_key, field in comp_fields: + kwargs = field.args.copy() + kwargs['related'] = 'company_id.' + field_key + field_key = re.sub('^' + self._prefix, '', field_key) + self._add_field(field_key, field.new(**kwargs)) cls._proper_fields = set(cls._fields) self._add_inherited_fields() cls.pool.model_cache[cls.__bases__] = cls - cls._setup_extra_done = True diff --git a/test_configuration_helper/models.py b/test_configuration_helper/models.py index 75b7b621d..bfffe2ba7 100644 --- a/test_configuration_helper/models.py +++ b/test_configuration_helper/models.py @@ -16,4 +16,3 @@ class MyConfigA(models.TransientModel): _name = 'res.config.settings' _inherit = ['res.config.settings', 'abstract.config.settings'] _prefix = 'prefix_a_' - _companyObject = ResCompany