From 2c948a3cab12f0fcbe1badd5fc74b03f11fbc94d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Sat, 6 Aug 2022 20:49:02 +0200
Subject: [PATCH] workflows: make "create document" action available in global
 actions (#53353)

---
 tests/admin_pages/test_workflow.py | 41 +++++++++++++++++++++++++++++-
 wcs/wf/export_to_model.py          | 14 ++++++----
 2 files changed, 49 insertions(+), 6 deletions(-)

diff --git a/tests/admin_pages/test_workflow.py b/tests/admin_pages/test_workflow.py
index d60d2465c..311a12e35 100644
--- a/tests/admin_pages/test_workflow.py
+++ b/tests/admin_pages/test_workflow.py
@@ -6,7 +6,7 @@ from unittest import mock
 
 import pytest
 from quixote.http_request import Upload as QuixoteUpload
-from webtest import Upload
+from webtest import Radio, Upload
 
 from wcs import fields
 from wcs.carddef import CardDef
@@ -1889,6 +1889,45 @@ def test_workflows_variables_with_export_to_model_action(pub):
     resp = resp.click('Edit', href='1/')
 
 
+def test_workflows_export_to_model_in_status(pub):
+    create_superuser(pub)
+
+    Workflow.wipe()
+    workflow = Workflow(name='foo')
+    baz_status = workflow.add_status(name='baz')
+    export_to = baz_status.add_action('export_to_model')
+    export_to.label = 'create doc'
+    workflow.store()
+
+    app = login(get_app(pub))
+    resp = app.get(export_to.get_admin_url())
+    assert isinstance(resp.form['method'], Radio)
+    resp.form['label'] = 'export label'
+    resp = resp.form.submit('submit')
+    workflow.refresh_from_storage()
+    assert workflow.possible_status[0].items[0].method == 'interactive'
+    assert workflow.possible_status[0].items[0].label == 'export label'
+
+
+def test_workflows_export_to_model_in_global_action(pub):
+    create_superuser(pub)
+
+    Workflow.wipe()
+    workflow = Workflow(name='foo')
+    ac1 = workflow.add_global_action('Action', 'ac1')
+    export_to = ac1.add_action('export_to_model')
+    export_to.label = 'create doc'
+    workflow.store()
+
+    app = login(get_app(pub))
+    resp = app.get(export_to.get_admin_url())
+    assert not isinstance(resp.form['method'], Radio)
+    assert 'label' not in resp.form.fields
+    resp = resp.form.submit('submit')
+    workflow.refresh_from_storage()
+    assert workflow.global_actions[0].items[0].method == 'non-interactive'
+
+
 def test_workflows_variables_replacement(pub):
     create_superuser(pub)
 
diff --git a/wcs/wf/export_to_model.py b/wcs/wf/export_to_model.py
index bbfe3dd9f..4242d376d 100644
--- a/wcs/wf/export_to_model.py
+++ b/wcs/wf/export_to_model.py
@@ -38,6 +38,7 @@ from wcs.fields import CommentField, PageField, SubtitleField, TitleField
 from wcs.portfolio import has_portfolio, push_document
 from wcs.workflows import (
     AttachmentEvolutionPart,
+    WorkflowGlobalAction,
     WorkflowStatusItem,
     get_formdata_template_context,
     register_item_class,
@@ -235,7 +236,7 @@ class ExportToModel(WorkflowStatusItem):
     key = 'export_to_model'
     category = 'formdata-action'
     support_substitution_variables = True
-    ok_in_global_action = False
+    ok_in_global_action = True
     filename = None
 
     endpoint = False
@@ -341,6 +342,7 @@ class ExportToModel(WorkflowStatusItem):
 
     def add_parameters_widgets(self, form, parameters, prefix='', formdef=None, **kwargs):
         super().add_parameters_widgets(form, parameters, prefix=prefix, formdef=formdef, **kwargs)
+        in_global_action = isinstance(self.parent, WorkflowGlobalAction)
         methods = collections.OrderedDict(
             [('interactive', _('Interactive (button)')), ('non-interactive', _('Non interactive'))]
         )
@@ -425,7 +427,9 @@ class ExportToModel(WorkflowStatusItem):
                 value=self.push_to_portfolio,
             )
 
-        if 'method' in parameters:
+        if 'method' in parameters and in_global_action:
+            form.add_hidden('%smethod' % prefix, 'non-interactive')
+        elif 'method' in parameters:
             form.add(
                 RadiobuttonsWidget,
                 '%smethod' % prefix,
@@ -436,7 +440,7 @@ class ExportToModel(WorkflowStatusItem):
                 extra_css_class='widget-inline-radio',
             )
 
-        if 'by' in parameters:
+        if 'by' in parameters and not in_global_action:
             options = [(None, '---', None)] + self.get_list_of_roles()
             form.add(
                 WidgetList,
@@ -452,7 +456,7 @@ class ExportToModel(WorkflowStatusItem):
                 element_kwargs={'render_br': False, 'options': options},
             )
 
-        if 'label' in parameters:
+        if 'label' in parameters and not in_global_action:
             form.add(
                 StringWidget,
                 '%slabel' % prefix,
@@ -464,7 +468,7 @@ class ExportToModel(WorkflowStatusItem):
                 },
             )
 
-        if 'backoffice_info_text' in parameters:
+        if 'backoffice_info_text' in parameters and not in_global_action:
             form.add(
                 WysiwygTextWidget,
                 '%sbackoffice_info_text' % prefix,
-- 
2.36.1

