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.

114 lines
4.8 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Author: David BEAL, Copyright 2014 Akretion
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as
  8. # published by the Free Software Foundation, either version 3 of the
  9. # License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. ##############################################################################
  20. import re
  21. from openerp.osv import orm, fields
  22. class AbstractConfigSettings(orm.AbstractModel):
  23. _name = 'abstract.config.settings'
  24. _description = 'Abstract configuration settings'
  25. # prefix field name to differentiate fields in company with those in config
  26. _prefix = 'setting_'
  27. # this is the class name to import in your module
  28. # (it should be ResCompany or res_company, depends of your code)
  29. _companyObject = None
  30. def _filter_field(self, field_key):
  31. """Inherit in your module to define for which company field
  32. you don't want have a matching related field"""
  33. return True
  34. def __init__(self, pool, cr):
  35. super(AbstractConfigSettings, self).__init__(pool, cr)
  36. if self._companyObject:
  37. for field_key in self._companyObject._columns:
  38. #allows to exclude some field
  39. if self._filter_field(field_key):
  40. args = ('company_id', field_key)
  41. kwargs = {
  42. 'string': self._companyObject._columns[field_key].string,
  43. 'help': self._companyObject._columns[field_key].help,
  44. 'type': self._companyObject._columns[field_key]._type,
  45. }
  46. if '_obj' in self._companyObject._columns[field_key].__dict__.keys():
  47. kwargs['relation'] = \
  48. self._companyObject._columns[field_key]._obj
  49. if '_domain' in \
  50. self._companyObject._columns[field_key].__dict__.keys():
  51. kwargs['domain'] = \
  52. self._companyObject._columns[field_key]._domain
  53. field_key = re.sub('^' + self._prefix, '', field_key)
  54. self._columns[field_key] = \
  55. fields.related(*args, **kwargs)
  56. _columns = {
  57. 'company_id': fields.many2one(
  58. 'res.company',
  59. 'Company',
  60. required=True),
  61. }
  62. def _default_company(self, cr, uid, context=None):
  63. user = self.pool['res.users'].browse(cr, uid, uid, context=context)
  64. return user.company_id.id
  65. _defaults = {
  66. 'company_id': _default_company,
  67. }
  68. def field_to_populate_as_related(self, cr, uid, field, company_cols, context=None):
  69. """Only fields which comes from company with the right prefix
  70. must be defined as related"""
  71. if self._prefix + field in company_cols:
  72. return True
  73. return False
  74. def onchange_company_id(self, cr, uid, ids, company_id, context=None):
  75. " update related fields "
  76. values = {}
  77. values['currency_id'] = False
  78. if not company_id:
  79. return {'value': values}
  80. company_m = self.pool['res.company']
  81. company = company_m.browse(
  82. cr, uid, company_id, context=context)
  83. company_cols = company_m._columns.keys()
  84. for field in self._columns:
  85. if self.field_to_populate_as_related(
  86. cr, uid, field, company_cols, context=context):
  87. cpny_field = self._columns[field].arg[-1]
  88. if self._columns[field]._type == 'many2one':
  89. values[field] = company[cpny_field]['id'] or False
  90. else:
  91. values[field] = company[cpny_field]
  92. return {'value': values}
  93. def create(self, cr, uid, values, context=None):
  94. id = super(AbstractConfigSettings, self).create(
  95. cr, uid, values, context=context)
  96. # Hack: to avoid some nasty bug, related fields are not written
  97. # upon record creation. Hence we write on those fields here.
  98. vals = {}
  99. for fname, field in self._columns.iteritems():
  100. if isinstance(field, fields.related) and fname in values:
  101. vals[fname] = values[fname]
  102. self.write(cr, uid, [id], vals, context)
  103. return id