From 7e35518129e351f8278b7d6946984be055f4f6da Mon Sep 17 00:00:00 2001 From: Cedric Prieels Date: Mon, 17 Jan 2022 16:31:40 +0000 Subject: [PATCH] [IMP] project: improve tags management In big databases, the number of tags becomes rapidly huge. This makes it very hard to find revelant tags. In practice, the same few tags are usually used in a given project. This commit: - Limits the search of tags to the project's tags of the task as well as to the tasks' tags of the task's project. - Prevents creating case sensitive variants of existing tags. - Reuse existing tags from other projects and other projects' tasks when the user `creates` (or at least thinks creating as the _name_search would not return the searched tag) a new tag in his project. task-2735833 closes odoo/odoo#82925 Related: odoo/enterprise#23814 Signed-off-by: Laurent Stukkens (ltu) --- addons/project/models/project.py | 22 +++++++++++++++++++++- addons/project/views/project_views.xml | 4 ++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/addons/project/models/project.py b/addons/project/models/project.py index 6c67bc69a2d..00faf9fed40 100644 --- a/addons/project/models/project.py +++ b/addons/project/models/project.py @@ -11,7 +11,7 @@ from random import randint from odoo import api, Command, fields, models, tools, SUPERUSER_ID, _ from odoo.exceptions import UserError, ValidationError, AccessError from odoo.tools import format_amount -from odoo.osv.expression import OR +from odoo.osv.expression import OR, AND from odoo.tools.misc import get_lang from .project_task_recurrence import DAYS, WEEKS @@ -2233,7 +2233,27 @@ class ProjectTags(models.Model): name = fields.Char('Name', required=True, translate=True) color = fields.Integer(string='Color', default=_get_default_color) + project_ids = fields.Many2many('project.project', 'project_project_project_tags_rel', string='Projects') + task_ids = fields.Many2many('project.task', string='Tasks') _sql_constraints = [ ('name_uniq', 'unique (name)', "Tag name already exists!"), ] + + @api.model + def _name_search(self, name='', args=None, operator='ilike', limit=100, name_get_uid=None): + domain = args + if 'project_id' in self.env.context: + project_id = self.env.context.get('project_id') + domain = AND([ + domain, + ['|', ('task_ids.project_id', '=', project_id), ('project_ids', 'in', project_id)] + ]) + return super()._name_search(name, domain, operator, limit, name_get_uid) + + @api.model + def name_create(self, name): + existing_tag = self.search([('name', '=ilike', name.strip())], limit=1) + if existing_tag: + return existing_tag.name_get()[0] + return super().name_create(name) diff --git a/addons/project/views/project_views.xml b/addons/project/views/project_views.xml index 528be4cdb08..8dc9e751a2d 100644 --- a/addons/project/views/project_views.xml +++ b/addons/project/views/project_views.xml @@ -923,7 +923,7 @@ - + @@ -1210,7 +1210,7 @@ - +