From 60fdcacc0ab9f61e173c7e05acadfa3270f41f39 Mon Sep 17 00:00:00 2001 From: "Laurent Stukkens (LTU)" Date: Thu, 18 Nov 2021 12:04:13 +0000 Subject: [PATCH] [FIX] hr_timesheet: prevent perf issues in project kanban Prior to this commit the total_timesheet_time field is used in the kanban view in order to decide whether or not a menu entry has to be displayed. This commit introduce a new compute non stored field in order to count the number of timesheets and use that info in order to decide whether or not the menu entry has to be displayed. closes odoo/odoo#80080 X-original-commit: 1dce6aea852b1496bdcfd3b98c4da531d2646ad0 Signed-off-by: Laurent Stukkens (ltu) --- addons/hr_timesheet/models/project.py | 12 ++++++++++++ addons/hr_timesheet/views/project_views.xml | 4 ++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/addons/hr_timesheet/models/project.py b/addons/hr_timesheet/models/project.py index a79273a59bf..ed6ba246bcf 100644 --- a/addons/hr_timesheet/models/project.py +++ b/addons/hr_timesheet/models/project.py @@ -38,6 +38,7 @@ class Project(models.Model): ) timesheet_ids = fields.One2many('account.analytic.line', 'project_id', 'Associated Timesheets') + timesheet_count = fields.Boolean(compute="_compute_timesheet_count") timesheet_encode_uom_id = fields.Many2one('uom.uom', related='company_id.timesheet_encode_uom_id') total_timesheet_time = fields.Integer( compute='_compute_total_timesheet_time', @@ -130,6 +131,17 @@ class Project(models.Model): total_time *= project.timesheet_encode_uom_id.factor project.total_timesheet_time = int(round(total_time)) + @api.depends('timesheet_ids') + def _compute_timesheet_count(self): + timesheet_read_group = self.env['account.analytic.line'].read_group( + [('project_id', 'in', self.ids)], + ['project_id'], + ['project_id'] + ) + timesheet_project_map = {project_info['project_id'][0]: project_info['project_id_count'] for project_info in timesheet_read_group} + for project in self: + project.timesheet_count = timesheet_project_map.get(project.id, 0) + @api.model_create_multi def create(self, vals_list): """ Create an analytic account if project allow timesheet and don't provide one diff --git a/addons/hr_timesheet/views/project_views.xml b/addons/hr_timesheet/views/project_views.xml index 8210a9ed4b3..4998ecadc77 100644 --- a/addons/hr_timesheet/views/project_views.xml +++ b/addons/hr_timesheet/views/project_views.xml @@ -274,13 +274,13 @@ - + -