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.

104 lines
3.5 KiB

9 years ago
  1. # Copyright 2020 Ivan Yelizariev <https://it-projects.info/team/yelizariev>
  2. # Copyright 2019 Kolushov Alexandr <https://it-projects.info/team/KolushovAlexandr>
  3. # License MIT (https://opensource.org/licenses/MIT).
  4. # pylint: disable=sql-injection
  5. from odoo import api, fields, models
  6. class MailMessage(models.Model):
  7. _inherit = "mail.message"
  8. sent = fields.Boolean(
  9. "Sent",
  10. compute="_compute_sent",
  11. help="Was message sent to someone",
  12. store=True,
  13. index=True,
  14. )
  15. @api.depends("author_id", "partner_ids", "channel_ids", "res_id", "model")
  16. def _compute_sent(self):
  17. to_check = set(self.ids)
  18. to_write = set()
  19. if to_check:
  20. # len(recipient_ids) > 1
  21. field = self._fields["partner_ids"]
  22. self.env.cr.execute(
  23. """
  24. SELECT {message_field}
  25. FROM (
  26. SELECT {message_field}, count(*) AS partner_count
  27. FROM {relation_table}
  28. WHERE {message_field} in %s
  29. GROUP BY {message_field}
  30. ) AS tmp WHERE partner_count > 1""".format(
  31. message_field=field.column1,
  32. partner_field=field.column2,
  33. relation_table=field.relation,
  34. ),
  35. (tuple(to_check),),
  36. )
  37. ids = {r[0] for r in self.env.cr.fetchall()}
  38. to_check -= ids
  39. to_write |= ids
  40. if to_check:
  41. # (len(recipient_ids) == 1 and recipient_ids[0].id != author_id.id)
  42. self.env.cr.execute(
  43. """
  44. SELECT {message_field}
  45. FROM {relation_table} rel
  46. LEFT JOIN mail_message msg ON msg.id = rel.{message_field}
  47. WHERE rel.{partner_field} != msg.author_id AND rel.{message_field} in %s
  48. """.format(
  49. message_field=field.column1,
  50. partner_field=field.column2,
  51. relation_table=field.relation,
  52. ),
  53. (tuple(to_check),),
  54. )
  55. ids = {r[0] for r in self.env.cr.fetchall()}
  56. to_check -= ids
  57. to_write |= ids
  58. if to_check:
  59. # (len(r_sudo.channel_ids))
  60. field = self._fields["channel_ids"]
  61. self.env.cr.execute(
  62. """
  63. SELECT {message_field}
  64. FROM (
  65. SELECT {message_field}, count(*) AS channel_count
  66. FROM {relation_table}
  67. WHERE {message_field} in %s
  68. GROUP BY {message_field}
  69. ) AS tmp WHERE channel_count > 0""".format(
  70. message_field=field.column1,
  71. channel_field=field.column2,
  72. relation_table=field.relation,
  73. ),
  74. (tuple(to_check),),
  75. )
  76. ids = {r[0] for r in self.env.cr.fetchall()}
  77. to_check -= ids
  78. to_write |= ids
  79. self.browse(to_write).update({"sent": True})
  80. @api.multi
  81. def message_format(self):
  82. message_values = super(MailMessage, self).message_format()
  83. message_index = {message["id"]: message for message in message_values}
  84. for item in self:
  85. msg = message_index.get(item.id)
  86. if msg:
  87. msg["sent"] = item.sent
  88. return message_values
  89. class MailComposeMessage(models.TransientModel):
  90. _inherit = "mail.compose.message"
  91. sent = fields.Boolean(help="dummy field to fix inherit error", store=False)