From 6700604f19111bdb88bc612f89ef6ec482563028 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Mart=C3=ADnez?= Date: Fri, 13 Sep 2024 09:08:54 +0200 Subject: [PATCH 1/4] [IMP] maintenance_project: Remove the Project > Administrator permission dependency TT50829 --- maintenance_project/__manifest__.py | 1 - .../data/demo_maintenance_project.xml | 8 +- .../models/maintenance_equipment.py | 24 +++-- .../security/maintenance_project_security.xml | 6 -- .../static/description/index.html | 1 - .../tests/test_maintenance_project.py | 88 ++++++++++++------- .../views/maintenance_equipment_views.xml | 26 ++++-- .../models/maintenance_equipment.py | 4 +- .../tests/test_maintenance_timesheet.py | 7 +- 9 files changed, 93 insertions(+), 72 deletions(-) delete mode 100644 maintenance_project/security/maintenance_project_security.xml diff --git a/maintenance_project/__manifest__.py b/maintenance_project/__manifest__.py index 3ac777887..1051d538e 100644 --- a/maintenance_project/__manifest__.py +++ b/maintenance_project/__manifest__.py @@ -10,7 +10,6 @@ "website": "https://github.com/OCA/maintenance", "depends": ["base_maintenance", "project"], "data": [ - "security/maintenance_project_security.xml", "views/maintenance_equipment_views.xml", "views/maintenance_request_views.xml", "views/project_project_views.xml", diff --git a/maintenance_project/data/demo_maintenance_project.xml b/maintenance_project/data/demo_maintenance_project.xml index d41b6febd..a7f83eaa3 100644 --- a/maintenance_project/data/demo_maintenance_project.xml +++ b/maintenance_project/data/demo_maintenance_project.xml @@ -21,7 +21,6 @@ S/N 1 MODEL1 - @@ -36,7 +35,6 @@ S/N 2 MODEL2 - @@ -53,6 +51,10 @@ S/N 3 MODEL3 - + diff --git a/maintenance_project/models/maintenance_equipment.py b/maintenance_project/models/maintenance_equipment.py index 27c050118..cde8a9def 100644 --- a/maintenance_project/models/maintenance_equipment.py +++ b/maintenance_project/models/maintenance_equipment.py @@ -1,31 +1,27 @@ # Copyright 2019 Solvos Consultoría Informática () +# Copyright 2024 Tecnativa - Víctor Martínez # License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html -from odoo import api, fields, models +from odoo import fields, models class MaintenanceEquipment(models.Model): _inherit = "maintenance.equipment" project_id = fields.Many2one(comodel_name="project.project", ondelete="restrict") - create_project_from_equipment = fields.Boolean(default=True) preventive_default_task_id = fields.Many2one( string="Default Task", comodel_name="project.task" ) - @api.model_create_multi - def create(self, vals_list): - project_obj = self.env["project.project"] - for values in vals_list: - if values.get("create_project_from_equipment"): - new_project = project_obj.create( - self._prepare_project_from_equipment_values(values) - ) - values["project_id"] = new_project.id - return super().create(vals_list) + def action_create_project(self): + self.ensure_one() + if not self.project_id: + self.project_id = self.env["project.project"].create( + self._prepare_project_from_equipment_values() + ) - def _prepare_project_from_equipment_values(self, values): + def _prepare_project_from_equipment_values(self): """ Default project data creation hook """ - return {"name": values.get("name")} + return {"name": self.name} diff --git a/maintenance_project/security/maintenance_project_security.xml b/maintenance_project/security/maintenance_project_security.xml deleted file mode 100644 index 14b6f309a..000000000 --- a/maintenance_project/security/maintenance_project_security.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - diff --git a/maintenance_project/static/description/index.html b/maintenance_project/static/description/index.html index e52cd904f..32a5852f4 100644 --- a/maintenance_project/static/description/index.html +++ b/maintenance_project/static/description/index.html @@ -1,4 +1,3 @@ - diff --git a/maintenance_project/tests/test_maintenance_project.py b/maintenance_project/tests/test_maintenance_project.py index 6ed884f0e..68f659b6c 100644 --- a/maintenance_project/tests/test_maintenance_project.py +++ b/maintenance_project/tests/test_maintenance_project.py @@ -1,22 +1,31 @@ # Copyright 2019 Solvos Consultor??a Inform??tica () +# Copyright 2024 Tecnativa - Víctor Martínez # License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html -import odoo.tests.common as test_common +from odoo.tests import Form, new_test_user +from odoo.tests.common import users +from odoo.addons.base.tests.common import BaseCommon -class TestMaintenanceProject(test_common.TransactionCase): + +class TestMaintenanceProject(BaseCommon): @classmethod def setUpClass(cls): - super(TestMaintenanceProject, cls).setUpClass() - + super().setUpClass() cls.cron = cls.env.ref("maintenance.maintenance_requests_cron") cls.project1 = cls.env["project.project"].create({"name": "My project"}) - cls.project_demo1 = cls.env.ref("maintenance_project.project_project_1") - + cls.project_demo = cls.env.ref("maintenance_project.project_project_1") + new_test_user( + cls.env, login="test-user", groups="maintenance.group_equipment_manager" + ) + new_test_user( + cls.env, + login="test-project_manager-user", + groups="maintenance.group_equipment_manager,project.group_project_manager", + ) cls.equipment1 = cls.env["maintenance.equipment"].create( { "name": "My equipment", - "create_project_from_equipment": True, "maintenance_team_id": cls.env.ref( "maintenance.equipment_team_metrology" ).id, @@ -27,50 +36,64 @@ def setUpClass(cls): cls.equipment2 = cls.env["maintenance.equipment"].create( { "name": "My equipment without project", - "create_project_from_equipment": False, } ) cls.equipment3 = cls.env["maintenance.equipment"].create( { "name": "My equipment with related project", - "create_project_from_equipment": False, "project_id": cls.project1.id, } ) + cls.equipment_demo = cls.env.ref("maintenance_project.equipment_3") - cls.equipment_demo1 = cls.env.ref("maintenance_project.equipment_1") - cls.equipment_demo2 = cls.env.ref("maintenance_project.equipment_2") - cls.equipment_demo3 = cls.env.ref("maintenance_project.equipment_3") - - def test_maintenance_equipment_project(self): - self.assertEqual(self.equipment1.name, self.equipment1.project_id.name) + def test_maintenance_equipment_project_misc(self): + self.assertFalse(self.equipment1.project_id) self.assertFalse(self.equipment2.project_id) self.assertEqual(self.equipment3.project_id, self.project1) - self.assertEqual( - self.equipment_demo3.name, self.equipment_demo3.project_id.name + self.assertEqual(self.equipment_demo.name, self.equipment_demo.project_id.name) + + @users("test-project_manager-user") + def test_maintenance_equipment_project_admin(self): + equipment_a = self.env["maintenance.equipment"].create( + { + "name": "Test equipment A", + } + ) + self.assertFalse(equipment_a.project_id) + equipment_a.action_create_project() + self.assertTrue(equipment_a.project_id) + self.assertEqual(equipment_a.name, equipment_a.project_id.name) + equipment_b = self.env["maintenance.equipment"].create( + { + "name": "Test equipment b", + "project_id": self.project1.id, + } ) + self.assertEqual(equipment_b.project_id, self.project1) + equipment_b.action_create_project() + self.assertEqual(equipment_b.project_id, self.project1) def test_project_equipment_count(self): + self.equipment1.action_create_project() self.assertEqual(self.project1.equipment_count, 1) self.assertEqual(self.equipment1.project_id.equipment_count, 1) - self.assertEqual(self.project_demo1.equipment_count, 2) - self.assertEqual(self.equipment_demo3.project_id.equipment_count, 1) + self.assertEqual(self.project_demo.equipment_count, 2) + self.assertEqual(self.equipment_demo.project_id.equipment_count, 1) - def test_request_onchange_equipment(self): - req1 = self.env["maintenance.request"].new({"name": "My test request #1"}) - self.assertFalse(req1.project_id) - req1.equipment_id = self.equipment1 - req1.onchange_equipment_id() - self.assertEqual(req1.project_id, self.equipment1.project_id) - - req2 = self.env["maintenance.request"].new({"name": "My test request #2"}) - req2.equipment_id = self.equipment2 - req2.onchange_equipment_id() - self.assertFalse(req2.project_id) + @users("test-user") + def test_request_equipment(self): + request_form_1 = Form(self.env["maintenance.request"]) + request_form_1.name = "My test request #1" + self.assertFalse(request_form_1.project_id) + request_form_1.equipment_id = self.equipment1 + self.assertEqual(request_form_1.project_id, self.equipment1.project_id) + request_form_2 = Form(self.env["maintenance.request"]) + request_form_2.name = "My test request #2" + request_form_2.equipment_id = self.equipment2 + self.assertFalse(request_form_2.project_id) def test_generate_requests(self): self.cron.method_direct_trigger() - generated_requests = self.env["maintenance.request"].search( [("project_id", "!=", False)] ) @@ -79,12 +102,11 @@ def test_generate_requests(self): self.assertEqual(req.task_id, req.equipment_id.preventive_default_task_id) self.assertEqual(req.project_id.maintenance_request_count, 1) - def test_action_views(self): + def test_project_action_views(self): act1 = self.project1.action_view_equipment_ids() self.assertEqual(act1["domain"][0][2], self.project1.id) self.assertEqual(act1["context"]["default_project_id"], self.project1.id) self.assertFalse(act1["context"]["default_create_project_from_equipment"]) - act2 = self.project1.action_view_maintenance_request_ids() self.assertEqual(act2["domain"][0][2], self.project1.id) self.assertEqual(act2["context"]["default_project_id"], self.project1.id) diff --git a/maintenance_project/views/maintenance_equipment_views.xml b/maintenance_project/views/maintenance_equipment_views.xml index 554be3b94..48736f853 100644 --- a/maintenance_project/views/maintenance_equipment_views.xml +++ b/maintenance_project/views/maintenance_equipment_views.xml @@ -37,14 +37,24 @@ - - + + diff --git a/maintenance_timesheet/models/maintenance_equipment.py b/maintenance_timesheet/models/maintenance_equipment.py index a5753ba03..4b63546b6 100644 --- a/maintenance_timesheet/models/maintenance_equipment.py +++ b/maintenance_timesheet/models/maintenance_equipment.py @@ -7,7 +7,7 @@ class MaintenanceEquipment(models.Model): _inherit = "maintenance.equipment" - def _prepare_project_from_equipment_values(self, values): - data = super()._prepare_project_from_equipment_values(values) + def _prepare_project_from_equipment_values(self): + data = super()._prepare_project_from_equipment_values() data["allow_timesheets"] = True return data diff --git a/maintenance_timesheet/tests/test_maintenance_timesheet.py b/maintenance_timesheet/tests/test_maintenance_timesheet.py index 9760606ac..a8a4c6015 100644 --- a/maintenance_timesheet/tests/test_maintenance_timesheet.py +++ b/maintenance_timesheet/tests/test_maintenance_timesheet.py @@ -102,7 +102,6 @@ def test_action_view_timesheet_ids(self): self.assertFalse(act1["context"]["readonly_employee_id"]) def test_prepare_project_from_equipment_values(self): - data = self.env["maintenance.equipment"]._prepare_project_from_equipment_values( - {"name": "my name"} - ) - self.assertTrue(data["allow_timesheets"]) + equipment = self.env["maintenance.equipment"].create({"name": "Test equipment"}) + equipment.action_create_project() + self.assertTrue(equipment.project_id.allow_timesheets) From 8098005c97e1b1f656a53d05a36a40c1fd3146ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Mart=C3=ADnez?= Date: Mon, 16 Sep 2024 09:42:48 +0200 Subject: [PATCH 2/4] [IMP] maintenance_project: Add groups="project.group_project_user" to project/task fields TT50829 --- .../tests/test_maintenance_project.py | 4 +++- .../views/maintenance_equipment_views.xml | 16 ++++++++++------ .../views/maintenance_request_views.xml | 16 +++++++++++----- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/maintenance_project/tests/test_maintenance_project.py b/maintenance_project/tests/test_maintenance_project.py index 68f659b6c..90218bf06 100644 --- a/maintenance_project/tests/test_maintenance_project.py +++ b/maintenance_project/tests/test_maintenance_project.py @@ -16,7 +16,9 @@ def setUpClass(cls): cls.project1 = cls.env["project.project"].create({"name": "My project"}) cls.project_demo = cls.env.ref("maintenance_project.project_project_1") new_test_user( - cls.env, login="test-user", groups="maintenance.group_equipment_manager" + cls.env, + login="test-user", + groups="maintenance.group_equipment_manager,project.group_project_user", ) new_test_user( cls.env, diff --git a/maintenance_project/views/maintenance_equipment_views.xml b/maintenance_project/views/maintenance_equipment_views.xml index 48736f853..3707413f0 100644 --- a/maintenance_project/views/maintenance_equipment_views.xml +++ b/maintenance_project/views/maintenance_equipment_views.xml @@ -5,7 +5,7 @@ - + @@ -14,7 +14,7 @@ - + @@ -23,10 +23,13 @@ - + -
+
Project:
@@ -38,11 +41,11 @@ -