From 916e26ad459ed614818887e4c1979b7a2b3ec838 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Mon, 20 Nov 2017 12:10:12 +0100 Subject: [PATCH] misc: allow a placeholder to force synchronous rendering (#20157) --- combo/data/models.py | 4 +++- combo/public/templatetags/combo.py | 22 +++++++++++++--------- tests/test_public.py | 26 ++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 10 deletions(-) diff --git a/combo/data/models.py b/combo/data/models.py index 903f0bdb..220ce90d 100644 --- a/combo/data/models.py +++ b/combo/data/models.py @@ -78,12 +78,14 @@ def element_is_visible(element, user=None): class Placeholder(object): - def __init__(self, key, name=None, acquired=False, render=True, cell=None): + def __init__(self, key, name=None, acquired=False, render=True, cell=None, + force_synchronous=False): self.key = key self.name = name self.acquired = acquired self.render = render self.cell = cell + self.force_synchronous = force_synchronous def get_name(self): if self.cell: diff --git a/combo/public/templatetags/combo.py b/combo/public/templatetags/combo.py index a7cf528a..b4e6a43d 100644 --- a/combo/public/templatetags/combo.py +++ b/combo/public/templatetags/combo.py @@ -46,6 +46,7 @@ def placeholder(context, placeholder_name, **options): if not context['traverse_cells']: return context context['render'] = True + context['placeholder'] = placeholder if not placeholder.render: context['render'] = False return context @@ -83,15 +84,18 @@ def render_cell(context, cell): raise PermissionDenied() in_dashboard = True - context['in_dashboard'] = in_dashboard - try: - return cell.render(context) - except NothingInCacheException: - return template.loader.get_template('combo/deferred-cell.html').render(context) - except: - if context.get('placeholder_search_mode'): - return '' - raise + with context.push(): + context['in_dashboard'] = in_dashboard + if 'placeholder' in context and context['placeholder'].force_synchronous: + context['synchronous'] = True + try: + return cell.render(context) + except NothingInCacheException: + return template.loader.get_template('combo/deferred-cell.html').render(context) + except: + if context.get('placeholder_search_mode'): + return '' + raise @register.tag def skeleton_extra_placeholder(parser, token): diff --git a/tests/test_public.py b/tests/test_public.py index 22b9ffbd..8d8fd405 100644 --- a/tests/test_public.py +++ b/tests/test_public.py @@ -415,3 +415,29 @@ def test_familyinfos_cell_with_placeholders(app, admin_user): kwargs={'page_pk': page.pk, 'cell_reference': family_cell.get_reference()})) assert "

Hello anonymous user

" not in resp.content assert "

You are not linked

" in resp.content + +def test_synchronous_placeholder(app): + page = Page(title=u'foo', slug='foo', template_name='standard', order=0) + page.save() + cell = FeedCell(page=page, placeholder='content', url='http://example.net', order=1) + cell.save() + + templates_settings = [settings.TEMPLATES[0].copy()] + templates_settings[0]['DIRS'] = ['%s/templates-1' % os.path.abspath(os.path.dirname(__file__))] + with override_settings(COMBO_PUBLIC_TEMPLATES={ + 'standard': { + 'name': 'Test', + 'template': 'combo/page_template_synchronous_placeholder.html', + }}, + TEMPLATES=templates_settings + ): + with mock.patch('requests.get') as requests_get: + requests_get.return_value = mock.Mock(content='', status_code=200) + resp = app.get('/foo/', status=200) + assert not 'data-ajax-cell-must-load="true"' in resp.body + + cell = FeedCell(page=page, placeholder='sidebar', url='http://example.org', order=1) + cell.save() + + resp = app.get('/foo/', status=200) + assert resp.body.count('data-ajax-cell-must-load="true"') == 1