From 928b38c5f345ad13e6cb2abdef67ebde35fb380e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sat, 1 Oct 2022 14:45:59 +0200 Subject: [PATCH] Add straight values to wizard state view issue11703 review431591006 --- CHANGELOG | 1 + doc/ref/wizard.rst | 19 ++++++++++++++++++- trytond/wizard/wizard.py | 41 ++++++++++++++++++++++++++++------------ 3 files changed, 48 insertions(+), 13 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index e6ec169608..179bb93028 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,4 @@ +* Add straight values to wizard state view * Add strip option to Char fields * Strip only one wildcard * Manage web extension origin as null diff --git a/doc/ref/wizard.rst b/doc/ref/wizard.rst index dd1be179cd..f421dca3dc 100644 --- a/doc/ref/wizard.rst +++ b/doc/ref/wizard.rst @@ -116,8 +116,15 @@ StateView The form is defined by the :class:`~trytond.model.ModelView` with the name ``model_name``, the ``XML`` id in ``view`` and the ``buttons``. - The default value of the view can be set with a method on wizard having the + The default values of the view can be set with a method on wizard having the same name as the state but starting with ``default_``. + The values of the view can be also set with a method on wizard having the + same name as the state but starting with ``value_``. + + .. note:: + The difference between default values and values is that the client calls + :meth:`~trytond.model.Model.on_change` and + :meth:`~trytond.model.Model.on_change_with` for the default values. Instance attributes are: @@ -154,6 +161,16 @@ Instance methods are: ``fields`` is the list of field names. +.. method:: StateView.get_values(wizard, state_name, fields) + + Return values for the fields. + + ``wizard`` is a :class:`Wizard` instance. + + ``state_name`` is the name of the :class:`State`. + + ``fields`` is the list of field names. + .. method:: StateView.get_buttons(wizard, state_name) Return button definitions of the wizard. diff --git a/trytond/wizard/wizard.py b/trytond/wizard/wizard.py index 51bae139da..cf82f89a6e 100644 --- a/trytond/wizard/wizard.py +++ b/trytond/wizard/wizard.py @@ -93,18 +93,32 @@ class StateView(State): default = getattr(wizard, 'default_%s' % state_name, None) if default: defaults.update(default(fields)) - for field_name, value in list(defaults.items()): - if '.' in field_name: - continue - field = Model_._fields[field_name] - if value and field._type == 'many2one': - Target = pool.get(field.model_name) - if 'rec_name' in Target._fields: - defaults.setdefault( - field_name + '.', {})['rec_name'] = Target( - value).rec_name + self._complete_values(defaults) return defaults + def get_values(self, wizard, state_name, fields): + "Return values for the fields" + values = {} + value = getattr(wizard, 'value_%s' % state_name, None) + if value: + values.update(value(fields)) + self._complete_values(values) + return values + + def _complete_values(self, values): + pool = Pool() + Model_ = pool.get(self.model_name) + for field_name, value in list(values.items()): + if '.' in field_name: + continue + field = Model_._fields[field_name] + if value and field._type == 'many2one': + Target = field.get_target() + if 'rec_name' in Target._fields: + values.setdefault( + field_name + '.', {})['rec_name'] = Target( + value).rec_name + def get_buttons(self, wizard, state_name): ''' Returns button definitions translated @@ -300,6 +314,7 @@ class Wizard(URLMixin, PoolBase): - ``view``: a dictionary with: - ``fields_view``: a fields/view definition - ``defaults``: a dictionary with default values + - ``values``: a dictionary with values - ``buttons``: a list of buttons ''' cls.check_access() @@ -321,12 +336,14 @@ class Wizard(URLMixin, PoolBase): if isinstance(state, StateView): view = state.get_view(self, state_name) - defaults = state.get_defaults(self, state_name, - list(view['fields'].keys())) + fields = list(view['fields'].keys()) + defaults = state.get_defaults(self, state_name, fields) + values = state.get_values(self, state_name, fields) buttons = state.get_buttons(self, state_name) result['view'] = { 'fields_view': view, 'defaults': defaults, + 'values': values, 'buttons': buttons, 'state': state_name, }