Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[16.0][FIX] mail_activity_done: patch _action_done in a more reliable way #1214

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mail_activity_done/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from . import models
from .hooks import post_load_hook, pre_init_hook, uninstall_hook
from .hooks import pre_init_hook, uninstall_hook
1 change: 0 additions & 1 deletion mail_activity_done/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,5 @@
],
},
"pre_init_hook": "pre_init_hook",
"post_load": "post_load_hook",
"uninstall_hook": "uninstall_hook",
}
54 changes: 0 additions & 54 deletions mail_activity_done/hooks.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# Copyright 2018-22 ForgeFlow <http://www.forgeflow.com>
# Copyright 2018 Odoo, S.A.
# License AGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
from odoo import Command, fields

from odoo.addons.mail.models.mail_activity import MailActivity


def pre_init_hook(cr):
Expand Down Expand Up @@ -31,57 +28,6 @@ def pre_init_hook(cr):
)


def post_load_hook():
def _new_action_done(self, feedback=False, attachment_ids=None):
"""Overwritten method"""
if "done" not in self._fields:
return self._action_done_original(
feedback=feedback, attachment_ids=attachment_ids
)
# marking as 'done'
messages = self.env["mail.message"]
next_activities_values = []
for activity in self:
# extract value to generate next activities
if activity.chaining_type == "trigger":
vals = activity.with_context(
activity_previous_deadline=activity.date_deadline
)._prepare_next_activity_values()
next_activities_values.append(vals)

# post message on activity, before deleting it
record = self.env[activity.res_model].browse(activity.res_id)
activity.done = True
activity.active = False
activity.date_done = fields.Date.today()
record.message_post_with_view(
"mail.message_activity_done",
values={
"activity": activity,
"feedback": feedback,
"display_assignee": activity.user_id != self.env.user,
},
subtype_id=self.env["ir.model.data"]._xmlid_to_res_id(
"mail.mt_activities"
),
mail_activity_type_id=activity.activity_type_id.id,
attachment_ids=[
Command.link(attachment_id) for attachment_id in attachment_ids
]
if attachment_ids
else [],
)
messages |= record.message_ids[0]

next_activities = self.env["mail.activity"].create(next_activities_values)

return messages, next_activities

if not hasattr(MailActivity, "_action_done_original"):
MailActivity._action_done_original = MailActivity._action_done
MailActivity._action_done = _new_action_done


def uninstall_hook(cr, registry):
"""The objective of this hook is to remove all activities that are done
upon module uninstall
Expand Down
15 changes: 15 additions & 0 deletions mail_activity_done/models/mail_activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
from odoo import api, fields, models

delete_sentinel = object()


class MailActivity(models.Model):

Expand Down Expand Up @@ -61,6 +63,19 @@ def _search_state(self, operator, operand):
("done", "=", True),
]

def unlink(self):
"""Don't unlink if we're asked not to"""
if self.env.context.get("mail_activity_done") != delete_sentinel:
return super().unlink()

def _action_done(self, feedback=False, attachment_ids=None):
"""Ask super not to delete the activity and set it to done"""
self.write({"done": True, "active": False, "date_done": fields.Date.today()})
return super(
MailActivity,
self.with_context(mail_activity_done=delete_sentinel),
)._action_done(feedback=feedback, attachment_ids=attachment_ids)


class MailActivityMixin(models.AbstractModel):

Expand Down
3 changes: 2 additions & 1 deletion mail_activity_done/tests/test_mail_activity_done.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ def setUp(self):
)

def test_mail_activity_done(self):
self.act1.done = True
self.act1._action_done()
self.assertTrue(self.act1.exists())
self.assertEqual(self.act1.state, "done")

def test_systray_get_activities(self):
Expand Down
Loading