def render_plugin(self, context=None, placeholder=None, admin=False, processors=None): instance, plugin = self.get_plugin_instance() if instance and not (admin and not plugin.admin_preview): if not placeholder or not isinstance(placeholder, Placeholder): placeholder = instance.placeholder placeholder_slot = placeholder.slot current_app = context.current_app if context else None context = PluginContext(context, instance, placeholder, current_app=current_app) context = plugin.render(context, instance, placeholder_slot) request = context.get('request', None) page = None if request: page = request.current_page context['allowed_child_classes'] = plugin.get_child_classes(placeholder_slot, page) if plugin.render_plugin: template = hasattr(instance, 'render_template') and instance.render_template or plugin.render_template if not template: raise ValidationError("plugin has no render_template: %s" % plugin.__class__) else: template = None return render_plugin(context, instance, placeholder, template, processors, context.current_app) else: from cms.middleware.toolbar import toolbar_plugin_processor if processors and toolbar_plugin_processor in processors: if not placeholder: placeholder = self.placeholder current_app = context.current_app if context else None context = PluginContext(context, self, placeholder, current_app=current_app) template = None return render_plugin(context, self, placeholder, template, processors, context.current_app) return ""
def render_plugin(self, context=None, placeholder=None, admin=False, processors=None): instance, plugin = self.get_plugin_instance() if instance and not (admin and not plugin.admin_preview): if not placeholder or not isinstance(placeholder, Placeholder): placeholder = instance.placeholder placeholder_slot = placeholder.slot current_app = context.current_app if context else None context = PluginContext(context, instance, placeholder, current_app=current_app) context = plugin.render(context, instance, placeholder_slot) request = context.get("request", None) page = None if request: page = request.current_page context["allowed_child_classes"] = plugin.get_child_classes(placeholder_slot, page) if plugin.render_plugin: template = plugin._get_render_template(context, instance, placeholder) if not template: raise ValidationError("plugin has no render_template: %s" % plugin.__class__) else: template = None return render_plugin(context, instance, placeholder, template, processors, context.current_app) else: from cms.middleware.toolbar import toolbar_plugin_processor if processors and toolbar_plugin_processor in processors: if not placeholder: placeholder = self.placeholder current_app = context.current_app if context else None context = PluginContext(context, self, placeholder, current_app=current_app) template = None return render_plugin(context, self, placeholder, template, processors, context.current_app) return ""
def render_tag(self, context, config_key, id_key): config = context.get(config_key) or {} component_id = context.get(id_key) or None metadata = config.get('metadata') or {} snippet_id = metadata.get('snippet_id', None) if not metadata or not snippet_id: return render_rendering_error( "Could not render smart snippet with UUID:{}".format(component_id), "Full config: {}".format(config)) try: snippet = SmartSnippet.objects.get(id=snippet_id) except (SmartSnippet.DoesNotExist, ValueError): return render_rendering_error( "Could not render smart snippet with id:{}".format(snippet_id), "Full config: {}".format(config)) fake_pointer = SmartSnippetPointer(snippet=snippet) fake_pointer.placeholder_id = 0 fake_pointer.id = 0 fake_pointer.pk = 0 plugin_context = PluginContext(context, fake_pointer, None) plugin_context.update(config.get('variables', {})) try: return snippet.render(plugin_context) except Exception as exc: # Rendering errors have very varied types return render_rendering_error( "Could not render smart snippet with id:{}. Rendering error.".format(snippet_id), "Full config: {}, error message:{}".format(config, exc.message))
def render_plugin(self, context=None, placeholder=None, admin=False, processors=None): instance, plugin = self.get_plugin_instance() if instance and not (admin and not plugin.admin_preview): if not isinstance(placeholder, Placeholder): placeholder = instance.placeholder placeholder_slot = placeholder.slot current_app = context.current_app if context else None context = PluginContext(context, instance, placeholder, current_app=current_app) context = plugin.render(context, instance, placeholder_slot) if plugin.render_plugin: template = hasattr( instance, 'render_template' ) and instance.render_template or plugin.render_template if not template: raise ValidationError("plugin has no render_template: %s" % plugin.__class__) else: template = None return render_plugin(context, instance, placeholder, template, processors, context.current_app) return ""
def test_render(self): page = create_page(title='hellp', template='page.html', language='en') request = self.get_request(page, 'en') plugin = add_plugin(page.placeholders.get(slot='content'), 'LinkPlugin', 'en', url='http://example.com', name='some text') context = PluginContext({'request': request}, plugin, page.placeholders.get(slot='content')) output = render_placeholder(page.placeholders.get(slot='content'), context, editable=False) self.assertEqual(output, '<a href="http://example.com">some text</a>') plugin.delete() plugin = add_plugin(page.placeholders.get(slot='content'), 'LinkPlugin', 'en', url='http://example.com') add_plugin(page.placeholders.get(slot='content'), 'TextPlugin', 'en', body='text body', target=plugin) output = render_placeholder(page.placeholders.get(slot='content'), context, editable=False) self.assertEqual( output, '<span class="plugin_link"><a href="http://example.com">text body</a></span>' )
def test_processors(self): """ Tests that default plugin context processors are working, that plugin processors and plugin context processors can be defined in settings and are working and that extra plugin context processors can be passed to PluginContext. """ def test_passed_plugin_context_processor(instance, placeholder, context): return { 'test_passed_plugin_context_processor': 'test_passed_plugin_context_processor_ok' } t = u'{% load cms_tags %}' + \ u'{{ plugin.counter }}|{{ plugin.instance.body }}|{{ test_passed_plugin_context_processor }}|{{ test_plugin_context_processor }}' instance, plugin = CMSPlugin.objects.all()[0].get_plugin_instance() instance.render_template = Template(t) context = PluginContext( {'original_context_var': 'original_context_var_ok'}, instance, self.test_placeholders['main'], processors=(test_passed_plugin_context_processor, )) plugin_rendering._standard_processors = {} c = render_plugins((instance, ), context, self.test_placeholders['main']) r = "".join(c) self.assertEqual( r, u'1|' + self.test_data['text_main'] + '|test_passed_plugin_context_processor_ok|test_plugin_context_processor_ok|' + self.test_data['text_main'] + '|main|original_context_var_ok|test_plugin_processor_ok|' + self.test_data['text_main'] + '|main|original_context_var_ok') plugin_rendering._standard_processors = {}
def test_processors(self): """ Tests that plugin processors and plugin context processors can be defined in settings and are working and that extra plugin context processors can be passed to PluginContext. """ from djangocms_text_ckeditor.cms_plugins import TextPlugin from cms.plugin_pool import plugin_pool instance = CMSPlugin.objects.all()[0].get_plugin_instance()[0] load_from_string = self.load_template_from_string @plugin_pool.register_plugin class ProcessorTestPlugin(TextPlugin): name = "Test Plugin" def get_render_template(self, context, instance, placeholder): t = u'{% load cms_tags %}' + \ u'{{ body }}|test_passed_plugin_context_processor_ok|' \ u'{{ test_plugin_context_processor }}' return load_from_string(t) def test_passed_plugin_context_processor(instance, placeholder, context): return { 'test_passed_plugin_context_processor': 'test_passed_plugin_context_processor_ok' } instance.plugin_type = 'ProcessorTestPlugin' instance._inst = instance context = PluginContext( {'original_context_var': 'original_context_var_ok'}, instance, self.test_placeholders['main'], processors=(test_passed_plugin_context_processor, )) plugin_rendering._standard_processors = {} content_renderer = self.get_content_renderer() r = content_renderer.render_plugin(instance, context, self.test_placeholders['main']) expected = ( self.test_data['text_main'] + # noqa: W504 '|test_passed_plugin_context_processor_ok|test_plugin_context_processor_ok|' + # noqa: W504 self.test_data['text_main'] + # noqa: W504 '|main|original_context_var_ok|test_plugin_processor_ok|' + # noqa: W504 self.test_data['text_main'] + # noqa: W504 '|main|original_context_var_ok' # noqa: W504 ) self.assertEqual(r, expected) plugin_rendering._standard_processors = {}
def render_plugin(self, context=None, placeholder=None, admin=False, processors=None): instance, plugin = self.get_plugin_instance() if instance and not (admin and not plugin.admin_preview): if not isinstance(placeholder, Placeholder): placeholder = instance.placeholder placeholder_slot = placeholder.slot current_app = context.current_app if context else None context = PluginContext(context, instance, placeholder, current_app=current_app) context = plugin.render(context, instance, placeholder_slot) request = context.get('request', None) page = None if request: page = request.current_page context['allowed_child_classes'] = plugin.get_child_classes(placeholder_slot, page) if plugin.render_plugin: template = hasattr(instance, 'render_template') and instance.render_template or plugin.render_template if not template: raise ValidationError("plugin has no render_template: %s" % plugin.__class__) else: template = None return render_plugin(context, instance, placeholder, template, processors, context.current_app) return ""
def get_plugin_context(self, page, lang, plugin, edit=False): """ Returns a context suitable for CMSPlugin.render_plugin / render_placeholder :param page: Page object :param lang: Current language :param plugin: Plugin instance :param edit: Enable edit mode for rendering :return: PluginContext instance """ from cms.plugin_rendering import PluginContext request = self.get_page_request(page, self.user, lang=lang, edit=edit) return PluginContext({'request': request}, plugin, plugin.placeholder)
def test_empty_plugin(self): placeholder = Placeholder.objects.create() model_instance = add_plugin( placeholder, RenderItPlugin, 'en', ) html = model_instance.render_plugin( PluginContext( dict={'request': None}, instance=model_instance, placeholder=placeholder, )) self.assertEqual('', html)
def get_plugin_index_data(base_plugin, request): text_bits = [] instance, plugin_type = base_plugin.get_plugin_instance() if instance is None: # this is an empty plugin return text_bits search_fields = getattr(instance, 'search_fields', []) if hasattr(instance, 'search_fulltext'): # check if the plugin instance has search enabled search_contents = instance.search_fulltext elif hasattr(base_plugin, 'search_fulltext'): # now check in the base plugin instance (CMSPlugin) search_contents = base_plugin.search_fulltext elif hasattr(plugin_type, 'search_fulltext'): # last check in the plugin class (CMSPluginBase) search_contents = plugin_type.search_fulltext else: # disabled if there's search fields defined, # otherwise it's enabled. search_contents = not bool(search_fields) if search_contents: if django.get_version() < '1.8': plugin_context = RequestContext(request) else: plugin_context = PluginContext({'request': request}, instance, instance.placeholder) plugin_contents = instance.render_plugin(context=plugin_context) if plugin_contents: text_bits = get_cleaned_bits(plugin_contents) else: values = (get_field_value(instance, field) for field in search_fields) for value in values: cleaned_bits = get_cleaned_bits(value or '') text_bits.extend(cleaned_bits) return text_bits
def get_plugin_context(self, page, lang, plugin, edit=False): """ Returns a context suitable for CMSPlugin.render_plugin / render_placeholder :param page: Page object :param lang: Current language :param plugin: Plugin instance :param edit: Enable edit mode for rendering :return: PluginContext instance """ from cms.plugin_rendering import PluginContext from sekizai.context_processors import sekizai request = self.get_page_request(page, self.user, lang=lang, edit=edit) context = {'request': request} renderer = self.get_content_renderer(request) if renderer: context['cms_content_renderer'] = renderer context.update(sekizai(request)) return PluginContext(context, plugin, plugin.placeholder)
def test_text_plugin_with_custom_tag_in_settings(self): placeholder = Placeholder.objects.create() renderit_instance = add_plugin( placeholder, RenderItPlugin, 'en', ) text_instance = add_plugin( placeholder, TextPlugin, 'en', body='sample: {% sample %}', ) renderit_instance.child_plugin_instances = [ text_instance, ] html = renderit_instance.render_plugin( PluginContext( dict={'request': None}, instance=renderit_instance, placeholder=placeholder, )) self.assertIn('HERE BE DA SIMPLE TAG!', html)
def test_text_plugin_with_url(self): placeholder = Placeholder.objects.create() renderit_instance = add_plugin( placeholder, RenderItPlugin, 'en', ) text_instance = add_plugin( placeholder, TextPlugin, 'en', body='admin: {% url "admin:index" %}', ) renderit_instance.child_plugin_instances = [ text_instance, ] html = renderit_instance.render_plugin( PluginContext( dict={'request': None}, instance=renderit_instance, placeholder=placeholder, )) self.assertEqual('admin: /en/admin/', html.strip())
def test_simple_text_plugin(self): placeholder = Placeholder.objects.create() renderit_instance = add_plugin( placeholder, RenderItPlugin, 'en', ) text_instance = add_plugin( placeholder, TextPlugin, 'en', body='just some text', ) renderit_instance.child_plugin_instances = [ text_instance, ] html = renderit_instance.render_plugin( PluginContext( dict={'request': None}, instance=renderit_instance, placeholder=placeholder, )) self.assertEqual('just some text', html.strip())
def test_text_plugin_with_custom_tag_on_model(self): placeholder = Placeholder.objects.create() renderit_instance = add_plugin( placeholder, RenderItPlugin, 'en', tag_libraries='sample_tags', ) text_instance = add_plugin( placeholder, TextPlugin, 'en', body='dummy_var: {{ dummy_var }}', ) renderit_instance.child_plugin_instances = [ text_instance, ] html = renderit_instance.render_plugin( PluginContext( dict={'request': None}, instance=renderit_instance, placeholder=placeholder, )) self.assertIn('whatzaaap!', html)