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.

120 lines
3.9 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 x620 <https://github.com/x620>
  3. # Copyright 2016 manawi <https://github.com/manawi>
  4. # Copyright 2017 Artyom Losev <https://github.com/ArtyomLosev>
  5. # Copyright 2019 Artem Rafailov <https://it-projects.info/team/Ommo73/>
  6. # License LGPL-3.0 (https://www.gnu.org/licenses/lgpl.html).
  7. from odoo import api, fields, models
  8. class MailComposeMessage(models.TransientModel):
  9. _inherit = "mail.compose.message"
  10. is_private = fields.Boolean(string="Send Internal Message")
  11. def get_internal_users_ids(self):
  12. internal_users_ids = self.env["res.users"].search([("share", "=", False)]).ids
  13. return internal_users_ids
  14. @api.multi
  15. def send_mail(self, auto_commit=False):
  16. for w in self:
  17. w.is_log = True if w.is_private else w.is_log
  18. super(MailComposeMessage, self).send_mail(auto_commit=False)
  19. class MailMessage(models.Model):
  20. _inherit = "mail.message"
  21. @api.multi
  22. def _notify(self, force_send=False, send_after_commit=True, user_signature=True):
  23. self_sudo = self.sudo()
  24. if (
  25. "is_private" not in self_sudo._context
  26. or not self_sudo._context["is_private"]
  27. ):
  28. super(MailMessage, self)._notify(
  29. force_send, send_after_commit, user_signature
  30. )
  31. else:
  32. self._notify_mail_private(force_send, send_after_commit, user_signature)
  33. @api.multi
  34. def _notify_mail_private(
  35. self, force_send=False, send_after_commit=True, user_signature=True
  36. ):
  37. """ The method was partially copied from Odoo.
  38. In the current method, the way of getting channels for a private message is changed.
  39. """
  40. # have a sudoed copy to manipulate partners (public can go here with
  41. # website modules like forum / blog / ...
  42. # TDE CHECK: add partners / channels as arguments to be able to notify a message with / without computation ??
  43. self.ensure_one() # tde: not sure, just for testinh, will see
  44. partners = self.env["res.partner"] | self.partner_ids
  45. channels = self.env["mail.channel"] | self.channel_ids
  46. # update message, with maybe custom values
  47. message_values = {
  48. "channel_ids": [(6, 0, channels.ids)],
  49. "needaction_partner_ids": [(6, 0, partners.ids)],
  50. }
  51. if (
  52. self.model
  53. and self.res_id
  54. and hasattr(self.env[self.model], "message_get_message_notify_values")
  55. ):
  56. message_values.update(
  57. self.env[self.model]
  58. .browse(self.res_id)
  59. .message_get_message_notify_values(self, message_values)
  60. )
  61. self.write(message_values)
  62. # notify partners and channels
  63. partners._notify(
  64. self,
  65. force_send=force_send,
  66. send_after_commit=send_after_commit,
  67. user_signature=user_signature,
  68. )
  69. channels._notify(self)
  70. # Discard cache, because child / parent allow reading and therefore
  71. # change access rights.
  72. if self.parent_id:
  73. self.parent_id.invalidate_cache()
  74. return True
  75. class MailThread(models.AbstractModel):
  76. _inherit = "mail.thread"
  77. @api.multi
  78. @api.returns("self", lambda value: value.id)
  79. def message_post(
  80. self,
  81. body="",
  82. subject=None,
  83. message_type="notification",
  84. subtype=None,
  85. parent_id=False,
  86. attachments=None,
  87. content_subtype="html",
  88. **kwargs
  89. ):
  90. if "channel_ids" in kwargs:
  91. kwargs["channel_ids"] = [(4, pid) for pid in kwargs["channel_ids"]]
  92. return super(MailThread, self).message_post(
  93. body,
  94. subject,
  95. message_type,
  96. subtype,
  97. parent_id,
  98. attachments,
  99. content_subtype,
  100. **kwargs
  101. )