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.

49 lines
1.9 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  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. class Base(models.AbstractModel):
  6. _inherit = 'base'
  7. @api.model
  8. def _get_new_values(self, record, on_change_result):
  9. vals = on_change_result.get('value', {})
  10. new_values = {}
  11. for fieldname, value in vals.items():
  12. if fieldname not in record:
  13. column = self._fields[fieldname]
  14. if value and column.type == 'many2one':
  15. value = value[0] # many2one are tuple (id, name)
  16. new_values[fieldname] = value
  17. return new_values
  18. def play_onchanges(self, values, onchange_fields):
  19. onchange_specs = self._onchange_spec()
  20. # we need all fields in the dict even the empty ones
  21. # otherwise 'onchange()' will not apply changes to them
  22. all_values = values.copy()
  23. # If self is a record (play onchange on existing record)
  24. # we take the value of the field
  25. # If self is an empty record we will have an empty value
  26. if self:
  27. self.ensure_one()
  28. record_values = self._convert_to_write(self.read()[0])
  29. else:
  30. record_values = {}
  31. for field in self._fields:
  32. if field not in all_values:
  33. all_values[field] = record_values.get(field, False)
  34. new_values = {}
  35. for field in onchange_fields:
  36. onchange_values = self.onchange(all_values, field, onchange_specs)
  37. new_values.update(self._get_new_values(values, onchange_values))
  38. all_values.update(new_values)
  39. return {
  40. f: v for f, v in all_values.items()
  41. if not self._fields[f].compute
  42. and (f in values or f in new_values)}