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.

54 lines
1.9 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. class AbstractConfigSettings(models.AbstractModel):
  7. _name = 'abstract.config.settings'
  8. _description = 'Abstract configuration settings'
  9. # prefix field name to differentiate fields in company with those in config
  10. _prefix = 'setting_'
  11. # this is the class name to import in your module
  12. # (it should be ResCompany or res_company, depends of your code)
  13. _companyObject = None
  14. _setup_extra_done = False
  15. company_id = fields.Many2one(
  16. 'res.company',
  17. 'Company',
  18. required=True,
  19. default=lambda self: self.env.user.company_id
  20. )
  21. def _filter_field(self, field_key):
  22. """Inherit in your module to define for which company field
  23. you don't want have a matching related field"""
  24. return True
  25. @api.model
  26. def _setup_base(self):
  27. import pdb; pdb.set_trace()
  28. cls = type(self)
  29. super(AbstractConfigSettings, self)._setup_base()
  30. if not cls._companyObject:
  31. return
  32. if cls._setup_extra_done:
  33. return
  34. for field_key in cls._companyObject.__dict__.keys():
  35. field = cls._companyObject.__dict__[field_key]
  36. if isinstance(field, fields.Field):
  37. # allows to exclude some field
  38. if self._filter_field(field_key):
  39. # fields.agrs contains fields attributes
  40. kwargs = field.args.copy()
  41. kwargs['related'] = 'company_id.' + field_key
  42. field_key = re.sub('^' + self._prefix, '', field_key)
  43. self._add_field(field_key, field.new(**kwargs))
  44. cls._proper_fields = set(cls._fields)
  45. self._add_inherited_fields()
  46. cls.pool.model_cache[cls.__bases__] = cls
  47. cls._setup_extra_done = True