|
|
@ -1,3 +1,8 @@ |
|
|
|
# Copyright 2020 Ivan Yelizariev <https://it-projects.info/team/yelizariev> |
|
|
|
# Copyright 2019 Kolushov Alexandr <https://it-projects.info/team/KolushovAlexandr> |
|
|
|
# License MIT (https://opensource.org/licenses/MIT). |
|
|
|
|
|
|
|
# pylint: disable=sql-injection |
|
|
|
from odoo import api, fields, models |
|
|
|
|
|
|
|
|
|
|
@ -5,27 +10,82 @@ class MailMessage(models.Model): |
|
|
|
_inherit = "mail.message" |
|
|
|
|
|
|
|
sent = fields.Boolean( |
|
|
|
"Sent", compute="_compute_sent", help="Was message sent to someone", store=True |
|
|
|
"Sent", |
|
|
|
compute="_compute_sent", |
|
|
|
help="Was message sent to someone", |
|
|
|
store=True, |
|
|
|
index=True, |
|
|
|
) |
|
|
|
|
|
|
|
@api.depends("author_id", "partner_ids", "channel_ids") |
|
|
|
@api.depends("author_id", "partner_ids", "channel_ids", "res_id", "model") |
|
|
|
def _compute_sent(self): |
|
|
|
for r in self: |
|
|
|
r_sudo = r.sudo() |
|
|
|
recipient_ids = r_sudo.partner_ids |
|
|
|
author_id = r_sudo.author_id |
|
|
|
res_id = ( |
|
|
|
r_sudo.model |
|
|
|
and r_sudo.res_id |
|
|
|
and r_sudo.env[r_sudo.model].browse(r_sudo.res_id) |
|
|
|
to_check = set(self.ids) |
|
|
|
to_write = set() |
|
|
|
|
|
|
|
if to_check: |
|
|
|
# len(recipient_ids) > 1 |
|
|
|
field = self._fields["partner_ids"] |
|
|
|
self.env.cr.execute( |
|
|
|
""" |
|
|
|
SELECT {message_field} |
|
|
|
FROM ( |
|
|
|
SELECT {message_field}, count(*) AS partner_count |
|
|
|
FROM {relation_table} |
|
|
|
WHERE {message_field} in %s |
|
|
|
GROUP BY {message_field} |
|
|
|
) AS tmp WHERE partner_count > 1""".format( |
|
|
|
message_field=field.column1, |
|
|
|
partner_field=field.column2, |
|
|
|
relation_table=field.relation, |
|
|
|
), |
|
|
|
(tuple(to_check),), |
|
|
|
) |
|
|
|
ids = {r[0] for r in self.env.cr.fetchall()} |
|
|
|
to_check -= ids |
|
|
|
to_write |= ids |
|
|
|
|
|
|
|
if to_check: |
|
|
|
# (len(recipient_ids) == 1 and recipient_ids[0].id != author_id.id) |
|
|
|
self.env.cr.execute( |
|
|
|
""" |
|
|
|
SELECT {message_field} |
|
|
|
FROM {relation_table} rel |
|
|
|
LEFT JOIN mail_message msg ON msg.id = rel.{message_field} |
|
|
|
WHERE rel.{partner_field} != msg.author_id AND rel.{message_field} in %s |
|
|
|
""".format( |
|
|
|
message_field=field.column1, |
|
|
|
partner_field=field.column2, |
|
|
|
relation_table=field.relation, |
|
|
|
), |
|
|
|
(tuple(to_check),), |
|
|
|
) |
|
|
|
sent = author_id and ( |
|
|
|
len(recipient_ids) > 1 |
|
|
|
or (len(recipient_ids) == 1 and recipient_ids[0].id != author_id.id) |
|
|
|
or (len(r_sudo.channel_ids)) |
|
|
|
or (res_id and len(res_id.message_partner_ids - author_id) > 0) |
|
|
|
ids = {r[0] for r in self.env.cr.fetchall()} |
|
|
|
to_check -= ids |
|
|
|
to_write |= ids |
|
|
|
|
|
|
|
if to_check: |
|
|
|
# (len(r_sudo.channel_ids)) |
|
|
|
field = self._fields["channel_ids"] |
|
|
|
self.env.cr.execute( |
|
|
|
""" |
|
|
|
SELECT {message_field} |
|
|
|
FROM ( |
|
|
|
SELECT {message_field}, count(*) AS channel_count |
|
|
|
FROM {relation_table} |
|
|
|
WHERE {message_field} in %s |
|
|
|
GROUP BY {message_field} |
|
|
|
) AS tmp WHERE channel_count > 0""".format( |
|
|
|
message_field=field.column1, |
|
|
|
channel_field=field.column2, |
|
|
|
relation_table=field.relation, |
|
|
|
), |
|
|
|
(tuple(to_check),), |
|
|
|
) |
|
|
|
r.sent = sent |
|
|
|
ids = {r[0] for r in self.env.cr.fetchall()} |
|
|
|
to_check -= ids |
|
|
|
to_write |= ids |
|
|
|
|
|
|
|
self.browse(to_write).update({"sent": True}) |
|
|
|
|
|
|
|
@api.multi |
|
|
|
def message_format(self): |
|
|
@ -41,4 +101,4 @@ class MailMessage(models.Model): |
|
|
|
class MailComposeMessage(models.TransientModel): |
|
|
|
|
|
|
|
_inherit = "mail.compose.message" |
|
|
|
sent = fields.Boolean("Sent", help="dummy field to fix inherit error") |
|
|
|
sent = fields.Boolean(help="dummy field to fix inherit error", store=False) |