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.4 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2019 ACSONE SA/NV
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. from odoo import api, fields, models
  5. class TestOnchangeHelperDiscussion(models.Model):
  6. _name = "test_onchange_helper.discussion"
  7. _description = "Test Onchange Helper Discussion"
  8. name = fields.Char(
  9. string="Title",
  10. required=True,
  11. help="General description of what this discussion is about.",
  12. )
  13. moderator = fields.Many2one("res.users")
  14. categories = fields.Many2many(
  15. "test_onchange_helper.category",
  16. "test_onchange_helper_discussion_category",
  17. "discussion",
  18. "category",
  19. )
  20. participants = fields.Many2many("res.users")
  21. messages = fields.One2many("test_onchange_helper.message", "discussion")
  22. message_concat = fields.Text(string="Message concatenate")
  23. important_messages = fields.One2many(
  24. "test_onchange_helper.message",
  25. "discussion",
  26. domain=[("important", "=", True)],
  27. )
  28. very_important_messages = fields.One2many(
  29. "test_onchange_helper.message",
  30. "discussion",
  31. domain=lambda self: self._domain_very_important(),
  32. )
  33. emails = fields.One2many("test_onchange_helper.emailmessage", "discussion")
  34. important_emails = fields.One2many(
  35. "test_onchange_helper.emailmessage",
  36. "discussion",
  37. domain=[("important", "=", True)],
  38. )
  39. def _domain_very_important(self):
  40. """Ensure computed O2M domains work as expected."""
  41. return [("important", "=", True)]
  42. @api.onchange("name")
  43. def _onchange_name(self):
  44. # test onchange modifying one2many field values
  45. # update body of existings messages and emails
  46. for message in self.messages:
  47. message.body = "not last dummy message"
  48. for message in self.important_messages:
  49. message.body = "not last dummy message"
  50. # add new dummy message
  51. message_vals = self.messages._add_missing_default_values(
  52. {"body": "dummy message", "important": True}
  53. )
  54. self.messages |= self.messages.new(message_vals)
  55. self.important_messages |= self.messages.new(message_vals)
  56. @api.onchange("moderator")
  57. def _onchange_moderator(self):
  58. self.participants |= self.moderator
  59. @api.onchange("messages")
  60. def _onchange_messages(self):
  61. self.message_concat = "\n".join(
  62. ["%s:%s" % (m.name, m.body) for m in self.messages]
  63. )