account_audit_unlink/models/mail_thread.py
2026-03-17 10:42:33 +07:00

47 lines
2.0 KiB
Python

# -*- coding: utf-8 -*-
from odoo import api, models, _
class MailThread(models.AbstractModel):
_inherit = 'mail.thread'
def unlink(self):
""" Override unlink to create a persistent audit log record before deletion.
The audit log will have res_id=0 to ensure it survives the standard unlink process.
"""
audit_models = (
'account.move', 'account.account', 'account.tax', 'res.partner', 'res.company',
'mrp.production', 'mrp.bom', 'stock.picking', 'stock.move', 'product.template', 'product.product',
'purchase.order', 'sale.order', 'hr.employee', 'product.category', 'stock.lot'
)
if self._name in audit_models:
for record in self:
# Get basic info while record still exists
record_id = record.id
record_name = record.display_name or _('Unknown Record')
company_id = getattr(record, 'company_id', self.env.company).id
# Create the persistent audit log
self.env['mail.message'].sudo().create({
'model': self._name,
'res_id': 0, # Persistent ID (not linked to an existing record)
'message_type': 'notification',
'subject': _('Record Unlinked: %s', record_name),
'body': _(
'<p>The following record was unlinked by <b>%s</b>:</p>'
'<ul>'
'<li><b>Model:</b> %s</li>'
'<li><b>Technical ID:</b> %s</li>'
'<li><b>Display Name:</b> %s</li>'
'</ul>',
self.env.user.name,
self._name,
record_id,
record_name
),
'author_id': self.env.user.partner_id.id,
'record_company_id': company_id,
})
return super(MailThread, self).unlink()