From cce8e002f5085b290bd4142c259f3f9f9a5e7225 Mon Sep 17 00:00:00 2001 From: jem-odoo Date: Mon, 13 Aug 2018 11:12:09 +0200 Subject: [PATCH] [IMP] hr_timesheet: label on unit_amount We want the UoM name (encoding timesheet) to be displayed in the label of unit_amount field. To do so, the field_view_get is extended to change the label on the field decorated with the `timesheet_uom` widget, in the view arch If a string attribute is set on the field, it is prior; so we only add label if no one is set to avoid breaking view inheritance. Task #39079 --- addons/hr_timesheet/models/hr_timesheet.py | 18 ++++++++++++++++++ addons/hr_timesheet/models/project.py | 7 +++++++ 2 files changed, 25 insertions(+) diff --git a/addons/hr_timesheet/models/hr_timesheet.py b/addons/hr_timesheet/models/hr_timesheet.py index da01b442fad..6e2ceffa36e 100644 --- a/addons/hr_timesheet/models/hr_timesheet.py +++ b/addons/hr_timesheet/models/hr_timesheet.py @@ -77,6 +77,24 @@ class AccountAnalyticLine(models.Model): self.filtered(lambda t: t.project_id)._timesheet_postprocess(values) return result + @api.model + def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False): + """ Set the correct label for `unit_amount`, depending on company UoM """ + result = super(AccountAnalyticLine, self).fields_view_get(view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=submenu) + result['arch'] = self._apply_timesheet_label(result['arch']) + return result + + @api.model + def _apply_timesheet_label(self, view_arch): + doc = etree.XML(view_arch) + encoding_uom = self.env.user.company_id.timesheet_encode_uom_id + # Here, we select only the unit_amount field having no string set to give priority to + # custom inheretied view stored in database. Even if normally, no xpath can be done on + # 'string' attribute. + for node in doc.xpath("//field[@name='unit_amount'][@widget='timesheet_uom'][not(@string)]"): + node.set('string', _('Duration (%s)') % (encoding_uom.name)) + return etree.tostring(doc, encoding='unicode') + # ---------------------------------------------------- # Business Methods # ---------------------------------------------------- diff --git a/addons/hr_timesheet/models/project.py b/addons/hr_timesheet/models/project.py index 7976df1defb..8ab95b05cab 100644 --- a/addons/hr_timesheet/models/project.py +++ b/addons/hr_timesheet/models/project.py @@ -151,3 +151,10 @@ class Task(models.Model): 'company_id': account_id.company_id.id, }) return result + + @api.model + def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False): + """ Set the correct label for `unit_amount`, depending on company UoM """ + result = super(Task, self).fields_view_get(view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=submenu) + result['arch'] = self.env['account.analytic.line']._apply_timesheet_label(result['arch']) + return result