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.

72 lines
2.8 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. """
  21. :param values: dict of input value that
  22. :param onchange_fields: fields for which onchange methods will be
  23. played
  24. Order in onchange_fields is very important as onchanges methods will
  25. be played in that order.
  26. :return: changed values
  27. """
  28. # _onchange_spec() will return onchange fields from the default view
  29. # we need all fields in the dict even the empty ones
  30. # otherwise 'onchange()' will not apply changes to them
  31. onchange_specs = {
  32. field_name: '1' for field_name, field in self._fields.items()
  33. }
  34. all_values = values.copy()
  35. # If self is a record (play onchange on existing record)
  36. # we take the value of the field
  37. # If self is an empty record we will have an empty value
  38. if self:
  39. self.ensure_one()
  40. record_values = self._convert_to_write(
  41. {
  42. field_name: self[field_name]
  43. for field_name, field in self._fields.items()
  44. }
  45. )
  46. else:
  47. # We get default values, they may be used in onchange
  48. record_values = self.default_get(self._fields.keys())
  49. for fn, field in self._fields.items():
  50. if not field.related and fn not in all_values: # related fields are False
  51. all_values[fn] = record_values.get(fn, False)
  52. new_values = {}
  53. with self.env.do_in_onchange(): # avoid cache issues with related fields!
  54. new = self.new(all_values)
  55. for field in onchange_fields:
  56. onchange_values = new.onchange(all_values, field, onchange_specs)
  57. new_values.update(self._get_new_values(values, onchange_values))
  58. all_values.update(new_values)
  59. return {
  60. f: v
  61. for f, v in all_values.items()
  62. if not (self._fields[f].compute and not self._fields[f].inverse)
  63. and (f in values or f in new_values or f in onchange_fields)
  64. }