From a9dd1abf4c3a7183c69e8ebd0483a7321c911313 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Fri, 26 Nov 2021 14:38:16 +0100
Subject: [PATCH 2/2] backoffice: computed data with datasource in cards graph
 (#59048)

---
 tests/admin_pages/test_card.py | 36 ++++++++++++++++++++++++----------
 wcs/carddef.py                 |  1 +
 2 files changed, 27 insertions(+), 10 deletions(-)

diff --git a/tests/admin_pages/test_card.py b/tests/admin_pages/test_card.py
index 9b6824ab..14660970 100644
--- a/tests/admin_pages/test_card.py
+++ b/tests/admin_pages/test_card.py
@@ -659,9 +659,15 @@ def test_cards_svg(pub):
         fields.StringField(id='1', label='string', type='string', varname='block foo 1', data_source=ds1),
         fields.ItemField(id='2', label='item', type='item', varname='block foo 2', data_source=ds1),
         fields.ItemsField(id='3', label='items', type='items', varname='block foo 3', data_source=ds1),
+        fields.ComputedField(
+            id='4', label='computed', type='computed', varname='block foo 4', data_source=ds1
+        ),
         fields.StringField(id='10', label='string', type='string', varname='block fooo 10', data_source=ds2),
         fields.ItemField(id='20', label='item', type='item', varname='block fooo 20', data_source=ds2),
         fields.ItemsField(id='30', label='items', type='items', varname='block fooo 30', data_source=ds2),
+        fields.ComputedField(
+            id='40', label='computed', type='computed', varname='block fooo 40', data_source=ds2
+        ),
     ]
     block1.store()
     block2 = BlockDef()
@@ -670,6 +676,9 @@ def test_cards_svg(pub):
         fields.StringField(id='1', label='string', type='string', varname='block bar 1', data_source=ds2),
         fields.ItemField(id='2', label='item', type='item', varname='block bar 2', data_source=ds2),
         fields.ItemsField(id='3', label='items', type='items', varname='block bar 3', data_source=ds2),
+        fields.ComputedField(
+            id='4', label='computed', type='computed', varname='block bar 4', data_source=ds2
+        ),
     ]
     block2.store()
     block3 = BlockDef()
@@ -678,6 +687,9 @@ def test_cards_svg(pub):
         fields.StringField(id='1', label='string', type='string', varname='block baz 1', data_source=ds3),
         fields.ItemField(id='2', label='item', type='item', varname='block baz 2', data_source=ds3),
         fields.ItemsField(id='3', label='items', type='items', varname='block baz 3', data_source=ds3),
+        fields.ComputedField(
+            id='4', label='computed', type='computed', varname='block baz 4', data_source=ds3
+        ),
     ]
     block3.store()
 
@@ -690,11 +702,13 @@ def test_cards_svg(pub):
         fields.StringField(id='1', label='string', type='string', varname='foo 1', data_source=ds1),
         fields.ItemField(id='2', label='item', type='item', varname='foo 2', data_source=ds1),
         fields.ItemsField(id='3', label='items', type='items', varname='foo 3', data_source=ds1),
-        fields.BlockField(id='4', label='block', type='block:%s' % block1.slug),
+        fields.ComputedField(id='4', label='computed', type='computed', varname='foo 4', data_source=ds1),
+        fields.BlockField(id='5', label='block', type='block:%s' % block1.slug),
         fields.StringField(id='10', label='string', type='string', varname='fooo 10', data_source=ds2),
         fields.ItemField(id='20', label='item', type='item', varname='fooo 20', data_source=ds2),
         fields.ItemsField(id='30', label='items', type='items', varname='fooo 30', data_source=ds2),
-        fields.BlockField(id='40', label='block', type='block:%s' % block2.slug),
+        fields.ComputedField(id='40', label='computed', type='computed', varname='fooo 40', data_source=ds2),
+        fields.BlockField(id='50', label='block', type='block:%s' % block2.slug),
     ]
     carddef11.store()
     carddef12 = CardDef()
@@ -715,7 +729,8 @@ def test_cards_svg(pub):
         fields.StringField(id='1', label='string', type='string', varname='bar 1', data_source=ds2),
         fields.ItemField(id='2', label='item', type='item', varname='bar 2', data_source=ds2),
         fields.ItemsField(id='3', label='items', type='items', varname='bar 3', data_source=ds2),
-        fields.BlockField(id='4', label='block', type='block:%s' % block2.slug),
+        fields.ComputedField(id='4', label='computed', type='computed', varname='bar 4', data_source=ds2),
+        fields.BlockField(id='5', label='block', type='block:%s' % block2.slug),
     ]
     carddef21.store()
     carddef22 = CardDef()
@@ -730,7 +745,8 @@ def test_cards_svg(pub):
         fields.StringField(id='1', label='string', type='string', varname='baz 1', data_source=ds3),
         fields.ItemField(id='2', label='item', type='item', varname='baz 2', data_source=ds3),
         fields.ItemsField(id='3', label='items', type='items', varname='baz 3', data_source=ds3),
-        fields.BlockField(id='4', label='block', type='block:%s' % block3.slug),
+        fields.ComputedField(id='4', label='computed', type='computed', varname='baz 4', data_source=ds3),
+        fields.BlockField(id='5', label='block', type='block:%s' % block3.slug),
     ]
     carddef31.store()
     carddef32 = CardDef()
@@ -755,10 +771,10 @@ def test_cards_svg(pub):
     assert '<title>card_card_3_1</title' in resp
     assert '<title>card_card_3_2</title' in resp
     # and relations
-    assert resp.text.count('<title>card_card_1_1&#45;&gt;card_card_1_2</title>') == 6
-    assert resp.text.count('<title>card_card_1_1&#45;&gt;card_card_2_2</title>') == 9
-    assert resp.text.count('<title>card_card_2_1&#45;&gt;card_card_2_2</title>') == 6
-    assert resp.text.count('<title>card_card_3_1&#45;&gt;card_card_3_2</title>') == 6
+    assert resp.text.count('<title>card_card_1_1&#45;&gt;card_card_1_2</title>') == 8
+    assert resp.text.count('<title>card_card_1_1&#45;&gt;card_card_2_2</title>') == 12
+    assert resp.text.count('<title>card_card_2_1&#45;&gt;card_card_2_2</title>') == 8
+    assert resp.text.count('<title>card_card_3_1&#45;&gt;card_card_3_2</title>') == 8
 
     resp = app.get('/backoffice/cards/svg?show-orphans=on')
     assert '<title>card_card_1_3</title' in resp
@@ -778,7 +794,7 @@ def test_cards_svg(pub):
     assert '<title>card_card_3_1</title' not in resp
     assert '<title>card_card_3_2</title' not in resp
     # and relations
-    assert resp.text.count('<title>card_card_1_1&#45;&gt;card_card_1_2</title>') == 6
+    assert resp.text.count('<title>card_card_1_1&#45;&gt;card_card_1_2</title>') == 8
     assert resp.text.count('<title>card_card_1_1&#45;&gt;card_card_2_2</title>') == 0
     assert resp.text.count('<title>card_card_2_1&#45;&gt;card_card_2_2</title>') == 0
     assert resp.text.count('<title>card_card_3_1&#45;&gt;card_card_3_2</title>') == 0
@@ -797,5 +813,5 @@ def test_cards_svg(pub):
     # and relations
     assert resp.text.count('<title>card_card_1_1&#45;&gt;card_card_1_2</title>') == 0
     assert resp.text.count('<title>card_card_1_1&#45;&gt;card_card_2_2</title>') == 0
-    assert resp.text.count('<title>card_card_2_1&#45;&gt;card_card_2_2</title>') == 6
+    assert resp.text.count('<title>card_card_2_1&#45;&gt;card_card_2_2</title>') == 8
     assert resp.text.count('<title>card_card_3_1&#45;&gt;card_card_3_2</title>') == 0
diff --git a/wcs/carddef.py b/wcs/carddef.py
index e5dfbf2e..b2a447b0 100644
--- a/wcs/carddef.py
+++ b/wcs/carddef.py
@@ -304,6 +304,7 @@ def get_cards_graph(category=None, show_orphans=False):
             'string': '1..n',
             'item': '1..n',
             'items': 'n..n',
+            'computed': '1..n',
         }
         for field in fields:
             data_source = getattr(field, 'data_source', None)
-- 
2.33.0

