Browse Source

Merge pull request #2 from p-tombez/11.0-mig_configuration_helper

[MIG] Update module to match V11 architecture
pull/1261/head
Akim Juillerat 6 years ago
committed by GitHub
parent
commit
5e4973400c
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 25
      configuration_helper/models/config.py
  2. 1
      test_configuration_helper/models.py

25
configuration_helper/models/config.py

@ -4,6 +4,7 @@
import re import re
from odoo import api, fields, models from odoo import api, fields, models
from inspect import getmembers
class AbstractConfigSettings(models.AbstractModel): class AbstractConfigSettings(models.AbstractModel):
@ -11,10 +12,6 @@ class AbstractConfigSettings(models.AbstractModel):
_description = 'Abstract configuration settings' _description = 'Abstract configuration settings'
# prefix field name to differentiate fields in company with those in config # prefix field name to differentiate fields in company with those in config
_prefix = 'setting_' _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( company_id = fields.Many2one(
'res.company', 'res.company',
@ -32,16 +29,15 @@ class AbstractConfigSettings(models.AbstractModel):
def _setup_base(self): def _setup_base(self):
cls = type(self) cls = type(self)
super(AbstractConfigSettings, self)._setup_base() 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
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 = field.args.copy()
kwargs['related'] = 'company_id.' + field_key kwargs['related'] = 'company_id.' + field_key
field_key = re.sub('^' + self._prefix, '', field_key) field_key = re.sub('^' + self._prefix, '', field_key)
@ -50,4 +46,3 @@ class AbstractConfigSettings(models.AbstractModel):
self._add_inherited_fields() self._add_inherited_fields()
cls.pool.model_cache[cls.__bases__] = cls cls.pool.model_cache[cls.__bases__] = cls
cls._setup_extra_done = True

1
test_configuration_helper/models.py

@ -16,4 +16,3 @@ class MyConfigA(models.TransientModel):
_name = 'res.config.settings' _name = 'res.config.settings'
_inherit = ['res.config.settings', 'abstract.config.settings'] _inherit = ['res.config.settings', 'abstract.config.settings']
_prefix = 'prefix_a_' _prefix = 'prefix_a_'
_companyObject = ResCompany
Loading…
Cancel
Save