|
| 1 | +# Copyright 2026 Cetmix |
| 2 | +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). |
| 3 | + |
| 4 | +import os |
| 5 | + |
| 6 | +from odoo import _, api, fields, models |
| 7 | +from odoo.exceptions import AccessError, UserError, ValidationError |
| 8 | + |
| 9 | +REASON_MISSING_FILE = "missing_file" |
| 10 | + |
| 11 | + |
| 12 | +class CleanupPurgeLineAttachment(models.TransientModel): |
| 13 | + _inherit = "cleanup.purge.line" |
| 14 | + _name = "cleanup.purge.line.attachment" |
| 15 | + _description = "Cleanup Purge Line Attachment" |
| 16 | + |
| 17 | + attachment_id = fields.Many2one("ir.attachment") |
| 18 | + reason = fields.Selection( |
| 19 | + [ |
| 20 | + (REASON_MISSING_FILE, "File missing in filestore"), |
| 21 | + ], |
| 22 | + ) |
| 23 | + error_message = fields.Char(readonly=True) |
| 24 | + wizard_id = fields.Many2one("cleanup.purge.wizard.attachment", readonly=True) |
| 25 | + |
| 26 | + def purge(self): |
| 27 | + """Unlink orphaned attachment records upon manual confirmation. |
| 28 | +
|
| 29 | + Filters unpurged lines with attachment_id. Unlinks each attachment |
| 30 | + individually; failures are logged and skipped so the batch continues. |
| 31 | + Only successfully removed attachments get their lines marked purged. |
| 32 | +
|
| 33 | + :return: result of write({"purged": True}) on successfully purged lines, |
| 34 | + or True if none were purged |
| 35 | + """ |
| 36 | + if self: |
| 37 | + objs = self |
| 38 | + else: |
| 39 | + objs = self.env["cleanup.purge.line.attachment"].browse( |
| 40 | + self._context.get("active_ids") |
| 41 | + ) |
| 42 | + to_unlink = objs.filtered(lambda x: not x.purged and x.attachment_id) |
| 43 | + self.logger.info("Purging attachments: %s", to_unlink.mapped("name")) |
| 44 | + purged_line_ids = [] |
| 45 | + for line in to_unlink: |
| 46 | + attach = line.attachment_id |
| 47 | + try: |
| 48 | + attach.unlink() |
| 49 | + purged_line_ids.append(line.id) |
| 50 | + except (UserError, ValidationError, AccessError) as exc: |
| 51 | + self.logger.warning( |
| 52 | + "Attachment #%s cannot be deleted: %s", |
| 53 | + attach.id, |
| 54 | + str(exc), |
| 55 | + ) |
| 56 | + line.error_message = str(exc) |
| 57 | + if not purged_line_ids: |
| 58 | + return True |
| 59 | + return ( |
| 60 | + self.env["cleanup.purge.line.attachment"] |
| 61 | + .browse(purged_line_ids) |
| 62 | + .write({"purged": True}) |
| 63 | + ) |
| 64 | + |
| 65 | + |
| 66 | +class CleanupPurgeWizardAttachment(models.TransientModel): |
| 67 | + _inherit = "cleanup.purge.wizard" |
| 68 | + _name = "cleanup.purge.wizard.attachment" |
| 69 | + _description = "Purge attachments" |
| 70 | + |
| 71 | + @api.model |
| 72 | + def find(self): |
| 73 | + """Collect ir.attachment records whose backing files are missing on disk. |
| 74 | +
|
| 75 | + Requires file storage. Searches binary attachments with store_fname, |
| 76 | + checks each file exists via os.path.isfile(_full_path(store_fname)). |
| 77 | +
|
| 78 | + :raises UserError: if storage != "file" or no orphaned entries found |
| 79 | + """ |
| 80 | + if self.env["ir.attachment"]._storage() != "file": |
| 81 | + raise UserError( |
| 82 | + _( |
| 83 | + "Attachment storage is not 'file'. " |
| 84 | + "Purge of orphaned attachments only works with file storage." |
| 85 | + ) |
| 86 | + ) |
| 87 | + res = [] |
| 88 | + attachments = self.env["ir.attachment"].search( |
| 89 | + [ |
| 90 | + ("store_fname", "!=", False), |
| 91 | + ("type", "=", "binary"), |
| 92 | + ] |
| 93 | + ) |
| 94 | + for attach in attachments: |
| 95 | + full_path = self.env["ir.attachment"]._full_path(attach.store_fname) |
| 96 | + if not os.path.isfile(full_path): |
| 97 | + res.append( |
| 98 | + fields.Command.create( |
| 99 | + { |
| 100 | + "attachment_id": attach.id, |
| 101 | + "name": attach.store_fname or attach.name or str(attach.id), |
| 102 | + "reason": REASON_MISSING_FILE, |
| 103 | + } |
| 104 | + ) |
| 105 | + ) |
| 106 | + if not res: |
| 107 | + raise UserError(_("No orphaned attachment entries found")) |
| 108 | + return res |
| 109 | + |
| 110 | + purge_line_ids = fields.One2many("cleanup.purge.line.attachment", "wizard_id") |
0 commit comments