From 3b3c7c4f2676cbc86191b336e275355937c0218f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Mon, 7 Dec 2020 16:35:20 +0100
Subject: [PATCH 2/3] workflow: add target mode/id to external and edit card
 items (#45653)

---
 tests/admin_pages/test_workflow.py | 20 ++++++++++++++++++++
 wcs/wf/edit_carddata.py            |  2 +-
 wcs/wf/external_workflow.py        | 25 +++++++++++++++++++++----
 3 files changed, 42 insertions(+), 5 deletions(-)

diff --git a/tests/admin_pages/test_workflow.py b/tests/admin_pages/test_workflow.py
index 5ddf5c5c..2a8e5e56 100644
--- a/tests/admin_pages/test_workflow.py
+++ b/tests/admin_pages/test_workflow.py
@@ -1613,6 +1613,16 @@ def test_workflows_global_actions_external_workflow_action(pub):
     resp.forms[0]['trigger_id'] = 'action:%s' % trigger.identifier
     resp = resp.forms[0].submit('submit').follow().follow()
     assert 'External workflow (action &quot;Global action&quot; on external)' in resp.text
+    assert Workflow.get(workflow.id).possible_status[0].items[0].target_mode == 'all'
+    assert Workflow.get(workflow.id).possible_status[0].items[0].target_id is None
+
+    resp = app.get('/backoffice/workflows/%s/status/%s/items/1/' % (workflow.id, st.id))
+    resp.forms[0]['target_mode'] = 'manual'
+    resp.forms[0]['target_id$type'] = 'template'
+    resp.forms[0]['target_id$value_template'] = '{{ form_var_plop_id }}'
+    resp = resp.forms[0].submit('submit')
+    assert Workflow.get(workflow.id).possible_status[0].items[0].target_mode == 'manual'
+    assert Workflow.get(workflow.id).possible_status[0].items[0].target_id == '{{ form_var_plop_id }}'
 
     trigger.identifier = 'another_test'
     wf.store()
@@ -1659,6 +1669,16 @@ def test_workflows_edit_carddata_action(pub):
     resp.forms[0]['mappings$element0$field_id'] = '1'
     resp = resp.forms[0].submit('submit').follow()
     assert 'Edit Card Data (not configured)' not in resp.text
+    assert Workflow.get(wf.id).possible_status[0].items[0].target_mode == 'all'
+    assert Workflow.get(wf.id).possible_status[0].items[0].target_id is None
+
+    resp = app.get('/backoffice/workflows/%s/status/%s/items/1/' % (wf.id, st.id))
+    resp.forms[0]['target_mode'] = 'manual'
+    resp.forms[0]['target_id$type'] = 'template'
+    resp.forms[0]['target_id$value_template'] = '{{ form_var_plop_id }}'
+    resp = resp.forms[0].submit('submit')
+    assert Workflow.get(wf.id).possible_status[0].items[0].target_mode == 'manual'
+    assert Workflow.get(wf.id).possible_status[0].items[0].target_id == '{{ form_var_plop_id }}'
 
 
 def test_workflows_criticality_levels(pub):
diff --git a/wcs/wf/edit_carddata.py b/wcs/wf/edit_carddata.py
index 5eb57eaf..4523ccb6 100644
--- a/wcs/wf/edit_carddata.py
+++ b/wcs/wf/edit_carddata.py
@@ -35,7 +35,7 @@ class EditCarddataWorkflowStatusItem(CreateCarddataWorkflowStatusItem, ExternalW
         return ExternalWorkflowGlobalAction.is_available()
 
     def get_parameters(self):
-        return ('formdef_slug', 'mappings', 'condition')
+        return ('formdef_slug', 'target_mode', 'target_id', 'mappings', 'condition')
 
     @property
     def slug(self):
diff --git a/wcs/wf/external_workflow.py b/wcs/wf/external_workflow.py
index ac016465..b384527a 100644
--- a/wcs/wf/external_workflow.py
+++ b/wcs/wf/external_workflow.py
@@ -17,7 +17,7 @@
 from quixote import get_publisher
 
 from wcs.qommon import _
-from wcs.qommon.form import SingleSelectWidget
+from wcs.qommon.form import SingleSelectWidget, ComputedExpressionWidget
 
 from wcs.logged_errors import LoggedError
 from wcs.workflows import WorkflowStatusItem, perform_items, register_item_class
@@ -33,6 +33,8 @@ class ExternalWorkflowGlobalAction(WorkflowStatusItem):
     category = 'formdata-action'
 
     slug = None
+    target_mode = None
+    target_id = None
     trigger_id = None
 
     @classmethod
@@ -67,8 +69,7 @@ class ExternalWorkflowGlobalAction(WorkflowStatusItem):
                 return trigger
 
     def add_parameters_widgets(self, form, parameters, prefix='', formdef=None):
-        super(ExternalWorkflowGlobalAction, self).add_parameters_widgets(
-            form, parameters, prefix=prefix, formdef=formdef)
+        super().add_parameters_widgets(form, parameters, prefix=prefix, formdef=formdef)
 
         if 'slug' in parameters:
             objects = [(None, '---', '')]
@@ -88,6 +89,22 @@ class ExternalWorkflowGlobalAction(WorkflowStatusItem):
                      required=True,
                      options=objects)
 
+        if 'target_mode' in parameters:
+            target_modes = [('all', _('Automatic'), 'all'), ('manual', _('Manual'), 'manual')]
+            form.add(SingleSelectWidget, '%starget_mode' % prefix,
+                     title=_('Targeting'),
+                     value=self.target_mode or 'all',
+                     required=True,
+                     options=target_modes,
+                     attrs={'data-dynamic-display-parent': 'true'})
+
+        if 'target_id' in parameters:
+            form.add(ComputedExpressionWidget, '%starget_id' % prefix,
+                     value=self.target_id,
+                     required=False,
+                     attrs={'data-dynamic-display-child-of': 'target_mode',
+                            'data-dynamic-display-value': 'manual'})
+
         if 'trigger_id' in parameters and form.get('%sslug' % prefix):
             object_def = self.get_object_def(form.get('%sslug' % prefix))
             if not object_def:
@@ -117,7 +134,7 @@ class ExternalWorkflowGlobalAction(WorkflowStatusItem):
         yield from formdata.iter_target_datas(objectdef=objectdef, object_type=self.slug, status_item=self)
 
     def get_parameters(self):
-        return ('slug', 'trigger_id', 'condition')
+        return ('slug', 'target_mode', 'target_id', 'trigger_id', 'condition')
 
     def perform(self, formdata):
         objectdef = self.get_object_def()
-- 
2.28.0

