From 3eae379c308e21bb76b18ba2fa4f4f4d24cb3494 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Mon, 14 Nov 2022 08:54:50 +0100
Subject: [PATCH 2/2] api: dependencies, wscalls can be used in conditions
 (#71252)

---
 tests/api/test_export_import.py | 22 +++++++++++++++++++---
 wcs/fields.py                   |  2 ++
 wcs/workflows.py                |  1 +
 3 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/tests/api/test_export_import.py b/tests/api/test_export_import.py
index 947d4b9be..011a47758 100644
--- a/tests/api/test_export_import.py
+++ b/tests/api/test_export_import.py
@@ -116,6 +116,12 @@ def test_export_import_dependencies(pub):
     wscall.store()
     wscall = NamedWsCall(name='Test bis')
     wscall.store()
+    wscall = NamedWsCall(name='Test ter')
+    wscall.store()
+    wscall = NamedWsCall(name='Test quater')
+    wscall.store()
+    wscall = NamedWsCall(name='Test quinquies')
+    wscall.store()
 
     carddef = CardDef()
     carddef.name = 'Test'
@@ -216,7 +222,10 @@ def test_export_import_dependencies(pub):
     send_mail.to = [role.id]
     send_mail.subject = '{{ webservice.test }}'
     send_mail.body = '{{ cards|objects:"test" }} {{ forms|objects:"test-ter" }}'
-    send_mail.condition = {'type': 'django', 'value': '{{ cards|objects:"test-ter" }}'}
+    send_mail.condition = {
+        'type': 'django',
+        'value': '{{ cards|objects:"test-ter" }} {{ webservice.test_ter }}',
+    }
 
     dispatch_auto = status.add_action('dispatch')
     dispatch_auto.rules = [{'role_id': role.id, 'value': 'xxx'}]
@@ -234,7 +243,7 @@ def test_export_import_dependencies(pub):
                 {
                     'condition': {
                         'type': 'django',
-                        'value': '{{ cards|objects:"test-bis" }} {{ forms|objects:"test-quater" }}',
+                        'value': '{{ cards|objects:"test-bis" }} {{ forms|objects:"test-quater" }} {{ webservice.test_quater }}',
                     },
                     'error_message': 'You shall not pass.',
                 }
@@ -244,7 +253,10 @@ def test_export_import_dependencies(pub):
             id='1',
             label='test',
             type='block:test',
-            condition={'type': 'django', 'value': '{{ forms|objects:"test-bis" }}'},
+            condition={
+                'type': 'django',
+                'value': '{{ forms|objects:"test-bis" }} {{ webservice.test_quinquies }}',
+            },
         ),
         CommentField(
             id='2',
@@ -262,6 +274,8 @@ def test_export_import_dependencies(pub):
         ('test', 'workflows'),
         ('test', 'blocks'),
         ('test', 'wscalls'),
+        ('test_quater', 'wscalls'),
+        ('test_quinquies', 'wscalls'),
         ('test', 'cards'),
         ('test-bis', 'cards'),
         ('test-bis', 'forms'),
@@ -279,6 +293,7 @@ def test_export_import_dependencies(pub):
         ('foobar', 'data-sources'),
         ('test', 'wscalls'),
         ('test_bis', 'wscalls'),
+        ('test_ter', 'wscalls'),
         ('test', 'cards'),
         ('test-bis', 'cards'),
         ('test-ter', 'cards'),
@@ -304,6 +319,7 @@ def test_export_import_dependencies(pub):
     assert {(x['id'], x['type']) for x in resp.json['data']} == {
         ('foobar', 'data-sources'),
         ('test_bis', 'wscalls'),
+        ('test_ter', 'wscalls'),
         ('test-bis', 'cards'),
         ('test-ter', 'cards'),
         ('test-bis', 'forms'),
diff --git a/wcs/fields.py b/wcs/fields.py
index 634850cd4..51616c40e 100644
--- a/wcs/fields.py
+++ b/wcs/fields.py
@@ -714,6 +714,7 @@ class Field:
             condition = self.condition
             if condition:
                 if condition.get('type') == 'django':
+                    yield from check_wscalls(condition.get('value'))
                     yield from check_carddefs(condition.get('value'))
                     yield from check_formdefs(condition.get('value'))
 
@@ -3070,6 +3071,7 @@ class PageField(Field):
             for post_condition in post_conditions:
                 condition = post_condition.get('condition') or {}
                 if condition.get('type') == 'django':
+                    yield from check_wscalls(condition.get('value'))
                     yield from check_carddefs(condition.get('value'))
                     yield from check_formdefs(condition.get('value'))
 
diff --git a/wcs/workflows.py b/wcs/workflows.py
index b4a642f2e..acfb8c853 100644
--- a/wcs/workflows.py
+++ b/wcs/workflows.py
@@ -2383,6 +2383,7 @@ class WorkflowStatusItem(XmlSerialisable):
             condition = self.condition
             if condition:
                 if condition.get('type') == 'django':
+                    yield from check_wscalls(condition.get('value'))
                     yield from check_carddefs(condition.get('value'))
                     yield from check_formdefs(condition.get('value'))
 
-- 
2.35.1

