From a23f1ed5b33d903ac9b2f5dc23170027a27eb114 Mon Sep 17 00:00:00 2001 From: "Pedro M. Baeza" Date: Mon, 27 Mar 2023 13:10:57 +0200 Subject: [PATCH 1/2] [REF] mrp_attachment_mgmt: Avoid code duplication for attachment view - Put the method on product, and call it from all the places. - Remove excessive method fragmentation w/o gain. - Replace action read by proper `_for_xml_id` call. --- mrp_attachment_mgmt/models/mrp_bom.py | 28 ++++----------------- mrp_attachment_mgmt/models/mrp_workorder.py | 19 ++------------ mrp_attachment_mgmt/models/product.py | 24 +++++++++++++++--- 3 files changed, 27 insertions(+), 44 deletions(-) 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 From 42f59d377eaf7aeca199a3749462722d9f586abd Mon Sep 17 00:00:00 2001 From: "Pedro M. Baeza" Date: Mon, 27 Mar 2023 13:17:25 +0200 Subject: [PATCH 2/2] [IMP] mrp_attachment_mgmt: Add smart-button to production orders --- mrp_attachment_mgmt/__manifest__.py | 1 + mrp_attachment_mgmt/models/__init__.py | 1 + mrp_attachment_mgmt/models/mrp_production.py | 10 +++++++ .../tests/test_mrp_attachment_mgmt.py | 5 ++++ .../views/mrp_production_views.xml | 30 +++++++++++++++++++ 5 files changed, 47 insertions(+) create mode 100644 mrp_attachment_mgmt/models/mrp_production.py create mode 100644 mrp_attachment_mgmt/views/mrp_production_views.xml diff --git a/mrp_attachment_mgmt/__manifest__.py b/mrp_attachment_mgmt/__manifest__.py index 45791accf..662975cf6 100644 --- a/mrp_attachment_mgmt/__manifest__.py +++ b/mrp_attachment_mgmt/__manifest__.py @@ -11,6 +11,7 @@ "installable": True, "data": [ "views/mrp_bom_view.xml", + "views/mrp_production_views.xml", "views/product_views.xml", "views/workorder_attachments_views.xml", ], diff --git a/mrp_attachment_mgmt/models/__init__.py b/mrp_attachment_mgmt/models/__init__.py index cf484cc1a..1542b7b49 100644 --- a/mrp_attachment_mgmt/models/__init__.py +++ b/mrp_attachment_mgmt/models/__init__.py @@ -1,3 +1,4 @@ from . import mrp_bom +from . import mrp_production from . import mrp_workorder from . import product diff --git a/mrp_attachment_mgmt/models/mrp_production.py b/mrp_attachment_mgmt/models/mrp_production.py new file mode 100644 index 000000000..8410a213b --- /dev/null +++ b/mrp_attachment_mgmt/models/mrp_production.py @@ -0,0 +1,10 @@ +# Copyright 2023 Tecnativa - Pedro M. Baeza +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +from odoo import models + + +class MrpProduction(models.Model): + _inherit = "mrp.production" + + def action_show_attachments(self): + return self.product_id._action_show_attachments() diff --git a/mrp_attachment_mgmt/tests/test_mrp_attachment_mgmt.py b/mrp_attachment_mgmt/tests/test_mrp_attachment_mgmt.py index 354518f6d..799578bb5 100644 --- a/mrp_attachment_mgmt/tests/test_mrp_attachment_mgmt.py +++ b/mrp_attachment_mgmt/tests/test_mrp_attachment_mgmt.py @@ -33,3 +33,8 @@ class TestMrpAttachmentMgmt(TestMrpAttachmentMgmtBase): attachment = self._create_attachment(self.product) action = self.workorder.action_see_workorder_attachments() self.assertIn(attachment.id, self.attachment_model.search(action["domain"]).ids) + + def test_mrp_production_attachments(self): + attachment = self._create_attachment(self.product) + action = self.mrp_production.action_show_attachments() + self.assertIn(attachment.id, self.attachment_model.search(action["domain"]).ids) diff --git a/mrp_attachment_mgmt/views/mrp_production_views.xml b/mrp_attachment_mgmt/views/mrp_production_views.xml new file mode 100644 index 000000000..808845724 --- /dev/null +++ b/mrp_attachment_mgmt/views/mrp_production_views.xml @@ -0,0 +1,30 @@ + + + + mrp.production.form - Add attachments smart-button + mrp.production + + + +