[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 <IT-Ideas@users.noreply.github.com>
This commit is contained in:
Prakash Prajapati
2021-06-25 10:54:59 +00:00
parent 574739e125
commit 9b53115345
3 changed files with 14 additions and 15 deletions
@@ -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
+2 -14
View File
@@ -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):
"""
+1 -1
View File
@@ -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