From 32a978a016cc25fb288a428a4c8d180fe1e65142 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sun, 10 Oct 2021 22:08:39 +0200 Subject: [PATCH] Add format method to DictSchemaMixin issue10758 review354001002 --- CHANGELOG | 1 + doc/ref/models/models.rst | 4 ++++ trytond/ir/message.xml | 6 ++++++ trytond/model/dictschema.py | 26 ++++++++++++++++++++++++++ 4 files changed, 37 insertions(+) diff --git a/CHANGELOG b/CHANGELOG index e60cc36495..1ef288a06c 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,4 @@ +* Add format method to DictSchemaMixin * Allow modify record name on the reports * Add methods to format number and symbol on Lang * Add format number with symbol on Report diff --git a/doc/ref/models/models.rst b/doc/ref/models/models.rst index 4b2ec8a2fe..a0d9540121 100644 --- a/doc/ref/models/models.rst +++ b/doc/ref/models/models.rst @@ -737,6 +737,10 @@ Instance methods: Getter for the :attr:`selection_json`. +.. method:: DictSchemaMixin.format(value[, lang]) + + Format the value using the key definition and the language. + ========== MatchMixin ========== diff --git a/trytond/ir/message.xml b/trytond/ir/message.xml index 805a451753..27d7edf2df 100644 --- a/trytond/ir/message.xml +++ b/trytond/ir/message.xml @@ -124,6 +124,12 @@ this repository contains the full copyright notices and license terms. --> Help Selection JSON + + Yes + + + No + Sequence diff --git a/trytond/model/dictschema.py b/trytond/model/dictschema.py index a4ca792f84..236cfeb42a 100644 --- a/trytond/model/dictschema.py +++ b/trytond/model/dictschema.py @@ -211,3 +211,29 @@ class DictSchemaMixin(object): def delete(cls, records): super().delete(records) cls._relation_fields_cache.clear() + + def format(self, value, lang=None): + pool = Pool() + Lang = pool.get('ir.lang') + if lang is None: + lang = Lang.get() + if value is None: + return '' + if self.type_ == 'boolean': + if value: + return gettext('ir.msg_dict_yes') + else: + return gettext('ir.msg_dict_no') + elif self.type_ == 'integer': + return lang.format('%i', value) + elif self.type_ in {'float', 'numeric'}: + return lang.format('%.*f', (self.digits, value)) + elif self.type_ in {'date', 'datetime'}: + return lang.strftime(value) + elif self.type_ in {'selection', 'multiselection'}: + values = dict(json.loads(self.selection_json)) + if self.type_ == 'selection': + return values.get(value, '') + else: + return "; ".join(values.get(v, '') for v in value) + return value