You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.6 KiB

  1. # © 2014 David BEAL Akretion
  2. # © 2016 Yannick Vaucher (Camptocamp SA)
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. import re
  5. from odoo import api, fields, models
  6. from inspect import getmembers
  7. class AbstractConfigSettings(models.AbstractModel):
  8. _name = 'abstract.config.settings'
  9. _description = 'Abstract configuration settings'
  10. # prefix field name to differentiate fields in company with those in config
  11. _prefix = 'setting_'
  12. company_id = fields.Many2one(
  13. 'res.company',
  14. 'Company',
  15. required=True,
  16. default=lambda self: self.env.user.company_id
  17. )
  18. def _filter_field(self, field_key):
  19. """Inherit in your module to define for which company field
  20. you don't want have a matching related field"""
  21. return True
  22. @api.model
  23. def _setup_base(self):
  24. cls = type(self)
  25. super()._setup_base()
  26. comp_fields = filter(
  27. lambda f: (f[0].startswith(self._prefix) and
  28. self._filter_field(f[0])),
  29. getmembers(type(self.env['res.company']),
  30. fields.Field.__instancecheck__)
  31. )
  32. for field_key, field in comp_fields:
  33. kwargs = field.args.copy()
  34. kwargs['related'] = 'company_id.' + field_key
  35. kwargs['readonly'] = False
  36. field_key = re.sub('^' + self._prefix, '', field_key)
  37. self._add_field(field_key, field.new(**kwargs))
  38. cls._proper_fields = set(cls._fields)
  39. self._add_inherited_fields()
  40. cls.pool.model_cache[cls.__bases__] = cls