Exemplo n.º 1
0
    def test_custom_regions(self):
        class CustomRegions(Regions):
            @cached_render  # Not necessary for this test, but good style.
            def render(self, region, context=None):
                html = []
                for plugin in self._contents[region]:
                    out = self._renderer.render_plugin_in_context(
                        plugin, context)
                    if isinstance(plugin, HTML):
                        html.extend(['<div class="raw">', out, "</div>"])
                    else:
                        html.append(out)
                return mark_safe("".join(html))

        renderer = TemplatePluginRenderer(regions_class=CustomRegions)
        # List of templates works too:
        renderer.register_template_renderer(HTML, ["renderer/html.html"])
        renderer.register_string_renderer(
            RichText, lambda plugin: mark_safe(plugin.text))

        page = Page.objects.create(template_key="standard")
        HTML.objects.create(parent=page,
                            ordering=10,
                            region="main",
                            html="<b>Hello</b>")
        RichText.objects.create(parent=page,
                                ordering=20,
                                region="main",
                                text="<strong>yes</strong>")

        regions = renderer.regions(page)
        self.assertEqual(
            regions.render("main"),
            '<div class="raw"><b>Hello</b>\n</div><strong>yes</strong>',
        )
Exemplo n.º 2
0
    def test_restart_subregion(self):
        """Restarting the subregion we're already in works correctly"""

        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

                    first = False

                yield "</div>"

        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,
        )

        self.assertEqual(
            regions.render("main"),
            'before<div class="stuff">first</div><div class="stuff">second</div>after',
        )
Exemplo n.º 3
0
from django.shortcuts import get_object_or_404, redirect, render
from django.utils.html import format_html

from feincms3 import plugins
from feincms3.renderer import TemplatePluginRenderer

from .models import HTML, External, Image, Page, RichText, Snippet

renderer = TemplatePluginRenderer()
renderer.register_string_renderer(
    RichText,
    plugins.render_richtext,
)
renderer.register_string_renderer(
    Image,
    lambda plugin: format_html(
        '<figure><img src="{}" alt=""/><figcaption>{}</figcaption></figure>',
        plugin.image.url,
        plugin.caption,
    ),
)
renderer.register_template_renderer(
    Snippet,
    lambda plugin: plugin.template_name,
    lambda plugin, context: {'additional': 'context'},
)
renderer.register_string_renderer(
    External,
    plugins.render_external,
)
renderer.register_string_renderer(
Exemplo n.º 4
0

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>"
Exemplo n.º 5
0
from django.utils.html import mark_safe

from feincms3.renderer import TemplatePluginRenderer

from app.pages import models


renderer = TemplatePluginRenderer()
renderer.register_string_renderer(
    models.RichText,
    lambda plugin: mark_safe(plugin.text),
)
renderer.register_template_renderer(
    models.Image,
    'plugins/image.html',
)
Exemplo n.º 6
0
from feincms3.renderer import TemplatePluginRenderer

from juso.events import models as events
from juso.renderer import register_renderers, render_image

renderer = TemplatePluginRenderer()
register_renderers(renderer, events)

location_renderer = TemplatePluginRenderer()

location_renderer.register_string_renderer(events.LocationImage, render_image)
Exemplo n.º 7
0
from django.utils.html import mark_safe

from feincms3.renderer import TemplatePluginRenderer

from app.pages import models

renderer = TemplatePluginRenderer()
renderer.register_string_renderer(
    models.RichText,
    lambda plugin: mark_safe(plugin.text),
)
renderer.register_template_renderer(
    models.Image,
    'plugins/image.html',
)
Exemplo n.º 8
0
from django.template.loader import render_to_string
from feincms3 import plugins
from feincms3.renderer import TemplatePluginRenderer

from juso.forms import models

renderer = TemplatePluginRenderer()

renderer.register_string_renderer(
    models.RichText, lambda plugin: plugins.richtext.render_richtext(plugin))


def render_form_field(field):
    return render_to_string(f"forms/fields/{field.input_type}.html", {
        "field": field,
    })


renderer.register_string_renderer(models.FormField,
                                  lambda plugin: render_form_field(plugin))
Exemplo n.º 9
0
from django.shortcuts import get_object_or_404, render

from feincms3 import plugins
from feincms3.renderer import TemplatePluginRenderer

from .models import Article, HTML, Download

renderer = TemplatePluginRenderer()
renderer.register_string_renderer(HTML, plugins.html.render_html)
renderer.register_template_renderer(Download, "plugins/download.html")


def article_detail(request, pk):
    article = get_object_or_404(Article, pk=pk)
    return render(
        request,
        "article.html",
        {
            "article": article,
            "regions": renderer.regions(article)
        },
    )
Exemplo n.º 10
0
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.get_redirect_url():
        return redirect(page.get_redirect_url())

    return render(
        request,
        page.template.template_name,
        {
            "page":
            page,
            "regions":