From 74c80cee6c435210d91752d014f8b5a8dac97fb5 Mon Sep 17 00:00:00 2001 From: "Manushi Shah (mash)" Date: Tue, 27 Jun 2023 14:27:59 +0530 Subject: [PATCH] [FIX] project: resolving validation error in quick create Steps: - Open Project - Go to Tasks > My Tasks or All Tasks - Create New Task - Create Project from Quick Create - Clicking on Save, will give a Validation Error Issue: - Validation Error is raised and thus, we aren't able to add the project and thus creating a task. Cause: - Due to the addition of context, the default_type_ids isn't obtained, and thus the SQL error occurs as the name of the task stage isn't set which is a mandatory field. Fix: - removing the context from the form view of Quick Create and set the stage closes odoo/odoo#145599 Task: 3378510 X-original-commit: 83119d5de49ae69d08a0f798453d9c5c56b2276a Signed-off-by: Xavier Bol (xbo) --- addons/project/models/project_task.py | 18 +++++++++++------- .../tests/test_project_task_quick_create.py | 11 +++++++++++ addons/project/views/project_task_views.xml | 1 - 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/addons/project/models/project_task.py b/addons/project/models/project_task.py index 5ee94a979e0..b9fca0b49a3 100644 --- a/addons/project/models/project_task.py +++ b/addons/project/models/project_task.py @@ -830,6 +830,15 @@ class Task(models.Model): if field not in self.SELF_WRITABLE_FIELDS: raise AccessError(_('You have not write access of %s field.', field)) + def _set_stage_on_project_from_task(self): + stage_ids_per_project = defaultdict(list) + for task in self: + if task.stage_id and task.stage_id not in task.project_id.type_ids and task.stage_id.id not in stage_ids_per_project[task.project_id]: + stage_ids_per_project[task.project_id].append(task.stage_id.id) + + for project, stage_ids in stage_ids_per_project.items(): + project.write({'type_ids': [Command.link(stage_id) for stage_id in stage_ids]}) + def _load_records_create(self, vals_list): for vals in vals_list: if vals.get('recurring_task'): @@ -840,13 +849,6 @@ class Task(models.Model): if project_id: self = self.with_context(default_project_id=project_id) tasks = super()._load_records_create(vals_list) - stage_ids_per_project = defaultdict(list) - for task in tasks: - if task.stage_id and task.stage_id not in task.project_id.type_ids and task.stage_id.id not in stage_ids_per_project[task.project_id]: - stage_ids_per_project[task.project_id].append(task.stage_id.id) - - for project, stage_ids in stage_ids_per_project.items(): - project.write({'type_ids': [Command.link(stage_id) for stage_id in stage_ids]}) return tasks @@ -943,6 +945,8 @@ class Task(models.Model): # if the portal user could really create the tasks based on the ir rule. tasks.with_user(self.env.user).check_access_rule('create') current_partner = self.env.user.partner_id + if tasks.project_id: + tasks._set_stage_on_project_from_task() for task in tasks: if task.project_id.privacy_visibility == 'portal': task._portal_ensure_token() diff --git a/addons/project/tests/test_project_task_quick_create.py b/addons/project/tests/test_project_task_quick_create.py index dc848b62812..e3c95885840 100644 --- a/addons/project/tests/test_project_task_quick_create.py +++ b/addons/project/tests/test_project_task_quick_create.py @@ -58,3 +58,14 @@ class TestProjectTaskQuickCreate(TestProjectCommon): task = task_form.save() results = (task.name, len(task.tag_ids), len(task.user_ids), task.priority, task.allocated_hours) self.assertEqual(results, (expression, 0, 0, '0', 0)) + + def test_set_stage_on_project_from_task(self): + new_stage = self.env['project.task.type'].create({ + 'name': 'New Stage', + }) + self.env['project.task'].create({ + 'name': 'Test Task', + 'stage_id': new_stage.id, + 'project_id': self.project_pigs.id, + }) + self.assertEqual(self.project_pigs.type_ids, new_stage, "Task stage is not set in project") diff --git a/addons/project/views/project_task_views.xml b/addons/project/views/project_task_views.xml index 69e28c1bfc0..786bf182555 100644 --- a/addons/project/views/project_task_views.xml +++ b/addons/project/views/project_task_views.xml @@ -507,7 +507,6 @@ placeholder="Private" class="o_project_task_project_field" domain="[('type_ids', 'in', context['default_stage_id'])] if context.get('default_stage_id') else []" - context="{'default_type_ids': [(4, context.get('default_stage_id', False))]}" />