From d3efda05fda0cb11939d4d8a3f26acc72c9d1435 Mon Sep 17 00:00:00 2001 From: Yannick Tivisse Date: Mon, 27 Nov 2017 10:15:17 +0100 Subject: [PATCH] [FIX] (project_)pad: Don't create/copy pads on tasks that don't use them Purpose ======= Currently if a project doesn't use pads, the created and duplicated tasks will initialize/copy them. That could be a real issue when migrating customers if we create a task for all the existing issues, since the models have been merged. Specification ============= Avoid this falsy behavior. We're forced to crappy fix with a context key as the project.task model 'inherit' from pad.common. --- addons/pad/models/pad.py | 9 +++++++-- addons/pad_project/models/project.py | 16 +++++++++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/addons/pad/models/pad.py b/addons/pad/models/pad.py index 43a1aaecafa..60eaa5fc2bb 100644 --- a/addons/pad/models/pad.py +++ b/addons/pad/models/pad.py @@ -106,6 +106,8 @@ class PadCommon(models.AbstractModel): # In case the pad is created programmatically, the content is not filled in yet since it is # normally initialized by the JS layer + if self.env.context.get('pad_no_create', False): + return pad for k, field in self._fields.items(): if hasattr(field, 'pad_content_field') and k not in vals: ctx = { @@ -141,6 +143,9 @@ class PadCommon(models.AbstractModel): default = {} for k, field in self._fields.items(): if hasattr(field, 'pad_content_field'): - pad = self.pad_generate_url() - default[k] = pad.get('url') + if self.env.context.get('pad_no_create', False): + default[k] = '' + else: + pad = self.pad_generate_url() + default[k] = pad.get('url') return super(PadCommon, self).copy(default) diff --git a/addons/pad_project/models/project.py b/addons/pad_project/models/project.py index 113a32b9b68..eae27a4f6a7 100644 --- a/addons/pad_project/models/project.py +++ b/addons/pad_project/models/project.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. -from odoo import fields, models +from odoo import api, fields, models class ProjectTask(models.Model): @@ -11,6 +11,20 @@ class ProjectTask(models.Model): description_pad = fields.Char('Pad URL', pad_content_field='description') use_pad = fields.Boolean(related="project_id.use_pads", string="Use collaborative pad") + @api.model + def create(self, vals): + # When using quick create, the project_id is in the context, not in the vals + project_id = vals.get('project_id', False) or self.default_get(['project_id'])['project_id'] + if not self.env['project.project'].browse(project_id).use_pads: + self = self.with_context(pad_no_create=True) + return super(ProjectTask, self).create(vals) + + @api.multi + def copy(self, default=None): + if not self.use_pad: + self = self.with_context(pad_no_create=True) + return super(ProjectTask, self).copy(default) + class ProjectProject(models.Model): _inherit = "project.project"