changeset 5665e78186f4 in trytond:default
details: https://hg.tryton.org/trytond?cmd=changeset&node=5665e78186f4
description:
        Add straight values to wizard state view

        issue11703
        review431591006
diffstat:

 CHANGELOG                |   1 +
 doc/ref/wizard.rst       |  19 ++++++++++++++++++-
 trytond/wizard/wizard.py |  41 +++++++++++++++++++++++++++++------------
 3 files changed, 48 insertions(+), 13 deletions(-)

diffs (116 lines):

diff -r defde6b8f3de -r 5665e78186f4 CHANGELOG
--- a/CHANGELOG Sun Sep 25 20:23:13 2022 +0200
+++ b/CHANGELOG Sat Oct 01 14:45:59 2022 +0200
@@ -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 -r defde6b8f3de -r 5665e78186f4 doc/ref/wizard.rst
--- a/doc/ref/wizard.rst        Sun Sep 25 20:23:13 2022 +0200
+++ b/doc/ref/wizard.rst        Sat Oct 01 14:45:59 2022 +0200
@@ -116,8 +116,15 @@
 
    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 @@
 
    ``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 -r defde6b8f3de -r 5665e78186f4 trytond/wizard/wizard.py
--- a/trytond/wizard/wizard.py  Sun Sep 25 20:23:13 2022 +0200
+++ b/trytond/wizard/wizard.py  Sat Oct 01 14:45:59 2022 +0200
@@ -93,18 +93,32 @@
         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 @@
             - ``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 @@
 
         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,
                 }

Reply via email to