diff --git a/mail_sent/__manifest__.py b/mail_sent/__manifest__.py index 56279de..85e17a9 100644 --- a/mail_sent/__manifest__.py +++ b/mail_sent/__manifest__.py @@ -1,4 +1,4 @@ -# Copyright 2016 Ivan Yelizariev +# Copyright 2016,2020 Ivan Yelizariev # Copyright 2019 Kolushov Alexandr # License MIT (https://opensource.org/licenses/MIT). { @@ -6,7 +6,7 @@ "summary": """Quick way to find sent messages""", "category": "Discuss", "images": ["images/menu.png"], - "version": "12.0.1.2.0", + "version": "12.0.1.3.0", "author": "IT-Projects LLC, Ivan Yelizariev, Pavel Romanchenko", "support": "apps@itpp.dev", "website": "https://it-projects.info", diff --git a/mail_sent/doc/changelog.rst b/mail_sent/doc/changelog.rst index a413f7c..ebe66f1 100644 --- a/mail_sent/doc/changelog.rst +++ b/mail_sent/doc/changelog.rst @@ -1,3 +1,9 @@ +`1.3.0` +------- +- **Imp:** optimize database queries +- **Fix:** some messages were incorrectly marked as "Sent" +- **Fix:** error on sending message via Composer and occasional related errors on installation + `1.2.0` ------- diff --git a/mail_sent/models.py b/mail_sent/models.py index 70a942b..ca238f9 100644 --- a/mail_sent/models.py +++ b/mail_sent/models.py @@ -1,3 +1,8 @@ +# Copyright 2020 Ivan Yelizariev +# Copyright 2019 Kolushov Alexandr +# 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)