Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[14.0][REF+IMP] mrp_attachment_mgmt: Cleanup + button in production orders #1007

Merged
merged 2 commits into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mrp_attachment_mgmt/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
Expand Down
1 change: 1 addition & 0 deletions mrp_attachment_mgmt/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from . import mrp_bom
from . import mrp_production
from . import mrp_workorder
from . import product
28 changes: 5 additions & 23 deletions mrp_attachment_mgmt/models/mrp_bom.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,12 @@
# 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


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:
Expand All @@ -40,12 +25,9 @@ def _get_components_ids(self, product_tmpl=None, product=None, recursive=False):
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()
10 changes: 10 additions & 0 deletions mrp_attachment_mgmt/models/mrp_production.py
Original file line number Diff line number Diff line change
@@ -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()
19 changes: 2 additions & 17 deletions mrp_attachment_mgmt/models/mrp_workorder.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -7,20 +8,6 @@
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"):
Expand All @@ -33,6 +20,4 @@ def action_see_workorder_attachments(self):
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()
24 changes: 20 additions & 4 deletions mrp_attachment_mgmt/models/product.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
5 changes: 5 additions & 0 deletions mrp_attachment_mgmt/tests/test_mrp_attachment_mgmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,8 @@ def test_mrp_workorder_attachments(self):
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)
30 changes: 30 additions & 0 deletions mrp_attachment_mgmt/views/mrp_production_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record id="mrp_production_form_view" model="ir.ui.view">
<field name="name">mrp.production.form - Add attachments smart-button</field>
<field name="model">mrp.production</field>
<field name="inherit_id" ref="mrp.mrp_production_form_view" />
<field name="arch" type="xml">
<xpath expr="///button[@name='action_see_move_scrap']" position="after">
<button
class="oe_stat_button"
name="action_show_attachments"
type="object"
icon="fa-files-o"
string="Attachments"
/>
</xpath>
</field>
</record>
<record id="action_show_production_attachments" model="ir.actions.server">
<field name="name">Attachments</field>
<field name="model_id" ref="mrp.model_mrp_production" />
<field name="binding_model_id" ref="mrp.model_mrp_production" />
<field name="binding_view_types">list</field>
<field name="state">code</field>
<field name="code">
if records:
action = records.action_show_attachments()
</field>
</record>
</odoo>