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.

61 lines
2.3 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. 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. @api.model
  19. def play_onchanges(self, values, onchange_fields):
  20. # _onchange_spec() will return onchange fields from the default view
  21. # we need all fields in the dict even the empty ones
  22. # otherwise 'onchange()' will not apply changes to them
  23. onchange_specs = {
  24. field_name: '1' for field_name, field in self._fields.items()
  25. }
  26. all_values = values.copy()
  27. # If self is a record (play onchange on existing record)
  28. # we take the value of the field
  29. # If self is an empty record we will have an empty value
  30. if self:
  31. self.ensure_one()
  32. record_values = self._convert_to_write(
  33. {
  34. field_name: self[field_name]
  35. for field_name, field in self._fields.items()
  36. }
  37. )
  38. else:
  39. # We get default values, they may be used in onchange
  40. record_values = self.default_get(self._fields.keys())
  41. for field in self._fields:
  42. if field not in all_values:
  43. all_values[field] = record_values.get(field, False)
  44. new_values = {}
  45. for field in onchange_fields:
  46. onchange_values = self.onchange(all_values, field, onchange_specs)
  47. new_values.update(self._get_new_values(values, onchange_values))
  48. all_values.update(new_values)
  49. return {
  50. f: v
  51. for f, v in all_values.items()
  52. if not self._fields[f].compute
  53. and (f in values or f in new_values or f in onchange_fields)
  54. }