Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def exit(self, **kwargs):
yield ""
def reenter(self, **kwargs):
yield from self.exit(**kwargs)
yield from self.enter(**kwargs)
continue_subrenderer = ContinueSubrenderer()
continue_subrenderer.register_string_renderer(Text, lambda plugin: plugin.text)
continue_subrenderer.register_string_renderer(Command, "")
class RegionsWithContinue(SubrendererRegions):
subrenderers = {"continue": continue_subrenderer}
continue_renderer = TemplatePluginRenderer()
continue_renderer.register_string_renderer(Text, lambda plugin: plugin.text)
continue_renderer.register_string_renderer(Command, "")
regions = RegionsWithContinue(
item=None,
contents={
"main": [
Text(text="before"),
Command(subrenderer="continue"),
Text(text="first"),
Command(subrenderer="continue"),
Text(text="second"),
Command(subrenderer=""),
Text(text="after"),
]
},
teaser_renderer = TeaserRenderer()
teaser_renderer.register_string_renderer(Teaser, lambda plugin: plugin.text)
teaser_renderer.register_string_renderer(Command, "")
faq_renderer = FAQRenderer()
faq_renderer.register_string_renderer(FAQ, lambda plugin: plugin.text)
faq_renderer.register_string_renderer(Command, "")
faq_renderer.register_string_renderer(File, lambda plugin: plugin.text)
class Regions(SubrendererRegions):
subrenderers = {"teasers": teaser_renderer, "faq": faq_renderer}
renderer = TemplatePluginRenderer(regions_class=Regions)
renderer.register_string_renderer(Text, lambda plugin: plugin.text)
renderer.register_string_renderer(Teaser, "")
renderer.register_string_renderer(FAQ, "")
renderer.register_string_renderer(Command, "")
renderer.register_string_renderer(File, lambda plugin: plugin.text)
class Test(TestCase):
def test_enter_exit(self):
regions = Regions(
item=None,
contents={
"main": [
Text(text="Text 1"),
Teaser(text="Teaser 1"),
Teaser(text="Teaser 2"),
from django.shortcuts import get_object_or_404, redirect, render
from feincms3.plugins import external, html, richtext
from feincms3.regions import Regions
from feincms3.renderer import TemplatePluginRenderer
from .models import HTML, External, Image, Page, RichText, Snippet
renderer = TemplatePluginRenderer()
renderer.register_string_renderer(RichText, richtext.render_richtext)
renderer.register_template_renderer(Image, "renderer/image.html")
Snippet.register_with(renderer)
renderer.register_string_renderer(External, external.render_external)
renderer.register_string_renderer(HTML, html.render_html)
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)
subregion = "teasers"
class FAQ(SimpleNamespace):
subregion = "faq"
class Command(SimpleNamespace):
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 test_plugin_template_instance(self):
renderer = TemplatePluginRenderer()
renderer.register_template_renderer(HTML, Template("{{ plugin.html|safe }}"))
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>")
self.assertEqual(regions.render("main", None), "<b>Hello</b>")
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"
)
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
first = False
yield ""
restart_renderer = TemplatePluginRenderer()
restart_renderer.register_string_renderer(Text, lambda plugin: plugin.text)
restart_renderer.register_string_renderer(Command, "")
regions = RestartRegions(
contents={
"main": [
Text(text="before"),
Command(subregion="restart"),
Text(text="first"),
Command(subregion="restart"),
Text(text="second"),
Command(subregion=""),
Text(text="after"),
]
},
renderer=restart_renderer,
from feincms3.renderer import Regions, TemplatePluginRenderer, cached_render
__all__ = ("Subrenderer", "SubrendererRegions")
warnings.warn(
"The subrenderer module will be removed. Please start using"
" the subregions feature of feincms3.regions.Regions instead.",
DeprecationWarning,
stacklevel=2,
)
class Subrenderer(TemplatePluginRenderer):
"""
``TemplatePluginRenderer`` subclass with hooks for adding wrapping elements
and for rejecting plugin types
"""
def accepts(self, plugin, context=None):
"""
Returns ``True`` if plugin is of a handleable type
Returning ``False`` exits the subrenderer.
"""
return plugin.__class__ in self._renderers
def enter(self, **kwargs):
"""
Hook for opening a wrapping element