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.

118 lines
4.8 KiB

10 years ago
  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. company_cols = self._companyObject._columns
  38. for field_key in company_cols:
  39. # allows to exclude some field
  40. if self._filter_field(field_key):
  41. args = ('company_id', field_key)
  42. kwargs = {
  43. 'string': company_cols[field_key].string,
  44. 'help': company_cols[field_key].help,
  45. 'type': company_cols[field_key]._type,
  46. }
  47. if '_obj' in company_cols[field_key].__dict__:
  48. kwargs['relation'] = \
  49. company_cols[field_key]._obj
  50. if '_domain' in \
  51. company_cols[field_key].__dict__:
  52. kwargs['domain'] = \
  53. company_cols[field_key]._domain
  54. field_key = re.sub('^' + self._prefix, '', field_key)
  55. self._columns[field_key] = \
  56. fields.related(*args, **kwargs)
  57. _columns = {
  58. 'company_id': fields.many2one(
  59. 'res.company',
  60. 'Company',
  61. required=True),
  62. }
  63. def _default_company(self, cr, uid, context=None):
  64. user = self.pool['res.users'].browse(cr, uid, uid, context=context)
  65. return user.company_id.id
  66. _defaults = {
  67. 'company_id': _default_company,
  68. }
  69. def field_to_populate_as_related(self, cr, uid,
  70. field,
  71. company_cols,
  72. context=None):
  73. """Only fields which comes from company with the right prefix
  74. must be defined as related"""
  75. if self._prefix + field in company_cols:
  76. return True
  77. return False
  78. def onchange_company_id(self, cr, uid, ids, company_id, context=None):
  79. " update related fields "
  80. values = {}
  81. values['currency_id'] = False
  82. if not company_id:
  83. return {'value': values}
  84. company_m = self.pool['res.company']
  85. company = company_m.browse(
  86. cr, uid, company_id, context=context)
  87. company_cols = company_m._columns.keys()
  88. for field in self._columns:
  89. if self.field_to_populate_as_related(
  90. cr, uid, field, company_cols, context=context):
  91. cpny_field = self._columns[field].arg[-1]
  92. if self._columns[field]._type == 'many2one':
  93. values[field] = company[cpny_field]['id'] or False
  94. else:
  95. values[field] = company[cpny_field]
  96. return {'value': values}
  97. def create(self, cr, uid, values, context=None):
  98. id = super(AbstractConfigSettings, self).create(
  99. cr, uid, values, context=context)
  100. # Hack: to avoid some nasty bug, related fields are not written
  101. # upon record creation. Hence we write on those fields here.
  102. vals = {}
  103. for fname, field in self._columns.iteritems():
  104. if isinstance(field, fields.related) and fname in values:
  105. vals[fname] = values[fname]
  106. self.write(cr, uid, [id], vals, context)
  107. return id