[IMP] project: always assign current user to private task

This commit ensures that, when creating a private task, the current user
is always part of the assignees.

This commit also adds tests on the different `project.task` creation scenari
to ensure it gets not broken in the future.

task-2818486

closes odoo/odoo#89041

Signed-off-by: Laurent Stukkens (ltu) <ltu@odoo.com>
This commit is contained in:
Prakash Prajapati
2022-06-15 19:17:01 +02:00
committed by Laurent Stukkens (LTU)
parent 47f453c1d4
commit afe41c4e88
2 changed files with 37 additions and 3 deletions
+6
View File
@@ -1764,6 +1764,8 @@ class Task(models.Model):
vals['analytic_account_id'] = project.analytic_account_id.id
if project.analytic_tag_ids:
vals['analytic_tag_ids'] = [Command.set(project.analytic_tag_ids.ids)]
else:
vals['user_ids'] = [Command.link(self.env.user.id)]
return vals
@@ -1874,6 +1876,10 @@ class Task(models.Model):
# user_ids change: update date_assign
if vals.get('user_ids'):
vals['date_assign'] = fields.Datetime.now()
if not project_id:
user_ids = self._fields['user_ids'].convert_to_cache(vals.get('user_ids', []), self)
if self.env.user.id not in user_ids:
vals['user_ids'] = [Command.set(list(user_ids) + [self.env.user.id])]
# Stage change: Update date_end if folded stage and date_last_stage_update
if vals.get('stage_id'):
vals.update(self.update_date_end(vals['stage_id']))
+31 -3
View File
@@ -366,9 +366,37 @@ class TestAccessRightsPrivateTask(TestAccessRights):
self.assertEqual(vals[0]['name'], private_task.name)
@users('Project user')
def test_project_user_cannot_create_private_task_for_another_user(self):
with self.assertRaises(AccessError):
self.create_private_task('test private for another user', self.env.user, user_ids=[Command.set(self.user_projectuser.ids)])
def test_project_user_can_create_private_task_for_another_user(self):
self.create_private_task('Private task', user_ids=[Command.set(self.user_projectuser.ids)])
@users('Project user')
def test_project_current_user_is_added_in_private_task_assignees(self):
task_values = {'name': 'Private task'}
my_private_task = self.env['project.task'].create(task_values)
self.assertEqual(my_private_task.user_ids, self.env.user, 'When no assignee is set on a private task, the task should be assigned to the current user.')
user_projectuser_private_task = self.env['project.task'].create({**task_values, 'user_ids': [Command.set(self.user_projectuser.ids)]})
self.assertTrue(self.env.user in user_projectuser_private_task.user_ids, 'When creating a private task for another user, the current user should be added to the assignees.')
@users('Project user')
def test_project_current_user_is_added_in_task_assignees_when_project_id_is_set(self):
task_values = {'name': 'Private task', 'project_id': self.project_pigs.id, 'user_ids': [Command.set(self.user_projectuser.ids)]}
user_projectuser_task = self.env['project.task'].create(task_values)
self.assertFalse(self.env.user in user_projectuser_task.user_ids, "When creating a task that has a project for another user, the current user should not be added to the assignees.")
@users('Project user')
def test_project_current_user_is_set_as_assignee_in_task_when_project_id_is_set_with_no_assignees(self):
task = self.env['project.task'].create({'name': 'Private task', 'project_id': self.project_pigs.id})
self.assertEqual(task.user_ids, self.env.user, "When creating a task that has a project without assignees, the task will be assigned to the current user if no default_project_id is provided in the context (which is handled in _default_personal_stage_type_id).")
@users('Project user')
def test_project_current_user_is_not_added_in_private_task_assignees_when_default_project_id_is_in_the_context(self):
task_values = {'name': 'Private task'}
context = {'default_project_id': self.project_pigs.id}
ProjectTask_with_default_project_id = self.env['project.task'].with_context(context)
task = ProjectTask_with_default_project_id.create(task_values)
self.assertNotEqual(task.user_ids, self.env.user, "When creating a task without assignees and providing default_project_id in the context, the task should not be assigned to the current user.")
user_projectuser_task = ProjectTask_with_default_project_id.create({**task_values, 'user_ids': [Command.set(self.user_projectuser.ids)]})
self.assertFalse(self.env.user in user_projectuser_task.user_ids, "When creating a task for another user and providing default_project_id in the context, the current user should not be added to the assignees.")
@users('Project user')
def test_project_user_cannot_write_private_task_of_another_user(self):