From e25d0b38913c276e67e415e76af5720bb99fc04c Mon Sep 17 00:00:00 2001 From: Kinjal Mehta Date: Wed, 27 Dec 2017 16:20:10 +0530 Subject: [PATCH] [IMP] project: Remove a shared stage if no linked record inside this project Purpose ======= Projects may have share columns. If we want to delete a stage on a project there's several use case Specification ============= Different use cases: - The stage is shared - There's no task in this stage for this project: Remove the stage from this project, don't delete it - There's a task in this stage (Active or not): Raise an error from the ondelete restrict - The stage is not shared - There's no task in this stage: Delete the stage (unlink) - There's a task in this stage (Active or not): Raise an error from the ondelete restrict --- addons/project/models/project.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/addons/project/models/project.py b/addons/project/models/project.py index 47525ea6820..0bdbbf2fab6 100644 --- a/addons/project/models/project.py +++ b/addons/project/models/project.py @@ -52,6 +52,18 @@ class ProjectTaskType(models.Model): " * A good feedback from the customer will update the kanban state to 'ready for the new stage' (green bullet).\n" " * A medium or a bad feedback will set the kanban state to 'blocked' (red bullet).\n") + @api.multi + def unlink(self): + stages = self + default_project_id = self.env.context.get('default_project_id') + if default_project_id: + shared_stages = self.filtered(lambda x: len(x.project_ids) > 1 and default_project_id in x.project_ids.ids) + tasks = self.env['project.task'].with_context(active_test=False).search([('project_id', '=', default_project_id), ('stage_id', 'in', self.ids)]) + if shared_stages and not tasks: + shared_stages.write({'project_ids': [(3, default_project_id)]}) + stages = self.filtered(lambda x: x not in shared_stages) + return super(ProjectTaskType, stages).unlink() + class Project(models.Model): _name = "project.project"