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. # -*- coding: utf-8 -*-
  2. # © 2014 David BEAL Akretion
  3. # © 2016 Yannick Vaucher (Camptocamp SA)
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  5. import re
  6. from openerp import api, fields, models
  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. # this is the class name to import in your module
  13. # (it should be ResCompany or res_company, depends of your code)
  14. _companyObject = None
  15. _setup_extra_done = False
  16. company_id = fields.Many2one(
  17. 'res.company',
  18. 'Company',
  19. required=True,
  20. default=lambda self: self.env.user.company_id
  21. )
  22. def _filter_field(self, field_key):
  23. """Inherit in your module to define for which company field
  24. you don't want have a matching related field"""
  25. return True
  26. @api.model
  27. def _setup_base(self, partial):
  28. cls = type(self)
  29. super(AbstractConfigSettings, self)._setup_base(partial)
  30. if not self._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