diff --git a/mrp_attachment_mgmt/models/mrp_bom.py b/mrp_attachment_mgmt/models/mrp_bom.py index d4c2ccebc..6926417ed 100644 --- a/mrp_attachment_mgmt/models/mrp_bom.py +++ b/mrp_attachment_mgmt/models/mrp_bom.py @@ -1,4 +1,5 @@ # Copyright 2022 Tecnativa - Víctor Martínez +# Copyright 2023 Tecnativa - Pedro M. Baeza # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from odoo import api, models @@ -6,22 +7,6 @@ from odoo import api, models class MrpBom(models.Model): _inherit = "mrp.bom" - def _action_see_bom_documents_products(self, products): - domain = [ - # ("res_field", "=", False), - "|", - "&", - ("res_model", "=", "product.product"), - ("res_id", "in", products.ids), - "&", - ("res_model", "=", "product.template"), - ("res_id", "in", products.mapped("product_tmpl_id").ids), - ] - res = self.env.ref("base.action_attachment").read()[0] - ctx = {"create": False, "edit": False} - res.update({"domain": domain, "context": ctx}) - return res - @api.model def _get_components_ids(self, product_tmpl=None, product=None, recursive=False): """Gets an objet with the ids of the components, within two arrays: @@ -40,12 +25,9 @@ class MrpBom(models.Model): product_ids.extend(subcomponents) return product_ids - def _action_see_bom_documents(self, product_tmpl=None, product=None): - product_ids = self._get_components_ids(product_tmpl, product, True) - products = self.env["product.product"].search([("id", "in", product_ids)]) - return self._action_see_bom_documents_products(products) - def action_see_bom_documents(self): - return self._action_see_bom_documents( - product_tmpl=self.product_tmpl_id, product=self.product_id + product_ids = self._get_components_ids( + self.product_tmpl_id, self.product_id, True ) + products = self.env["product.product"].search([("id", "in", product_ids)]) + return products._action_show_attachments() diff --git a/mrp_attachment_mgmt/models/mrp_workorder.py b/mrp_attachment_mgmt/models/mrp_workorder.py index e711f702c..e21c624bb 100644 --- a/mrp_attachment_mgmt/models/mrp_workorder.py +++ b/mrp_attachment_mgmt/models/mrp_workorder.py @@ -1,4 +1,5 @@ # Copyright 2021 Tecnativa - Víctor Martínez +# Copyright 2023 Tecnativa - Pedro M. Baeza # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from odoo import _, models from odoo.exceptions import UserError @@ -7,20 +8,6 @@ from odoo.exceptions import UserError class MrpWorkorder(models.Model): _inherit = "mrp.workorder" - def _action_see_workorder_attachments_products(self, products): - domain = [ - "|", - "&", - ("res_model", "=", "product.product"), - ("res_id", "in", products.ids), - "&", - ("res_model", "=", "product.template"), - ("res_id", "in", products.mapped("product_tmpl_id").ids), - ] - action = self.env.ref("base.action_attachment").read()[0] - action.update({"domain": domain}) - return action - def action_see_workorder_attachments(self): error = [] for product in self.mapped("product_id"): @@ -33,6 +20,4 @@ class MrpWorkorder(models.Model): raise UserError( _("%d Product(s) without drawing:\n%s") % (len(error), "\n".join(error)) ) - return self._action_see_workorder_attachments_products( - self.mapped("product_id") - ) + return self.product_id._action_show_attachments() diff --git a/mrp_attachment_mgmt/models/product.py b/mrp_attachment_mgmt/models/product.py index 265c141f2..168fbde1a 100644 --- a/mrp_attachment_mgmt/models/product.py +++ b/mrp_attachment_mgmt/models/product.py @@ -1,4 +1,5 @@ # Copyright 2022 Tecnativa - Víctor Martínez +# Copyright 2023 Tecnativa - Pedro M. Baeza # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from odoo import fields, models @@ -7,13 +8,28 @@ class ProductTemplate(models.Model): _inherit = "product.template" def action_see_bom_documents(self): - first_bom = fields.first(self.bom_ids) - return first_bom._action_see_bom_documents(self) + return fields.first(self.bom_ids).action_see_bom_documents() class ProductProduct(models.Model): _inherit = "product.product" def action_see_bom_documents(self): - first_bom = fields.first(self.bom_ids) - return first_bom._action_see_bom_documents(self.product_tmpl_id, self) + return fields.first(self.bom_ids).action_see_bom_documents() + + def _action_show_attachments(self): + """Returns the action to show the attachments linked to the products + recordset or to their templates. + """ + domain = [ + "|", + "&", + ("res_model", "=", "product.product"), + ("res_id", "in", self.ids), + "&", + ("res_model", "=", "product.template"), + ("res_id", "in", self.product_tmpl_id.ids), + ] + action = self.env["ir.actions.actions"]._for_xml_id("base.action_attachment") + action.update({"domain": domain}) + return action