-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathcms_plugins.py
59 lines (48 loc) · 1.87 KB
/
cms_plugins.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from cms.plugin_base import CMSPluginBase
from cms.plugin_pool import plugin_pool
from django import template
from django.conf import settings
from django.utils.html import escape
from django.utils.safestring import mark_safe
from django.utils.translation import gettext_lazy as _
from .forms import SnippetPluginForm
from .models import SnippetPtr
from .utils import show_draft_content
CACHE_ENABLED = getattr(settings, "DJANGOCMS_SNIPPET_CACHE", False)
class SnippetPlugin(CMSPluginBase):
model = SnippetPtr
name = _("Snippet")
render_template = "djangocms_snippet/snippet.html"
text_enabled = True
text_editor_preview = False
cache = CACHE_ENABLED
form = SnippetPluginForm
def render(self, context, instance, placeholder):
snippet = instance.snippet_grouper.snippet(show_editable=show_draft_content(context["request"]))
# Handle the potential for no snippet to be found i.e. Draft
if not snippet:
return context
try:
if snippet.template:
context = context.flatten()
context.update({"html": mark_safe(snippet.html)})
t = template.loader.get_template(snippet.template)
content = t.render(context)
else:
# only html provided
t = template.Template(snippet.html)
content = t.render(context)
except template.TemplateDoesNotExist:
content = _("Template %(template)s does not exist.") % {"template": snippet.template}
except Exception as e:
content = escape(str(e))
context.update(
{
"placeholder": placeholder,
"object": instance,
"html": mark_safe(snippet.html),
"content": content,
}
)
return context
plugin_pool.register_plugin(SnippetPlugin)