Browse Source

🚑 mail_sent: fix installation problems

fixes #290
pull/294/head
Ivan Yelizariev 5 years ago
parent
commit
c749bd166f
No known key found for this signature in database GPG Key ID: C034D49D267D42C0
  1. 4
      mail_sent/__manifest__.py
  2. 6
      mail_sent/doc/changelog.rst
  3. 94
      mail_sent/models.py

4
mail_sent/__manifest__.py

@ -1,4 +1,4 @@
# Copyright 2016 Ivan Yelizariev <https://it-projects.info/team/yelizariev>
# Copyright 2016,2020 Ivan Yelizariev <https://it-projects.info/team/yelizariev>
# Copyright 2019 Kolushov Alexandr <https://it-projects.info/team/KolushovAlexandr> # Copyright 2019 Kolushov Alexandr <https://it-projects.info/team/KolushovAlexandr>
# License MIT (https://opensource.org/licenses/MIT). # License MIT (https://opensource.org/licenses/MIT).
{ {
@ -6,7 +6,7 @@
"summary": """Quick way to find sent messages""", "summary": """Quick way to find sent messages""",
"category": "Discuss", "category": "Discuss",
"images": ["images/menu.png"], "images": ["images/menu.png"],
"version": "12.0.1.2.0",
"version": "12.0.1.3.0",
"author": "IT-Projects LLC, Ivan Yelizariev, Pavel Romanchenko", "author": "IT-Projects LLC, Ivan Yelizariev, Pavel Romanchenko",
"support": "apps@itpp.dev", "support": "apps@itpp.dev",
"website": "https://it-projects.info", "website": "https://it-projects.info",

6
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` `1.2.0`
------- -------

94
mail_sent/models.py

@ -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 from odoo import api, fields, models
@ -5,27 +10,82 @@ class MailMessage(models.Model):
_inherit = "mail.message" _inherit = "mail.message"
sent = fields.Boolean( 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): 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 @api.multi
def message_format(self): def message_format(self):
@ -41,4 +101,4 @@ class MailMessage(models.Model):
class MailComposeMessage(models.TransientModel): class MailComposeMessage(models.TransientModel):
_inherit = "mail.compose.message" _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)
Loading…
Cancel
Save