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.

51 lines
1.7 KiB

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