misc: allow a placeholder to force synchronous rendering (#20157)
This commit is contained in:
parent
dd4ac80249
commit
916e26ad45
|
@ -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:
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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 "<p>Hello anonymous user</p>" not in resp.content
|
||||
assert "<p>You are not linked</p>" 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
|
||||
|
|
Loading…
Reference in New Issue