Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
parent=page, ordering=10, region="main", html="<b>Hello</b>"
)
regions = Regions.from_item(page, renderer=renderer)
self.assertEqual(regions.render("main", Context()), "<b>Hello</b>\n")
regions = Regions.from_item(page, renderer=renderer)
self.assertEqual(
regions.render("main", Context({"outer": "Test"})), "<b>Hello</b>Test\n"
)
regions = Regions.from_item(page, renderer=renderer, timeout=3)
self.assertEqual(
regions.render("main", Context({"outer": "Test2"})), "<b>Hello</b>Test2\n"
)
regions = Regions.from_item(page, renderer=renderer, timeout=3)
# Output stays the same.
self.assertEqual(
regions.render("main", Context({"outer": "Test3"})), "<b>Hello</b>Test2\n"
)
self.assertEqual(regions.cache_key("main"), "testapp.page-%s-main" % page.pk)
def page_detail(request, path=None):
page = get_object_or_404(
Page.objects.active(), path=("/%s/" % path) if path else "/"
)
page.activate_language(request)
if page.redirect_to_url or page.redirect_to_page:
return redirect(page.redirect_to_url or page.redirect_to_page)
return render(
request,
page.template.template_name,
{
"page": page,
"regions": Regions.from_item(
item=page, renderer=renderer, inherit_from=page.ancestors().reverse()
),
def test_standalone_renderer(self):
"""The renderer also works when used without a wrapping template"""
renderer = TemplatePluginRenderer()
renderer.register_template_renderer(
HTML, ["renderer/html.html", "renderer/html.html"]
)
page = Page.objects.create(template_key="standard")
HTML.objects.create(
parent=page, ordering=10, region="main", html="<b>Hello</b>"
)
regions = Regions.from_item(page, renderer=renderer)
self.assertEqual(regions.render("main", Context()), "<b>Hello</b>\n")
regions = Regions.from_item(page, renderer=renderer)
self.assertEqual(
regions.render("main", Context({"outer": "Test"})), "<b>Hello</b>Test\n"
)
regions = Regions.from_item(page, renderer=renderer, timeout=3)
self.assertEqual(
regions.render("main", Context({"outer": "Test2"})), "<b>Hello</b>Test2\n"
)
regions = Regions.from_item(page, renderer=renderer, timeout=3)
# Output stays the same.
self.assertEqual(
regions.render("main", Context({"outer": "Test3"})), "<b>Hello</b>Test2\n"
)
def test_restart_subregion(self):
class RestartRegions(Regions):
def handle_restart(self, items, context):
first = True
yield '<div class="stuff">'
while True:
yield self.renderer.render_plugin_in_context(
items.popleft(), context
)
# Item isn't the first and explicitly specifies
# subregion="restart", restart subregion
if (
not first
and items
and matches(items[0], subregions={None, "restart"})
):
break
</div>
subregion = ""
class File(SimpleNamespace):
pass
renderer = TemplatePluginRenderer()
renderer.register_string_renderer(Text, lambda plugin: plugin.text)
renderer.register_string_renderer(Teaser, lambda plugin: plugin.text)
renderer.register_string_renderer(FAQ, lambda plugin: plugin.text)
renderer.register_string_renderer(File, lambda plugin: plugin.text)
renderer.register_string_renderer(Command, "")
class MyRegions(Regions):
def handle_teasers(self, items, context):
yield '<div class="teasers">'
while True:
yield self.renderer.render_plugin_in_context(items.popleft(), context)
if not items or not matches(items[0], plugins=(Teaser,)):
break
yield "</div>"
def handle_faq(self, items, context):
yield '<div class="faq">'
while True:
yield self.renderer.render_plugin_in_context(items.popleft(), context)
if not items or not matches(
items[0], plugins=(FAQ, File), subregions={None, "faq"}
):
break</div>
def page_detail(request, path=None):
page = get_object_or_404(
Page.objects.active(),
path='/{}/'.format(path) if path else '/',
)
page.activate_language(request)
return render(request, page.template.template_name, {
'page': page,
'regions': Regions.from_item(
page, renderer=renderer, inherit_from=page.ancestors().reverse()),
})