Skip to content

Commit

Permalink
[REF] mail: manage attachments through ORM
Browse files Browse the repository at this point in the history
Instead of doing a manual search and grouping of the attachments, rely
on the ORM to do the labour.
  • Loading branch information
william-andre committed Jun 14, 2023
1 parent 2e3658d commit 749e8a4
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 20 deletions.
25 changes: 5 additions & 20 deletions addons/mail/models/mail_activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def _default_activity_type_for_model(self, model):
'Related Document Model',
index=True, related='res_model_id.model', precompute=True, store=True, readonly=True)
res_id = fields.Many2oneReference(string='Related Document ID', index=True, model_field='res_model')
linked_attachment_ids = fields.One2many(comodel_name='ir.attachment', inverse_name='res_id')
res_name = fields.Char(
'Document Name', compute='_compute_res_name', compute_sudo=True, store=True,
readonly=True)
Expand Down Expand Up @@ -498,18 +499,6 @@ def _action_done(self, feedback=False, attachment_ids=None):
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 model, activity_data in self._classify_by_model().items():
records = self.env[model].browse(activity_data['record_ids'])
for record, activity in zip(records, activity_data['activities']):
Expand All @@ -534,14 +523,10 @@ def _action_done(self, feedback=False, attachment_ids=None):
# 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
if activity_attachments[activity.id]:
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
message_attachments = activity.linked_attachment_ids
if message_attachments:
activity_message.attachment_ids = message_attachments
activity_message.linked_attachment_ids += message_attachments
messages += activity_message

next_activities = self.env['mail.activity']
Expand Down
1 change: 1 addition & 0 deletions addons/mail/models/mail_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def default_get(self, fields):
'ir.attachment', 'message_attachment_rel',
'message_id', 'attachment_id',
string='Attachments')
linked_attachment_ids = fields.One2many(comodel_name='ir.attachment', inverse_name='res_id')
parent_id = fields.Many2one(
'mail.message', 'Parent Message', index='btree_not_null', ondelete='set null')
child_ids = fields.One2many('mail.message', 'parent_id', 'Child Messages')
Expand Down

0 comments on commit 749e8a4

Please sign in to comment.