From 9b53115345e988b5ac49ca5728d4de2dd6af56cd Mon Sep 17 00:00:00 2001 From: Prakash Prajapati Date: Thu, 24 Jun 2021 09:00:15 +0000 Subject: [PATCH] [IMP] hr_timesheet: refactor _apply_time_label method The purpose of the commit is in order to avoid code duplication and ease maintenance, the _apply_time_label method has been centralized in hr.timesheet which makes more sense since the relation to the timesheet encoding unit. It can now be used in other models by providing the right model name. closes odoo/odoo#72679 Taskid: 2451722 Related: odoo/upgrade#2499 Related: odoo/enterprise#18621 Signed-off-by: LTU-Odoo --- addons/hr_timesheet/models/hr_timesheet.py | 11 +++++++++++ addons/hr_timesheet/models/project.py | 16 ++-------------- addons/hr_timesheet/report/project_report.py | 2 +- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/addons/hr_timesheet/models/hr_timesheet.py b/addons/hr_timesheet/models/hr_timesheet.py index e7d9ff2e348..cbd7562e531 100644 --- a/addons/hr_timesheet/models/hr_timesheet.py +++ b/addons/hr_timesheet/models/hr_timesheet.py @@ -134,6 +134,17 @@ class AccountAnalyticLine(models.Model): node.set('string', _('Duration (%s)') % (re.sub(r'[\(\)]', '', encoding_uom.name or ''))) return etree.tostring(doc, encoding='unicode') + @api.model + def _apply_time_label(self, view_arch, related_model): + doc = etree.XML(view_arch) + Model = self.env[related_model] + encoding_uom = self.env.company.timesheet_encode_uom_id + for node in doc.xpath("//field[@widget='timesheet_uom'][not(@string)] | //field[@widget='timesheet_uom_no_toggle'][not(@string)]"): + name_with_uom = re.sub(_('Hours') + "|Hours", encoding_uom.name or '', Model._fields[node.get('name')]._description_string(self.env), flags=re.IGNORECASE) + node.set('string', name_with_uom) + + return etree.tostring(doc, encoding='unicode') + def _timesheet_get_portal_domain(self): if self.env.user.has_group('hr_timesheet.group_hr_timesheet_user'): # Then, he is internal user, and we take the domain for this current user diff --git a/addons/hr_timesheet/models/project.py b/addons/hr_timesheet/models/project.py index 3c040734b14..b79e333ee2c 100644 --- a/addons/hr_timesheet/models/project.py +++ b/addons/hr_timesheet/models/project.py @@ -1,7 +1,5 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. -import re -from lxml import etree from odoo import models, fields, api, _ from odoo.exceptions import UserError, ValidationError, RedirectWarning @@ -243,20 +241,10 @@ class Task(models.Model): result['arch'] = self.env['account.analytic.line']._apply_timesheet_label(result['arch']) if view_type in ['tree', 'pivot', 'graph'] and self.env.company.timesheet_encode_uom_id == self.env.ref('uom.product_uom_day'): - result['arch'] = self._apply_time_label(result['arch']) + result['arch'] = self.env['account.analytic.line']._apply_time_label(result['arch'], related_model=self._name) + return result - @api.model - def _apply_time_label(self, view_arch, related_model=None): - doc = etree.XML(view_arch) - Model = self.env[related_model or self._name] - encoding_uom = self.env.company.timesheet_encode_uom_id - for node in doc.xpath("//field[@widget='timesheet_uom'][not(@string)] | //field[@widget='timesheet_uom_no_toggle'][not(@string)]"): - name_with_uom = re.sub(_('Hours') + "|Hours", encoding_uom.name or '', Model._fields[node.get('name')]._description_string(self.env), flags=re.IGNORECASE) - node.set('string', name_with_uom) - - return etree.tostring(doc, encoding='unicode') - @api.ondelete(at_uninstall=False) def _unlink_except_contains_entries(self): """ diff --git a/addons/hr_timesheet/report/project_report.py b/addons/hr_timesheet/report/project_report.py index f8c33eb7423..2f5890f834c 100644 --- a/addons/hr_timesheet/report/project_report.py +++ b/addons/hr_timesheet/report/project_report.py @@ -30,5 +30,5 @@ class ReportProjectTaskUser(models.Model): def _fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False): result = super(ReportProjectTaskUser, self)._fields_view_get(view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=submenu) if view_type in ['pivot', 'graph'] and self.env.company.timesheet_encode_uom_id == self.env.ref('uom.product_uom_day'): - result['arch'] = self.env['project.task']._apply_time_label(result['arch'], related_model=self._name) + result['arch'] = self.env['account.analytic.line']._apply_time_label(result['arch'], related_model=self._name) return result