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.

70 lines
2.5 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Author: Yannick Vaucher
  4. # Copyright 2015 Camptocamp SA
  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. import logging
  20. from openerp.osv import orm, fields
  21. _logger = logging.getLogger(__name__)
  22. class RecordArchiverConfigSettings(orm.TransientModel):
  23. _name = 'record.archiver.config.settings'
  24. _inherit = 'res.config.settings'
  25. _columns = {
  26. 'company_id': fields.many2one('res.company', 'Company', required=True),
  27. 'lifespan_ids': fields.related(
  28. 'company_id', 'lifespan_ids',
  29. string='Record Lifespans',
  30. type='one2many',
  31. relation='record.lifespan'),
  32. }
  33. def _default_company(self, cr, uid, context=None):
  34. user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
  35. return user.company_id.id
  36. _defaults = {
  37. 'company_id': _default_company,
  38. }
  39. def create(self, cr, uid, values, context=None):
  40. _super = super(RecordArchiverConfigSettings, self)
  41. rec_id = _super.create(cr, uid, values, context=context)
  42. # Hack: to avoid some nasty bug, related fields are not written upon
  43. # record creation.
  44. # Hence we write on those fields here.
  45. vals = {}
  46. for fname, field in self._columns.iteritems():
  47. if isinstance(field, fields.related) and fname in values:
  48. vals[fname] = values[fname]
  49. self.write(cr, uid, [rec_id], vals, context=context)
  50. return id
  51. def onchange_company_id(self, cr, uid, ids, company_id, context=None):
  52. # update related fields
  53. if not company_id:
  54. return {'value': {}}
  55. company = self.pool.get('res.company'
  56. ).browse(cr, uid, company_id, context=context)
  57. lifespan_ids = [l.id for l in company.lifespan_ids]
  58. values = {
  59. 'lifespan_ids': lifespan_ids,
  60. }
  61. return {'value': values}