[FIX] mail: attachments in the message when marking activity as done
Fine tuning of 1e411e3a6d - It moves the attachment in the message when we mark an activity as done. - It adds a test OPW-2196668 closes odoo/odoo#47349 X-original-commit: 2033066426a6af6eed5f80f1bcde6e650e998f0d Signed-off-by: Nicolas Martinelli (nim) <nim@odoo.com>
This commit is contained in:
@@ -464,6 +464,19 @@ class MailActivity(models.Model):
|
||||
# marking as 'done'
|
||||
messages = self.env['mail.message']
|
||||
next_activities_values = []
|
||||
|
||||
# Search for all attachments linked to the activities we are about to unlink. This way, we
|
||||
# can link them to the message posted and prevent their deletion.
|
||||
attachments = self.env['ir.attachment'].search_read([
|
||||
('res_model', '=', self._name),
|
||||
('res_id', 'in', self.ids),
|
||||
], ['id', 'res_id'])
|
||||
|
||||
activity_attachments = defaultdict(list)
|
||||
for attachment in attachments:
|
||||
activity_id = attachment['res_id']
|
||||
activity_attachments[activity_id].append(attachment['id'])
|
||||
|
||||
for activity in self:
|
||||
# extract value to generate next activities
|
||||
if activity.force_next:
|
||||
@@ -494,7 +507,19 @@ class MailActivity(models.Model):
|
||||
mail_activity_type_id=activity.activity_type_id.id,
|
||||
attachment_ids=[(4, attachment_id) for attachment_id in attachment_ids] if attachment_ids else [],
|
||||
)
|
||||
messages |= record.message_ids[0]
|
||||
|
||||
# Moving the attachments in the message
|
||||
# TODO: Fix void res_id on attachment when you create an activity with an image
|
||||
# directly, see route /web_editor/attachment/add
|
||||
activity_message = record.message_ids[0]
|
||||
message_attachments = self.env['ir.attachment'].browse(activity_attachments[activity.id])
|
||||
if message_attachments:
|
||||
message_attachments.write({
|
||||
'res_id': activity_message.id,
|
||||
'res_model': activity_message._name,
|
||||
})
|
||||
activity_message.attachment_ids = message_attachments
|
||||
messages |= activity_message
|
||||
|
||||
next_activities = self.env['mail.activity'].create(next_activities_values)
|
||||
self.unlink() # will unlink activity, dont access `self` after that
|
||||
|
||||
@@ -148,6 +148,50 @@ class TestActivityFlow(TestActivityCommon):
|
||||
activity.with_user(self.user_admin).write({'user_id': self.user_employee.id})
|
||||
self.assertEqual(activity.user_id, self.user_employee)
|
||||
|
||||
def test_action_feedback_attachment(self):
|
||||
Partner = self.env['res.partner']
|
||||
Activity = self.env['mail.activity']
|
||||
Attachment = self.env['ir.attachment']
|
||||
Message = self.env['mail.message']
|
||||
|
||||
partner = self.env['res.partner'].create({
|
||||
'name': 'Tester',
|
||||
})
|
||||
|
||||
activity = Activity.create({
|
||||
'summary': 'Test',
|
||||
'activity_type_id': 1,
|
||||
'res_model_id': self.env.ref('base.model_res_partner').id,
|
||||
'res_id': partner.id,
|
||||
})
|
||||
|
||||
attachments = Attachment
|
||||
attachments += Attachment.create({
|
||||
'name': 'test',
|
||||
'res_name': 'test',
|
||||
'res_model': 'mail.activity',
|
||||
'res_id': activity.id,
|
||||
'datas': 'test',
|
||||
})
|
||||
attachments += Attachment.create({
|
||||
'name': 'test2',
|
||||
'res_name': 'test',
|
||||
'res_model': 'mail.activity',
|
||||
'res_id': activity.id,
|
||||
'datas': 'testtest',
|
||||
})
|
||||
|
||||
# Adding the attachments to the activity
|
||||
activity.attachment_ids = attachments
|
||||
|
||||
# Checking if the attachment has been forwarded to the message
|
||||
# when marking an activity as "Done"
|
||||
activity.action_feedback()
|
||||
activity_message = Message.search([], order='id desc', limit=1)
|
||||
self.assertEqual(set(activity_message.attachment_ids.ids), set(attachments.ids))
|
||||
for attachment in attachments:
|
||||
self.assertEqual(attachment.res_id, activity_message.id)
|
||||
self.assertEqual(attachment.res_model, activity_message._name)
|
||||
|
||||
@tests.tagged('mail_activity')
|
||||
class TestActivityMixin(TestActivityCommon):
|
||||
|
||||
@@ -229,7 +229,7 @@ class TestMailAPIPerformance(BaseMailPerformance):
|
||||
#voip module read activity_type during create leading to one less query in enterprise on action_feedback
|
||||
category = activity.activity_type_id.category
|
||||
|
||||
with self.assertQueryCount(__system__=17, emp=21):
|
||||
with self.assertQueryCount(__system__=18, emp=22):
|
||||
activity.action_feedback(feedback='Zizisse Done !')
|
||||
|
||||
@users('__system__', 'emp')
|
||||
@@ -246,7 +246,7 @@ class TestMailAPIPerformance(BaseMailPerformance):
|
||||
|
||||
record.write({'name': 'Dupe write'})
|
||||
|
||||
with self.assertQueryCount(__system__=18, emp=21):
|
||||
with self.assertQueryCount(__system__=19, emp=22):
|
||||
record.action_close('Dupe feedback')
|
||||
|
||||
self.assertEqual(record.activity_ids, self.env['mail.activity'])
|
||||
|
||||
Reference in New Issue
Block a user