From c8bedfbe5cee179afc5cc93a4aa2668edd0e82ec Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Fri, 17 Dec 2021 18:26:53 +0100
Subject: [PATCH] wcs: use full context to render custom title and fields
 (#58735)

---
 combo/apps/wcs/models.py |  7 ++++---
 tests/test_wcs.py        | 37 +++++++++++++++++++++++++++++++++++--
 2 files changed, 39 insertions(+), 5 deletions(-)

diff --git a/combo/apps/wcs/models.py b/combo/apps/wcs/models.py
index 86587ee2..d9af2dc1 100644
--- a/combo/apps/wcs/models.py
+++ b/combo/apps/wcs/models.py
@@ -1039,10 +1039,11 @@ class WcsCardInfosCell(CardMixin, CellBase):
 
         if response.status_code == 200:
             extra_context['card'] = response.json()
-            context = Context(extra_context)
+            custom_context = Context(extra_context)
+            custom_context.update(context)
             if self.title_type == 'manual':
                 try:
-                    extra_context['title'] = Template(self.custom_title).render(context)
+                    extra_context['title'] = Template(self.custom_title).render(custom_context)
                 except (VariableDoesNotExist, TemplateSyntaxError):
                     extra_context['title'] = ''
             # auto title or custom_title gives an empty title, use default value + card text
@@ -1059,7 +1060,7 @@ class WcsCardInfosCell(CardMixin, CellBase):
                     try:
                         extra_context['card']['custom_fields'][item['template']] = Template(
                             item['template']
-                        ).render(context)
+                        ).render(custom_context)
                     except (VariableDoesNotExist, TemplateSyntaxError):
                         pass
 
diff --git a/tests/test_wcs.py b/tests/test_wcs.py
index 8e86c9fd..b0b30b0d 100644
--- a/tests/test_wcs.py
+++ b/tests/test_wcs.py
@@ -1811,7 +1811,7 @@ def test_card_cell_load(mock_send):
 
 
 @mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)
-def test_card_cell_render(mock_send, context):
+def test_card_cell_render(mock_send, context, app):
     page = Page.objects.create(title='xxx', template_name='standard')
     cell = WcsCardInfosCell(page=page, placeholder='content', order=0)
     cell.carddef_reference = 'default:card_model_1'
@@ -1889,6 +1889,21 @@ def test_card_cell_render(mock_send, context):
     result = cell.render(context)
     assert '<h2>' not in result
 
+    # test available context
+    cell.title_type = 'manual'
+    cell.custom_title = 'X{{ site_base }}Y'
+    cell.card_ids = '11'
+    cell.save()
+    resp = app.get(page.get_online_url())
+    assert len(resp.context['cells']) == 1
+    extra_ctx = re.findall(r'data-extra-context="(.*)"', resp.text)
+    cell_url = reverse(
+        'combo-public-ajax-page-cell',
+        kwargs={'page_pk': page.pk, 'cell_reference': cell.get_reference()},
+    )
+    cell_resp = app.get(cell_url + '?ctx=' + extra_ctx[0])
+    assert '<h2>Xhttp://testserverY</h2>' in cell_resp
+
 
 @mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)
 def test_card_cell_render_text_field(mock_send, context):
@@ -1947,7 +1962,7 @@ def test_card_cell_render_email_field(mock_send, context):
 
 
 @mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)
-def test_card_cell_render_custom_schema(mock_send, context):
+def test_card_cell_render_custom_schema(mock_send, context, app):
     page = Page.objects.create(title='xxx', template_name='standard')
     cell = WcsCardInfosCell(page=page, placeholder='content', order=0)
     cell.carddef_reference = 'default:card_model_1'
@@ -2002,6 +2017,24 @@ def test_card_cell_render_custom_schema(mock_send, context):
     assert PyQuery(result).find('p.label').text() == 'Foo bar baz'
     assert PyQuery(result).find('div.value').text() == 'Foo,Bar'
 
+    # test available context
+    cell.card_ids = '11'
+    cell.custom_schema = {
+        'cells': [
+            {'varname': '@custom@', 'template': 'Foo bar baz X{{ site_base }}Y', 'display_mode': 'label'},
+        ]
+    }
+    cell.save()
+    resp = app.get(page.get_online_url())
+    assert len(resp.context['cells']) == 1
+    extra_ctx = re.findall(r'data-extra-context="(.*)"', resp.text)
+    cell_url = reverse(
+        'combo-public-ajax-page-cell',
+        kwargs={'page_pk': page.pk, 'cell_reference': cell.get_reference()},
+    )
+    cell_resp = app.get(cell_url + '?ctx=' + extra_ctx[0])
+    assert '<p class="label">Foo bar baz Xhttp://testserverY</p>' in cell_resp
+
 
 @mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)
 def test_card_cell_render_identifier(mock_send, nocache, app):
-- 
2.33.0

