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.

52 lines
1.7 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2016-2017 Akretion (http://www.akretion.com)
  3. # © 2016-2017 Camptocamp (http://www.camptocamp.com/)
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  5. from odoo import api, models
  6. def get_new_values(model, record, on_change_result):
  7. vals = on_change_result.get('value', {})
  8. new_values = {}
  9. for fieldname, value in vals.iteritems():
  10. if fieldname not in record:
  11. column = model._fields[fieldname]
  12. if value and column.type == 'many2one':
  13. value = value[0] # many2one are tuple (id, name)
  14. new_values[fieldname] = value
  15. return new_values
  16. @api.model
  17. def play_onchanges(self, values, onchange_fields):
  18. onchange_specs = self._onchange_spec()
  19. # we need all fields in the dict even the empty ones
  20. # otherwise 'onchange()' will not apply changes to them
  21. all_values = values.copy()
  22. for field in self._fields:
  23. if field not in all_values:
  24. all_values[field] = False
  25. # we work on a temporary record
  26. new_record = self.new(all_values)
  27. new_values = {}
  28. for field in onchange_fields:
  29. onchange_values = new_record.onchange(all_values,
  30. field, onchange_specs)
  31. new_values.update(get_new_values(self, values, onchange_values))
  32. all_values.update(new_values)
  33. res = {f: v for f, v in all_values.iteritems()
  34. if f in values or f in new_values}
  35. return res
  36. class IrRule(models.Model):
  37. _inherit = 'ir.rule'
  38. def _setup_complete(self):
  39. if not hasattr(models.BaseModel, 'play_onchanges'):
  40. setattr(models.BaseModel, 'play_onchanges', play_onchanges)
  41. return super(IrRule, self)._setup_complete()