From 1de9da1a6a75415e1afb1aff079a3d42f99c4d4a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Fri, 17 Jun 2022 17:20:21 +0200
Subject: [PATCH] workflows: add button/link to go to workflow form fields view
 (#65790)

---
 tests/admin_pages/test_workflow.py | 18 ++++++++++++++++++
 wcs/admin/workflows.py             |  2 +-
 wcs/wf/form.py                     | 25 ++++++++++++++++++++++---
 3 files changed, 41 insertions(+), 4 deletions(-)

diff --git a/tests/admin_pages/test_workflow.py b/tests/admin_pages/test_workflow.py
index ddf678218..de7fe70a3 100644
--- a/tests/admin_pages/test_workflow.py
+++ b/tests/admin_pages/test_workflow.py
@@ -3243,17 +3243,35 @@ def test_workflows_form_action_config(pub):
     resp.form['varname'] = 'myform'
     resp.form['condition$type'] = 'django'
     resp.form['condition$value_django'] = '42'
+    assert 'Edit Fields' not in resp.text
+    assert resp.form.fields['submit'][0]._value == 'Submit and go to fields edition'
     resp = resp.form.submit('submit')
     assert resp.location == 'http://example.net/backoffice/workflows/%s/status/%s/items/1/fields/' % (
         wf.id,
         st.id,
     )
+    resp = resp.follow()
+    resp.form['label'] = 'Text field'
+    resp = resp.form.submit('submit')
 
     wf = Workflow.get(wf.id)
     form = wf.possible_status[0].items[0]
     assert form.by == ['_submitter']
     assert form.varname == 'myform'
     assert form.condition == {'type': 'django', 'value': '42'}
+    assert form.formdef.fields[0].label == 'Text field'
+
+    resp = app.get('/backoffice/workflows/%s/status/%s/items/1/' % (wf.id, st.id))
+    assert 'Edit Fields' in resp.text
+    resp = resp.click('Edit Fields')
+
+    resp = app.get('/backoffice/workflows/%s/status/%s/items/1/' % (wf.id, st.id))
+    assert resp.form.fields['submit'][0]._value == 'Submit'
+    resp = resp.form.submit('submit')
+    assert resp.location == 'http://example.net/backoffice/workflows/%s/status/%s/items/' % (
+        wf.id,
+        st.id,
+    )
 
 
 def test_workflows_inspect_view(pub):
diff --git a/wcs/admin/workflows.py b/wcs/admin/workflows.py
index fdbc27ece..e6e21602e 100644
--- a/wcs/admin/workflows.py
+++ b/wcs/admin/workflows.py
@@ -421,7 +421,7 @@ class WorkflowItemPage(Directory):
                         'name': self.parent.name,
                     }
                 )
-                if hasattr(self.item, 'redirect_after_submit_url'):
+                if getattr(self.item, 'redirect_after_submit_url', None):
                     return redirect(self.item.redirect_after_submit_url)
                 return redirect('..')
 
diff --git a/wcs/wf/form.py b/wcs/wf/form.py
index c7d06fbb9..11fb2ae28 100644
--- a/wcs/wf/form.py
+++ b/wcs/wf/form.py
@@ -29,7 +29,7 @@ from wcs.variables import LazyFormDataVar
 from wcs.workflows import EvolutionPart, RedisplayFormException, WorkflowStatusItem, register_item_class
 
 from ..qommon import _
-from ..qommon.form import SingleSelectWidget, VarnameWidget, WidgetList
+from ..qommon.form import HtmlWidget, SingleSelectWidget, VarnameWidget, WidgetList
 
 
 class WorkflowFormEvolutionPart(EvolutionPart):
@@ -115,13 +115,24 @@ class FormWorkflowStatusItem(WorkflowStatusItem):
     ok_in_global_action = False
     endpoint = False
     waitpoint = True
-    redirect_after_submit_url = 'fields/'
-    submit_button_label = _('Submit and go to fields edition')
 
     by = []
     formdef = None
     varname = None
 
+    @property
+    def submit_button_label(self):
+        # make submit button go to fields page when there are not yet any field.
+        if self.formdef and self.formdef.fields:
+            return _('Submit')
+        return _('Submit and go to fields edition')
+
+    @property
+    def redirect_after_submit_url(self):
+        if self.formdef and self.formdef.fields:
+            return None
+        return 'fields/'
+
     @classmethod
     def init(cls):
         if 'lookup_wf_form_file' not in FileDirectory._lookup_methods:
@@ -152,6 +163,14 @@ class FormWorkflowStatusItem(WorkflowStatusItem):
                 value=self.varname,
                 hint=_('This is used as prefix for form fields variable names.'),
             )
+            if not formdef and self.formdef and self.formdef.fields:
+                # add link to go edit or view fields
+                widget = HtmlWidget(
+                    '<p><a class="pk-button" href="fields/">%s</a></p>'
+                    % (_('View fields') if self.parent.parent.is_readonly() else _('Edit Fields'))
+                )
+                widget.tab = ('general', _('General'))
+                form.widgets.append(widget)
 
     def get_parameters(self):
         return ('by', 'varname', 'condition')
-- 
2.36.1

