[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.
This commit is contained in:
Pedro M. Baeza
2023-03-27 13:10:57 +02:00
parent e01a9c4524
commit a23f1ed5b3
3 changed files with 27 additions and 44 deletions

View File

@@ -1,4 +1,5 @@
# Copyright 2022 Tecnativa - Víctor Martínez # 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). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo import api, models from odoo import api, models
@@ -6,22 +7,6 @@ from odoo import api, models
class MrpBom(models.Model): class MrpBom(models.Model):
_inherit = "mrp.bom" _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 @api.model
def _get_components_ids(self, product_tmpl=None, product=None, recursive=False): def _get_components_ids(self, product_tmpl=None, product=None, recursive=False):
"""Gets an objet with the ids of the components, within two arrays: """Gets an objet with the ids of the components, within two arrays:
@@ -40,12 +25,9 @@ class MrpBom(models.Model):
product_ids.extend(subcomponents) product_ids.extend(subcomponents)
return product_ids 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): def action_see_bom_documents(self):
return self._action_see_bom_documents( product_ids = self._get_components_ids(
product_tmpl=self.product_tmpl_id, product=self.product_id self.product_tmpl_id, self.product_id, True
) )
products = self.env["product.product"].search([("id", "in", product_ids)])
return products._action_show_attachments()

View File

@@ -1,4 +1,5 @@
# Copyright 2021 Tecnativa - Víctor Martínez # 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). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo import _, models from odoo import _, models
from odoo.exceptions import UserError from odoo.exceptions import UserError
@@ -7,20 +8,6 @@ from odoo.exceptions import UserError
class MrpWorkorder(models.Model): class MrpWorkorder(models.Model):
_inherit = "mrp.workorder" _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): def action_see_workorder_attachments(self):
error = [] error = []
for product in self.mapped("product_id"): for product in self.mapped("product_id"):
@@ -33,6 +20,4 @@ class MrpWorkorder(models.Model):
raise UserError( raise UserError(
_("%d Product(s) without drawing:\n%s") % (len(error), "\n".join(error)) _("%d Product(s) without drawing:\n%s") % (len(error), "\n".join(error))
) )
return self._action_see_workorder_attachments_products( return self.product_id._action_show_attachments()
self.mapped("product_id")
)

View File

@@ -1,4 +1,5 @@
# Copyright 2022 Tecnativa - Víctor Martínez # 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). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo import fields, models from odoo import fields, models
@@ -7,13 +8,28 @@ class ProductTemplate(models.Model):
_inherit = "product.template" _inherit = "product.template"
def action_see_bom_documents(self): def action_see_bom_documents(self):
first_bom = fields.first(self.bom_ids) return fields.first(self.bom_ids).action_see_bom_documents()
return first_bom._action_see_bom_documents(self)
class ProductProduct(models.Model): class ProductProduct(models.Model):
_inherit = "product.product" _inherit = "product.product"
def action_see_bom_documents(self): def action_see_bom_documents(self):
first_bom = fields.first(self.bom_ids) return fields.first(self.bom_ids).action_see_bom_documents()
return first_bom._action_see_bom_documents(self.product_tmpl_id, self)
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