Before this commit, the translation is not done in lazy, so for each stat button shown in the right side panel of the project update, a query is done to find the correct translation based on the language of the user. It means, the translation is costly and need to be done in lazy to reduce the time for the translation of those stat buttons. This commit uses the `_lt` to translate in lazy mode those stat buttons. By doing that, the execution time is reduced from 750 ms to 250 ms. X-original-commit: 95fe018 Part-of: odoo/odoo#85660 Co-authored-by: Yannick Tivisse <yti@odoo.com> Co-authored-by: Laurent Stukkens (LTU) <ltu@odoo.com>
60 lines
2.4 KiB
Python
60 lines
2.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
|
|
|
from odoo import api, fields, models, _, _lt
|
|
|
|
class Project(models.Model):
|
|
_inherit = 'project.project'
|
|
|
|
expenses_count = fields.Integer('# Expenses', compute='_compute_expenses_count', groups='hr_expense.group_hr_expense_team_approver')
|
|
|
|
@api.depends('analytic_account_id')
|
|
def _compute_expenses_count(self):
|
|
expenses_data = self.env['hr.expense']._read_group([
|
|
('analytic_account_id', '!=', False),
|
|
('analytic_account_id', 'in', self.analytic_account_id.ids)
|
|
],
|
|
['analytic_account_id'], ['analytic_account_id'])
|
|
mapped_data = {data['analytic_account_id'][0]: data['analytic_account_id_count'] for data in expenses_data}
|
|
for project in self:
|
|
project.expenses_count = mapped_data.get(project.analytic_account_id.id, 0)
|
|
|
|
# ----------------------------
|
|
# Actions
|
|
# ----------------------------
|
|
|
|
def action_open_project_expenses(self):
|
|
expenses = self.env['hr.expense'].search([
|
|
('analytic_account_id', '!=', False),
|
|
('analytic_account_id', 'in', self.analytic_account_id.ids)
|
|
])
|
|
action = self.env["ir.actions.actions"]._for_xml_id("hr_expense.hr_expense_actions_all")
|
|
action.update({
|
|
'display_name': _('Expenses'),
|
|
'views': [[False, 'tree'], [False, 'form'], [False, 'kanban'], [False, 'graph'], [False, 'pivot']],
|
|
'context': {'default_analytic_account_id': self.analytic_account_id.id},
|
|
'domain': [('id', 'in', expenses.ids)]
|
|
})
|
|
if(len(expenses) == 1):
|
|
action["views"] = [[False, 'form']]
|
|
action["res_id"] = expenses.id
|
|
return action
|
|
|
|
# ----------------------------
|
|
# Project Updates
|
|
# ----------------------------
|
|
|
|
def _get_stat_buttons(self):
|
|
buttons = super(Project, self)._get_stat_buttons()
|
|
if self.user_has_groups('hr_expense.group_hr_expense_team_approver'):
|
|
buttons.append({
|
|
'icon': 'money',
|
|
'text': _lt('Expenses'),
|
|
'number': self.expenses_count,
|
|
'action_type': 'object',
|
|
'action': 'action_open_project_expenses',
|
|
'show': self.expenses_count > 0,
|
|
'sequence': 33,
|
|
})
|
|
return buttons
|