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