Skip to content

Commit 02aab63

Browse files
committed
[MIG] mail_post_defer: Migration to 19.0
1 parent 0f4be40 commit 02aab63

File tree

11 files changed

+115
-138
lines changed

11 files changed

+115
-138
lines changed

mail_post_defer/README.rst

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ Deferred Message Posting
2121
:target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html
2222
:alt: License: LGPL-3
2323
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fmail-lightgray.png?logo=github
24-
:target: https://github.com/OCA/mail/tree/18.0/mail_post_defer
24+
:target: https://github.com/OCA/mail/tree/19.0/mail_post_defer
2525
:alt: OCA/mail
2626
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
27-
:target: https://translation.odoo-community.org/projects/mail-18-0/mail-18-0-mail_post_defer
27+
:target: https://translation.odoo-community.org/projects/mail-19-0/mail-19-0-mail_post_defer
2828
:alt: Translate me on Weblate
2929
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
30-
:target: https://runboat.odoo-community.org/builds?repo=OCA/mail&target_branch=18.0
30+
:target: https://runboat.odoo-community.org/builds?repo=OCA/mail&target_branch=19.0
3131
:alt: Try me on Runboat
3232

3333
|badge1| |badge2| |badge3| |badge4| |badge5|
@@ -61,21 +61,26 @@ appropriately out of the box. Just make sure the following scheduled
6161
actions are active:
6262

6363
- Mail: Email Queue Manager (mail.ir_cron_mail_scheduler_action)
64-
- Notification: Send scheduled message notifications
64+
- Notification: Notify scheduled messages
6565
(mail.ir_cron_send_scheduled_message)
6666

67-
The mail queue processing is made by a cron job. This is normal Odoo
68-
behavior, not specific to this module. However, since you will start
69-
using that queue for every message posted by any user in any thread,
70-
this module configures that job to execute every minute by default.
67+
The mail queue processing and scheduled messages notifications are
68+
handled by cron jobs. This is normal Odoo behavior, not specific to this
69+
module. However, since you will start using that queue for every message
70+
posted by any user in any thread, both jobs are configured to execute
71+
every minute by default.
7172

7273
You can still change that cadence after installing the module (although
7374
it is not recommended). To do so:
7475

7576
1. Log in with an administrator user.
7677
2. Activate developer mode.
7778
3. Go to *Settings > Technical > Automation > Scheduled Actions*.
78-
4. Edit the action named "Mail: Email Queue Manager".
79+
4. Find the cron(s) you want to adjust:
80+
81+
- Mail: Email Queue Manager - handles outgoing emails.
82+
- Notification: Notify scheduled messages - handles notifications.
83+
7984
5. Lower down the frequency in the field *Execute Every*. Recommended: 1
8085
minute.
8186

@@ -112,7 +117,7 @@ Bug Tracker
112117
Bugs are tracked on `GitHub Issues <https://github.com/OCA/mail/issues>`_.
113118
In case of trouble, please check there if your issue has already been reported.
114119
If you spotted it first, help us to smash it by providing a detailed and welcomed
115-
`feedback <https://github.com/OCA/mail/issues/new?body=module:%20mail_post_defer%0Aversion:%2018.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
120+
`feedback <https://github.com/OCA/mail/issues/new?body=module:%20mail_post_defer%0Aversion:%2019.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
116121

117122
Do not contact contributors directly about support or help with technical issues.
118123

@@ -123,11 +128,15 @@ Authors
123128
-------
124129

125130
* Moduon
131+
* OERP Canada
126132

127133
Contributors
128134
------------
129135

130136
- Jairo Llopis (https://www.moduon.team/)
137+
- `OERP Canada <https://www.oerp.ca>`__
138+
139+
- Yash Pathak
131140

132141
Maintainers
133142
-----------
@@ -150,6 +159,6 @@ Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:
150159

151160
|maintainer-Yajo|
152161

153-
This module is part of the `OCA/mail <https://github.com/OCA/mail/tree/18.0/mail_post_defer>`_ project on GitHub.
162+
This module is part of the `OCA/mail <https://github.com/OCA/mail/tree/19.0/mail_post_defer>`_ project on GitHub.
154163

155164
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

mail_post_defer/__manifest__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
{
44
"name": "Deferred Message Posting",
55
"summary": "Faster and cancellable outgoing messages",
6-
"version": "18.0.1.0.0",
6+
"version": "19.0.1.0.0",
77
"development_status": "Alpha",
88
"category": "Productivity/Discuss",
99
"website": "https://github.com/OCA/mail",
10-
"author": "Moduon, Odoo Community Association (OCA)",
10+
"author": "Moduon, OERP Canada, Odoo Community Association (OCA)",
1111
"maintainers": ["Yajo"],
1212
"license": "LGPL-3",
1313
"depends": [

mail_post_defer/hooks.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,23 @@
66

77

88
def post_init_hook(env):
9-
"""Increase cadence of mail queue cron."""
10-
try:
11-
cron = env.ref("mail.ir_cron_mail_scheduler_action")
12-
except ValueError:
13-
_logger.warning(
14-
"Couldn't find the standard mail scheduler cron. "
15-
"Maybe no mails will be ever sent!"
16-
)
17-
else:
18-
_logger.info("Setting mail queue cron cadence to 1 minute")
19-
cron.interval_number = 1
20-
cron.interval_type = "minutes"
9+
"""Increase cadence of mail queue and notification crons."""
10+
crons = {
11+
"mail.ir_cron_mail_scheduler_action": "Mail: Email Queue Manager",
12+
"mail.ir_cron_send_scheduled_message": "Notification: Notify scheduled messages", # noqa: E501
13+
}
14+
15+
for cron_ref, description in crons.items():
16+
try:
17+
cron = env.ref(cron_ref)
18+
except ValueError:
19+
_logger.warning(
20+
"Couldn't find the %s cron (%s). "
21+
"Maybe no mails/notification will be ever sent!.",
22+
description,
23+
cron_ref,
24+
)
25+
else:
26+
_logger.info("Setting %s cron cadence to 1 minute", description)
27+
cron.interval_number = 1
28+
cron.interval_type = "minutes"

mail_post_defer/models/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
from . import mail_message
21
from . import mail_message_schedule
32
from . import mail_thread

mail_post_defer/models/mail_message.py

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
# Copyright 2022-2024 Moduon Team S.L. <info@moduon.team>
22
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl).
33

4-
from datetime import datetime, timedelta
4+
from datetime import timedelta
55

6-
from odoo import _, fields, models
7-
from odoo.exceptions import UserError
6+
from odoo import fields, models
87

98

109
class MailThread(models.AbstractModel):
@@ -27,41 +26,45 @@ def _notify_thread(self, message, msg_vals=False, **kwargs):
2726
if defer_seconds:
2827
kwargs.setdefault(
2928
"scheduled_date",
30-
datetime.utcnow() + timedelta(seconds=defer_seconds),
29+
fields.Datetime.now() + timedelta(seconds=defer_seconds),
3130
)
3231
return super(MailThread, _self)._notify_thread(
3332
message, msg_vals=msg_vals, **kwargs
3433
)
3534

36-
def _check_can_update_message_content(self, messages):
37-
"""Allow updating unsent messages.
38-
39-
Upstream Odoo only allows updating notes. We want to be able to update
40-
any message that is not sent yet. When a message is scheduled,
41-
notifications and mails will still not exist. Another possibility is
42-
that they exist but are not sent yet. In those cases, we are still on
43-
time to update it.
44-
"""
45-
# Check that no notification or mail has been sent yet
46-
if any(ntf.notification_status == "sent" for ntf in messages.notification_ids):
47-
raise UserError(
48-
_("Cannot modify message; notifications were already sent.")
49-
) from None
50-
if any(mail.state in {"sent", "received"} for mail in messages.mail_ids):
51-
raise UserError(
52-
_("Cannot modify message; notifications were already sent.")
53-
) from None
54-
55-
# For unsent messages, we're more permissive than the original implementation
56-
# Only check the basic restrictions that make sense
57-
if any(message.message_type != "comment" for message in messages):
58-
raise UserError(
59-
_("Only messages type comment can have their content updated")
35+
def _message_update_content(self, message, /, *, body, **kwargs):
36+
# If anything already went out, fall back to the standard behavior
37+
if any(ntf.notification_status == "sent" for ntf in message.notification_ids):
38+
return super()._message_update_content(message, body=body, **kwargs)
39+
scheduled_date = fields.Datetime.now() + timedelta(seconds=30)
40+
Schedule = self.env["mail.message.schedule"].sudo()
41+
sched = Schedule.search([("mail_message_id", "=", message.id)], limit=1)
42+
if sched:
43+
sched.scheduled_datetime = scheduled_date
44+
# In case emails are already created but not sent yet, cancel queued mails and
45+
# delete the notification (so nothing goes out with old content)
46+
else:
47+
message.mail_ids.filtered(
48+
lambda x: x.state in {"outgoing", "exception", "draft"}
49+
).write({"state": "cancel"})
50+
message.notification_ids.filtered(
51+
lambda x: x.notification_status
52+
in {"ready", "exception", "canceled", "bounce"}
53+
).unlink()
54+
Schedule.create(
55+
{"mail_message_id": message.id, "scheduled_datetime": scheduled_date}
6056
)
61-
62-
def _message_update_content(self, *args, **kwargs):
63-
"""Defer messages by extra 30 seconds after updates."""
64-
kwargs.setdefault(
65-
"scheduled_date", fields.Datetime.now() + timedelta(seconds=30)
66-
)
67-
return super()._message_update_content(*args, **kwargs)
57+
kw = dict(kwargs)
58+
# Drop empty [] for partner_ids, or else Odoo will try to send to nobody
59+
if not kw.get("partner_ids"):
60+
kw.pop("partner_ids", None)
61+
res = super()._message_update_content(message, body=body, **kw)
62+
# Delete empty pending outgoing mails
63+
if empty_messages := message.sudo()._filter_empty():
64+
empty_messages.mail_ids.filtered(
65+
lambda mail: mail.state == "outgoing"
66+
).unlink()
67+
empty_messages.env["mail.message.schedule"].search(
68+
[("mail_message_id", "in", empty_messages.ids)]
69+
).unlink()
70+
return res

mail_post_defer/readme/CONFIGURE.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@ You usually don't need to do anything. The module is configured appropriately ou
22
Just make sure the following scheduled actions are active:
33

44
- Mail: Email Queue Manager (mail.ir_cron_mail_scheduler_action)
5-
- Notification: Send scheduled message notifications (mail.ir_cron_send_scheduled_message)
5+
- Notification: Notify scheduled messages (mail.ir_cron_send_scheduled_message)
66

7-
The mail queue processing is made by a cron job. This is normal Odoo
8-
behavior, not specific to this module. However, since you will start
7+
The mail queue processing and scheduled messages notifications are handled by cron jobs.
8+
This is normal Odoo behavior, not specific to this module. However, since you will start
99
using that queue for every message posted by any user in any thread,
10-
this module configures that job to execute every minute by default.
10+
both jobs are configured to execute every minute by default.
1111

1212
You can still change that cadence after installing the module (although
1313
it is not recommended). To do so:
1414

1515
1. Log in with an administrator user.
1616
2. Activate developer mode.
1717
3. Go to *Settings \> Technical \> Automation \> Scheduled Actions*.
18-
4. Edit the action named "Mail: Email Queue Manager".
19-
5. Lower down the frequency in the field *Execute Every*. Recommended: 1 minute.
18+
4. Find the cron(s) you want to adjust:
19+
- Mail: Email Queue Manager - handles outgoing emails.
20+
- Notification: Notify scheduled messages - handles notifications.
21+
5. Lower down the frequency in the field *Execute Every*. Recommended: 1 minute.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
- Jairo Llopis (<https://www.moduon.team/>)
2+
- [OERP Canada](https://www.oerp.ca)
3+
- Yash Pathak

mail_post_defer/static/description/index.html

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ <h1>Deferred Message Posting</h1>
374374
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
375375
!! source digest: sha256:b8388b0cb11cde1fb22cb0f1e2d686f10b048d834cf2b8fa29b3582c98b85853
376376
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
377-
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Alpha" src="https://img.shields.io/badge/maturity-Alpha-red.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/lgpl-3.0-standalone.html"><img alt="License: LGPL-3" src="https://img.shields.io/badge/license-LGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/mail/tree/18.0/mail_post_defer"><img alt="OCA/mail" src="https://img.shields.io/badge/github-OCA%2Fmail-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/mail-18-0/mail-18-0-mail_post_defer"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/mail&amp;target_branch=18.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
377+
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Alpha" src="https://img.shields.io/badge/maturity-Alpha-red.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/lgpl-3.0-standalone.html"><img alt="License: LGPL-3" src="https://img.shields.io/badge/license-LGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/mail/tree/19.0/mail_post_defer"><img alt="OCA/mail" src="https://img.shields.io/badge/github-OCA%2Fmail-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/mail-19-0/mail-19-0-mail_post_defer"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/mail&amp;target_branch=19.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
378378
<p>This module enhances mail threads by using the mail queue by default.</p>
379379
<p>Without this module, Odoo attempts to notify recipients of your message
380380
immediately. If your mail server is slow or you have many followers,
@@ -411,20 +411,25 @@ <h2><a class="toc-backref" href="#toc-entry-1">Configuration</a></h2>
411411
actions are active:</p>
412412
<ul class="simple">
413413
<li>Mail: Email Queue Manager (mail.ir_cron_mail_scheduler_action)</li>
414-
<li>Notification: Send scheduled message notifications
414+
<li>Notification: Notify scheduled messages
415415
(mail.ir_cron_send_scheduled_message)</li>
416416
</ul>
417-
<p>The mail queue processing is made by a cron job. This is normal Odoo
418-
behavior, not specific to this module. However, since you will start
419-
using that queue for every message posted by any user in any thread,
420-
this module configures that job to execute every minute by default.</p>
417+
<p>The mail queue processing and scheduled messages notifications are
418+
handled by cron jobs. This is normal Odoo behavior, not specific to this
419+
module. However, since you will start using that queue for every message
420+
posted by any user in any thread, both jobs are configured to execute
421+
every minute by default.</p>
421422
<p>You can still change that cadence after installing the module (although
422423
it is not recommended). To do so:</p>
423424
<ol class="arabic simple">
424425
<li>Log in with an administrator user.</li>
425426
<li>Activate developer mode.</li>
426427
<li>Go to <em>Settings &gt; Technical &gt; Automation &gt; Scheduled Actions</em>.</li>
427-
<li>Edit the action named “Mail: Email Queue Manager”.</li>
428+
<li>Find the cron(s) you want to adjust:<ul>
429+
<li>Mail: Email Queue Manager - handles outgoing emails.</li>
430+
<li>Notification: Notify scheduled messages - handles notifications.</li>
431+
</ul>
432+
</li>
428433
<li>Lower down the frequency in the field <em>Execute Every</em>. Recommended: 1
429434
minute.</li>
430435
</ol>
@@ -461,7 +466,7 @@ <h2><a class="toc-backref" href="#toc-entry-4">Bug Tracker</a></h2>
461466
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/mail/issues">GitHub Issues</a>.
462467
In case of trouble, please check there if your issue has already been reported.
463468
If you spotted it first, help us to smash it by providing a detailed and welcomed
464-
<a class="reference external" href="https://github.com/OCA/mail/issues/new?body=module:%20mail_post_defer%0Aversion:%2018.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
469+
<a class="reference external" href="https://github.com/OCA/mail/issues/new?body=module:%20mail_post_defer%0Aversion:%2019.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
465470
<p>Do not contact contributors directly about support or help with technical issues.</p>
466471
</div>
467472
<div class="section" id="credits">
@@ -470,12 +475,17 @@ <h2><a class="toc-backref" href="#toc-entry-5">Credits</a></h2>
470475
<h3><a class="toc-backref" href="#toc-entry-6">Authors</a></h3>
471476
<ul class="simple">
472477
<li>Moduon</li>
478+
<li>OERP Canada</li>
473479
</ul>
474480
</div>
475481
<div class="section" id="contributors">
476482
<h3><a class="toc-backref" href="#toc-entry-7">Contributors</a></h3>
477483
<ul class="simple">
478484
<li>Jairo Llopis (<a class="reference external" href="https://www.moduon.team/">https://www.moduon.team/</a>)</li>
485+
<li><a class="reference external" href="https://www.oerp.ca">OERP Canada</a><ul>
486+
<li>Yash Pathak</li>
487+
</ul>
488+
</li>
479489
</ul>
480490
</div>
481491
<div class="section" id="maintainers">
@@ -489,7 +499,7 @@ <h3><a class="toc-backref" href="#toc-entry-8">Maintainers</a></h3>
489499
promote its widespread use.</p>
490500
<p>Current <a class="reference external" href="https://odoo-community.org/page/maintainer-role">maintainer</a>:</p>
491501
<p><a class="reference external image-reference" href="https://github.com/Yajo"><img alt="Yajo" src="https://github.com/Yajo.png?size=40px" /></a></p>
492-
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/mail/tree/18.0/mail_post_defer">OCA/mail</a> project on GitHub.</p>
502+
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/mail/tree/19.0/mail_post_defer">OCA/mail</a> project on GitHub.</p>
493503
<p>You are welcome to contribute. To learn how please visit <a class="reference external" href="https://odoo-community.org/page/Contribute">https://odoo-community.org/page/Contribute</a>.</p>
494504
</div>
495505
</div>

0 commit comments

Comments
 (0)