-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FIX] hr_expense: set main attachment on
hr.expense.sheet
Issue: Main attachments on expense sheets was never correctly set, leading to missing attachment previous in the hr.expense.sheet form view, and unnecessary RPC requests to `register_main_attachment`, because the attachment was never set. Steps to reproduce: - Install Expenses - Create 2 different expenses with different attachments - Create a report from those 2 expenses, save it - Notice there is nothing in the preview for the attachments, but if you zoom in or zoom out the page, then you can see it. Also swiping on the list of attachments isn't persistent, once you refresh the page, the main attachment we selected lastly is lost. Cause: There are 2 main issues: - `hr.expense.sheet` has `message_main_attachment_id`, but it's never set. - When writing an attachment via `register_main_attachment`, we pass only the id of the attachment. Then in the backend, the related record is based on the model of the attachment, but the attachment is linked to `hr.expense`, not `hr.expense.sheet`. Therefor we re-write the same attachment on the record we read the attachment from. And then the frontend will continue to call `register_main_attachment` everytime it tries to find the attachments linked to the sheet, but it's never done. Fix: - Add a compute to initially set the value of the `message_main_attachment_id` based on the spec "we take the first line that has a main attachment (attachment if no main attachment) and we set it on the sheet" - The `hr.expense.sheet` now maintains a copies of the attachments linked to it's expenses, for proper ACL and functionality of `register_main_attachment`. Affected versions: 16.0 up to master = saas-17.1 Reference: task-3572440 closes odoo#151183 X-original-commit: 6ae4729 Signed-off-by: Cedric Snauwaert <csn@odoo.com> Signed-off-by: Piryns Victor (pivi) <pivi@odoo.com>
- Loading branch information
Showing
5 changed files
with
184 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from odoo import models, api | ||
|
||
|
||
class IrAttachment(models.Model): | ||
_inherit = 'ir.attachment' | ||
|
||
@api.model_create_multi | ||
def create(self, vals_list): | ||
attachments = super().create(vals_list) | ||
if self.env.context.get('sync_attachment', True): | ||
expenses_attachments = attachments.filtered(lambda att: att.res_model == 'hr.expense') | ||
if expenses_attachments: | ||
expenses = self.env['hr.expense'].browse(expenses_attachments.mapped('res_id')) | ||
for expense in expenses.filtered('sheet_id'): | ||
checksums = set(expense.sheet_id.attachment_ids.mapped('checksum')) | ||
for attachment in expense.attachment_ids.filtered(lambda att: att.checksum not in checksums): | ||
attachment.copy({ | ||
'res_model': 'hr.expense.sheet', | ||
'res_id': expense.sheet_id.id, | ||
}) | ||
return attachments | ||
|
||
def unlink(self): | ||
if self.env.context.get('sync_attachment', True): | ||
attachments_to_unlink = self.env['ir.attachment'] | ||
expenses_attachments = self.filtered(lambda att: att.res_model == 'hr.expense') | ||
if expenses_attachments: | ||
expenses = self.env['hr.expense'].browse(expenses_attachments.mapped('res_id')) | ||
for expense in expenses.exists().filtered('sheet_id'): | ||
checksums = set(expense.attachment_ids.mapped('checksum')) | ||
attachments_to_unlink += expense.sheet_id.attachment_ids.filtered(lambda att: att.checksum in checksums) | ||
sheets_attachments = self.filtered(lambda att: att.res_model == 'hr.expense.sheet') | ||
if sheets_attachments: | ||
sheets = self.env['hr.expense.sheet'].browse(sheets_attachments.mapped('res_id')) | ||
for sheet in sheets.exists(): | ||
checksums = set((sheet.attachment_ids & sheets_attachments).mapped('checksum')) | ||
attachments_to_unlink += sheet.expense_line_ids.attachment_ids.filtered(lambda att: att.checksum in checksums) | ||
super(IrAttachment, attachments_to_unlink).unlink() | ||
return super().unlink() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters