From 9c5d7adf3d37ece4685407c8ac866bb753467a6e Mon Sep 17 00:00:00 2001 From: damr Date: Thu, 17 Nov 2022 10:54:33 +0000 Subject: [PATCH] [IMP] project: add default personnal stage to user Currently, when a db is loaded without demo data, or that a user has no task assigned and no personnal stage, the view 'My tasks' is empty. The purpose of this commit is to assign default personnal stage to a user in these case to ease the understanding of new user of what the view can be used for. This commit : - add default personnal stage to user the first time he clicks on the 'My tasks' menu if the user has no personnal stage and no task are assigned to him. task-3047496 closes odoo/odoo#105940 Signed-off-by: Xavier Bol (xbo) --- addons/project/models/project.py | 9 ++++++++ addons/project/tests/test_personal_stages.py | 22 ++++++++++++++++++++ addons/project/views/project_views.xml | 11 +++++++++- 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/addons/project/models/project.py b/addons/project/models/project.py index 0c858d7f054..46262d3e874 100644 --- a/addons/project/models/project.py +++ b/addons/project/models/project.py @@ -2373,6 +2373,15 @@ class Task(models.Model): res.update(super(Task, leftover)._notify_get_reply_to(default=default)) return res + def _ensure_personal_stages(self): + user = self.env.user + ProjectTaskTypeSudo = self.env['project.task.type'].sudo() + # In the case no stages have been found, we create the default stages for the user + if not ProjectTaskTypeSudo.search_count([('user_id', '=', user.id)], limit=1): + ProjectTaskTypeSudo.with_context(lang=user.lang, default_project_id=False).create( + self.with_context(lang=user.lang)._get_default_personal_stage_create_vals(user.id) + ) + def email_split(self, msg): email_list = tools.email_split((msg.get('to') or '') + ',' + (msg.get('cc') or '')) # check left-part is not already an alias diff --git a/addons/project/tests/test_personal_stages.py b/addons/project/tests/test_personal_stages.py index 06cd7164a89..bdc929e0bf4 100644 --- a/addons/project/tests/test_personal_stages.py +++ b/addons/project/tests/test_personal_stages.py @@ -90,6 +90,28 @@ class TestPersonalStages(TestProjectCommon): self.assertEqual(1, total_stage_0) self.assertEqual(1, total_stage_1) + def test_default_personal_stage(self): + user_without_stage, user_with_stages = self.env['res.users'].create([{ + 'login': 'test_no_stage', + 'name': "Test User without stage", + }, { + 'login': 'test_stages', + 'name': "Test User with stages", + }]) + personal_stage = self.env['project.task.type'].create({ + 'name': 'personal stage', + 'user_id': user_with_stages.id, + }) + ProjectTaskTypeSudo = self.env['project.task.type'].sudo() + # ensure that a user without personal stage is getting the default stages + self.task_1.with_user(user_without_stage)._ensure_personal_stages() + stages = ProjectTaskTypeSudo.search([('user_id', '=', user_without_stage.id)]) + self.assertEqual(len(stages), 7, "As this user had no personal stage, the default ones should have been created for him") + # ensure that the user's personal stages are not changing if the user already had some + self.task_1.with_user(user_with_stages)._ensure_personal_stages() + stages = ProjectTaskTypeSudo.search([('user_id', '=', user_with_stages.id)]) + self.assertEqual(stages, personal_stage, "As this user already had a personal stage, none should be added") + @tagged('-at_install', 'post_install') class TestPersonalStageTour(HttpCase, TestProjectCommon): diff --git a/addons/project/views/project_views.xml b/addons/project/views/project_views.xml index 90331343c0a..cfbd1a9d345 100644 --- a/addons/project/views/project_views.xml +++ b/addons/project/views/project_views.xml @@ -1770,8 +1770,17 @@ + + menu view My Tasks + + code + + model._ensure_personal_stages(); action = env["ir.actions.actions"]._for_xml_id("project.action_view_my_task") + + + + action="action_server_view_my_task" sequence="2" groups="group_project_user"/>