From 82c42a8e610f224e9858c1adb30993e49518c2ca Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Mon, 11 Jul 2022 18:31:28 +0200
Subject: [PATCH] wcs: fix use of custom view in card cell with related
 (#67260)

---
 combo/apps/wcs/models.py | 14 ++++++++------
 tests/test_wcs.py        | 28 ++++++++++++++++++++++++++--
 2 files changed, 34 insertions(+), 8 deletions(-)

diff --git a/combo/apps/wcs/models.py b/combo/apps/wcs/models.py
index a20df2c1..7f673a49 100644
--- a/combo/apps/wcs/models.py
+++ b/combo/apps/wcs/models.py
@@ -1024,12 +1024,12 @@ class WcsCardInfosCell(CardMixin, CellBase):
     def get_repeat_template(self, context):
         return len(context.get(self.global_context_key) or [])
 
-    def get_card_data(self, card_slug, card_id, context, synchronous=False):
+    def get_card_data(self, card_slug, card_custom_view, card_id, context, synchronous=False):
         api_url = '/api/cards/%s/%s/?include-files-content=off' % (card_slug, card_id)
-        if self.card_custom_view:
+        if card_custom_view:
             api_url = '/api/cards/%s/%s/%s/?include-files-content=off' % (
                 card_slug,
-                self.card_custom_view,
+                card_custom_view,
                 card_id,
             )
             user = self.get_concerned_user(context)
@@ -1211,7 +1211,7 @@ class WcsCardInfosCell(CardMixin, CellBase):
                 return []
             # and data
             next_card_data = self.get_card_data(
-                card_slug, card_data['fields']['%s_raw' % varname], context, synchronous=True
+                card_slug, None, card_data['fields']['%s_raw' % varname], context, synchronous=True
             )
             if not next_card_data:
                 # card data not found
@@ -1240,7 +1240,9 @@ class WcsCardInfosCell(CardMixin, CellBase):
         if not card_id:
             # no card id found
             return []
-        card_data = self.get_card_data(first_cell.card_slug, card_id, context, synchronous=True)
+        card_data = self.get_card_data(
+            first_cell.card_slug, first_cell.card_custom_view, card_id, context, synchronous=True
+        )
         if not card_data:
             # card data not found
             return []
@@ -1318,7 +1320,7 @@ class WcsCardInfosCell(CardMixin, CellBase):
                 extra_context['card_not_found'] = True
             return extra_context
 
-        card_data = self.get_card_data(self.card_slug, card_id, context)
+        card_data = self.get_card_data(self.card_slug, self.card_custom_view, card_id, context)
         if not card_data:
             return extra_context
 
diff --git a/tests/test_wcs.py b/tests/test_wcs.py
index e47a8530..f453784f 100644
--- a/tests/test_wcs.py
+++ b/tests/test_wcs.py
@@ -122,7 +122,7 @@ WCS_CARDDEFS_DATA = [
     {'title': 'Card Model 2', 'slug': 'card_model_2'},
     {'title': 'Card Model 3', 'slug': 'card_model_3'},
     {'title': 'Card A', 'slug': 'card_a'},
-    {'title': 'Card B', 'slug': 'card_b'},
+    {'title': 'Card B', 'slug': 'card_b', 'custom_views': [{'id': 'a-custom-view', 'text': 'foo bar'}]},
     {'title': 'Card C', 'slug': 'card_c'},
     {'title': 'Card D', 'slug': 'card_d'},
     {'title': 'Card E', 'slug': 'card_e'},
@@ -1695,6 +1695,7 @@ def test_cards_cell_setup(mock_send):
         ('default:card_model_3', 'test : Card Model 3'),
         ('default:card_a', 'test : Card A'),
         ('default:card_b', 'test : Card B'),
+        ('default:card_b:a-custom-view', 'test : Card B - foo bar'),
         ('default:card_c', 'test : Card C'),
         ('default:card_d', 'test : Card D'),
         ('default:card_e', 'test : Card E'),
@@ -1704,6 +1705,7 @@ def test_cards_cell_setup(mock_send):
         ('other:card_model_3', 'test2 : Card Model 3'),
         ('other:card_a', 'test2 : Card A'),
         ('other:card_b', 'test2 : Card B'),
+        ('other:card_b:a-custom-view', 'test2 : Card B - foo bar'),
         ('other:card_c', 'test2 : Card C'),
         ('other:card_d', 'test2 : Card D'),
         ('other:card_e', 'test2 : Card E'),
@@ -1998,6 +2000,7 @@ def test_card_cell_setup(mock_send, app, admin_user):
         ('default:card_model_3', 'test : Card Model 3'),
         ('default:card_a', 'test : Card A'),
         ('default:card_b', 'test : Card B'),
+        ('default:card_b:a-custom-view', 'test : Card B - foo bar'),
         ('default:card_c', 'test : Card C'),
         ('default:card_d', 'test : Card D'),
         ('default:card_e', 'test : Card E'),
@@ -2007,6 +2010,7 @@ def test_card_cell_setup(mock_send, app, admin_user):
         ('other:card_model_3', 'test2 : Card Model 3'),
         ('other:card_a', 'test2 : Card A'),
         ('other:card_b', 'test2 : Card B'),
+        ('other:card_b:a-custom-view', 'test2 : Card B - foo bar'),
         ('other:card_c', 'test2 : Card C'),
         ('other:card_d', 'test2 : Card D'),
         ('other:card_e', 'test2 : Card E'),
@@ -3477,10 +3481,30 @@ def test_card_cell_render_identifier_from_related(mock_send, nocache, app):
     for j, url in enumerate(urls):
         assert url in mock_send.call_args_list[j][0][0].url
 
+    # reset
     cell.order = 0  # reset
     cell.save()
+    cell2.slug = 'slugb'
+    cell2.save()
+
+    # test with custom_view
+    cell2.carddef_reference = 'default:card_b:a-custom-view'
+    cell2.save()
+    single(
+        urls=[
+            # get first cell data
+            '/api/cards/card_a/1/',
+            # get card_c schema
+            '/api/cards/card_c/@schema',
+            # follow cardc relation
+            '/api/cards/card_c/6/',
+            # and follow cardb relation
+            '/api/cards/card_b/a-custom-view/7/',
+        ]
+    )
+
     # direct and multiple relation (items)
-    cell2.slug = 'slugb'  # reset
+    cell2.carddef_reference = 'default:card_b'  # reset
     cell2.related_card_path = 'sluga/cardsb'
     cell2.save()
     multiple(
-- 
2.35.1

