Skip to content
Snippets Groups Projects
xbus_message_link.py 2.45 KiB
Newer Older
##############################################################################
#
#    Xbus Common, for Odoo
Axel Prel's avatar
Axel Prel committed
#    Copyright © 2023, 2024 XCG Consulting <https://xcg-consulting.fr>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from odoo import _, exceptions, fields, models


class XbusMessageLink(models.Model):
    """Represents a link between a message and an Odoo object.

    Informational only, often used for analysing purpose.
    The data might be referencing an unlinked record"""

    _name = "xbus.message.link"
    _description = "Message-Record Link"

    _log_access = False

    message_id = fields.Many2one(
        string="Message",
        comodel_name="xbus.message",
        readonly=True,
        index=True,
    )

    res_model = fields.Char(
        string="Model Name",
        required=True,
        help="The record this message is for.",
        readonly=True,
        index=True,
    )
    res_id = fields.Many2oneReference(
        string="Resource ID",
        required=True,
        model_field="res_model",
        help="ID of the target record in the database",
        readonly=True,
        index=True,
    )

    def action_open_document(self):
        """Opens the related record based on the model and ID"""
        self.ensure_one()
        record_id = self.res_id
        record_model = self.res_model
        if record_model in self.env:
            record = self.env[record_model].browse(record_id)
        if record_model not in self.env or not record:
            raise exceptions.MissingError(_("Source record missing."))
        return {
            "res_id": self.res_id,
            "res_model": self.res_model,
            "target": "current",
            "type": "ir.actions.act_window",
            "view_mode": "form",
        }