From d193350c6f0348b5badd51d69ff2c840ae379a6a Mon Sep 17 00:00:00 2001
From: amoyaux <arm@odoo.com>
Date: Thu, 4 May 2017 15:51:51 +0200
Subject: [PATCH] [REF] mrp: ir.attachment handling
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Rev[0] and [1] introduced a versioning on product's attachments through an
ECO creation or through a stat button on the product form view. These revisions
added some fields on the ir attachment model: a many2one and a boolean field
with a default value, and due to concerns over the migration of databases with a
lot of ir.attachment records (like ours), we adapted the implentation in rev[2][3].

We deemed reasonnable to remove the "active" field, as adding an active field not
in the “base” module of the model could impact other modules that did not took into
consideration this field. Also, this field only had a meaning when mrp_plm is
installed, and ir.attachment is used in various places in contexts across Odoo.

So, rev [2] and [3] changed the implementation of the versioning to only link
ir.attachment to an ECO record, forgetting the functionality of archiving directly
through the product form view stat button (without an ECO).

To introduce back this behavior, we chose to create a model to handle the mrp
attachments (named “mrp.document”) which inheritS ir.attachment. This way, we keep
the behavior of ir.attachment and we do not alter the original table.

This commit moved the priority field already set on ir.attachment to mrp.document
and adds the active field that will be used in mrp_plm to archive/versioning purposes.

linked commit:
[0] https://github.com/odoo/enterprise/commit/99138b6711760a7562cb9559663aef6a4208a48f
[1] https://github.com/odoo/enterprise/commit/4f88eb409776c1c4bc8f8d71d845b913c3604a54

[2] https://github.com/odoo/enterprise/commit/7e1c73cfc84cd23b6ba87dab713c3078de16568d
[3] https://github.com/odoo/enterprise/commit/ee1c4b29b8872dac9c525fcc6acdf80b5ef73131
---
 addons/mrp/models/__init__.py           |  2 +-
 addons/mrp/models/ir_attachment.py      | 15 ---------------
 addons/mrp/models/mrp_bom.py            |  2 +-
 addons/mrp/models/mrp_document.py       | 23 +++++++++++++++++++++++
 addons/mrp/security/ir.model.access.csv |  2 ++
 addons/mrp/views/ir_attachment_view.xml | 16 ++++++++++++++--
 6 files changed, 41 insertions(+), 19 deletions(-)
 delete mode 100644 addons/mrp/models/ir_attachment.py
 create mode 100644 addons/mrp/models/mrp_document.py

diff --git a/addons/mrp/models/__init__.py b/addons/mrp/models/__init__.py
index 011d288123a6..a349e45182f3 100644
--- a/addons/mrp/models/__init__.py
+++ b/addons/mrp/models/__init__.py
@@ -1,7 +1,7 @@
 # -*- coding: utf-8 -*-
 # Part of Odoo. See LICENSE file for full copyright and licensing details.
 
-from . import ir_attachment
+from . import mrp_document
 from . import mrp_config_settings
 from . import mrp_bom
 from . import mrp_message
diff --git a/addons/mrp/models/ir_attachment.py b/addons/mrp/models/ir_attachment.py
deleted file mode 100644
index acdf42a59836..000000000000
--- a/addons/mrp/models/ir_attachment.py
+++ /dev/null
@@ -1,15 +0,0 @@
-# -*- coding: utf-8 -*-
-# Part of Odoo. See LICENSE file for full copyright and licensing details.
-
-from odoo import fields, models
-
-
-class IrAttachment(models.Model):
-    _inherit = 'ir.attachment'
-    _order = "priority desc, id desc"
-
-    priority = fields.Selection([
-        ('0', 'Normal'),
-        ('1', 'Low'),
-        ('2', 'High'),
-        ('3', 'Very High')], string="Priority", help="Gives the sequence order when displaying a list of tasks.")
diff --git a/addons/mrp/models/mrp_bom.py b/addons/mrp/models/mrp_bom.py
index fb248b9a59ee..e4fef9f58cda 100644
--- a/addons/mrp/models/mrp_bom.py
+++ b/addons/mrp/models/mrp_bom.py
@@ -262,7 +262,7 @@ class MrpBomLine(models.Model):
         return {
             'name': _('Attachments'),
             'domain': domain,
-            'res_model': 'ir.attachment',
+            'res_model': 'mrp.document',
             'type': 'ir.actions.act_window',
             'view_id': attachment_view.id,
             'views': [(attachment_view.id, 'kanban'), (False, 'form')],
diff --git a/addons/mrp/models/mrp_document.py b/addons/mrp/models/mrp_document.py
new file mode 100644
index 000000000000..01cdc4b78451
--- /dev/null
+++ b/addons/mrp/models/mrp_document.py
@@ -0,0 +1,23 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from odoo import fields, models
+
+
+class MrpDocument(models.Model):
+    """ Extension of ir.attachment only used in MRP to handle archivage
+    and basic versioning.
+    """
+    _name = 'mrp.document'
+    _inherits = {
+        'ir.attachment': 'ir_attachment_id',
+    }
+    _order = "priority desc, id desc"
+
+    ir_attachment_id = fields.Many2one('ir.attachment', string='Related attachment', required=True, ondelete='cascade')
+    active = fields.Boolean('Active', default=True)
+    priority = fields.Selection([
+        ('0', 'Normal'),
+        ('1', 'Low'),
+        ('2', 'High'),
+        ('3', 'Very High')], string="Priority", help='Gives the sequence order when displaying a list of MRP documents.')
diff --git a/addons/mrp/security/ir.model.access.csv b/addons/mrp/security/ir.model.access.csv
index 47a7e677285f..e82ca628e9cf 100644
--- a/addons/mrp/security/ir.model.access.csv
+++ b/addons/mrp/security/ir.model.access.csv
@@ -60,3 +60,5 @@ access_mrp_message_mrp_user,mrp.message,model_mrp_message,group_mrp_user,1,0,0,0
 access_mrp_message_mrp_manager,mrp.message,model_mrp_message,group_mrp_manager,1,1,1,1
 access_stock_move_lots,stock.move.lots,model_stock_move_lots,group_mrp_user,1,1,1,0
 access_stock_move_lots_manager,stock.move.lots,model_stock_move_lots,group_mrp_manager,1,1,1,1
+access_mrp_document_mrp_manager,mrp.document group_user,model_mrp_document,group_mrp_manager,1,1,1,1
+access_mrp_document_mrp_user,mrp.document group_user,model_mrp_document,group_mrp_user,1,1,1,1
\ No newline at end of file
diff --git a/addons/mrp/views/ir_attachment_view.xml b/addons/mrp/views/ir_attachment_view.xml
index 4ac0dd9035a0..49fdf2c6037d 100644
--- a/addons/mrp/views/ir_attachment_view.xml
+++ b/addons/mrp/views/ir_attachment_view.xml
@@ -2,8 +2,8 @@
 <odoo>
         <!-- Files -->
     <record model="ir.ui.view" id="view_document_file_kanban_mrp">
-        <field name="name">ir.attachment kanban.mrp</field>
-        <field name="model">ir.attachment</field>
+        <field name="name">mrp.document kanban.mrp</field>
+        <field name="model">mrp.document</field>
         <field name="arch" type="xml">
             <kanban>
                 <field name="id"/>
@@ -43,4 +43,16 @@
             </kanban>
         </field>
     </record>
+
+    <record id="view_document_form" model="ir.ui.view">
+        <field name="name">mrp.document.form</field>
+        <field name="model">mrp.document</field>
+        <field name="inherit_id" ref="base.view_attachment_form"/>
+        <field name="mode">primary</field>
+        <field name="arch" type="xml">
+          <xpath expr="//field[@name='mimetype']" position="after">
+              <field name="priority" widget="priority"/>
+          </xpath>
+        </field>
+    </record>
 </odoo>
-- 
GitLab