def test_render_plugin_toolbar_config(self): """ Ensures that the render_plugin_toolbar_config tag sets the correct values in the sekizai context. """ page = self._getfirst() placeholder = page.placeholders.get(slot='body') parent_plugin = add_plugin(placeholder, 'SolarSystemPlugin', 'en') child_plugin_1 = add_plugin(placeholder, 'PlanetPlugin', 'en', target=parent_plugin) child_plugin_2 = add_plugin(placeholder, 'PlanetPlugin', 'en', target=parent_plugin) parent_plugin.child_plugin_instances = [ child_plugin_1, child_plugin_2, ] plugins = [ parent_plugin, child_plugin_1, child_plugin_2, ] placeholder_slot = placeholder.slot with self.login_user_context(self.get_superuser()): context = self.get_context(path=page.get_absolute_url(), page=page) context['request'].toolbar = CMSToolbar(context['request']) context['request'].toolbar.edit_mode = True context[get_varname()] = defaultdict(UniqueSequence) parent_plugin.render_plugin( context, placeholder=placeholder, processors=[toolbar_plugin_processor], ) # Rendering the parent plugin will put # three strings into the "js" collection in sekizai. # These strings contain the toolbar configuration # for each plugin. plugin_scripts = context[get_varname()]['js'] self.assertEqual(len(plugin_scripts), 3) for plugin, script in zip(plugins, reversed(plugin_scripts)): plugin_instance = plugin.get_plugin_class_instance() child_classes = plugin_instance.get_child_classes(placeholder_slot, page) or [] plugin_restrictions = ['"{}"'.format(klass) for klass in child_classes] plugin_restrictions_text = "plugin_restriction: [{}]".format(','.join(plugin_restrictions)) parent_classes = plugin_instance.get_parent_classes(placeholder_slot, page) or [] parent_restrictions = ['"{}"'.format(klass) for klass in parent_classes] parent_restrictions_text = "plugin_parent_restriction: [{}]".format(','.join(parent_restrictions)) self.assertTrue(plugin_restrictions_text in script) self.assertTrue(parent_restrictions_text in script)
def render_carusel(context, section): if not hasattr(section, 'carusel'): return context carusel = section.carusel.all() if carusel.count() < 1: return return { 'carusel': carusel, get_varname(): context[get_varname()], }
def render_map(context, section): if not hasattr(section, 'map'): return context map = section.map.all() if map.count() < 1: return return { 'map': map, get_varname(): context[get_varname()], }
def render_sec_block(context, section): if not hasattr(section, 'block'): return context sec_block = section.block image = None if hasattr(section, 'image'): image = section.image return { 'sec_block': sec_block, 'image': image, 'section': section, get_varname(): context[get_varname()], }
def render_filter(context, section): if not hasattr(section, 'filter'): return context filter = section.filter.all() if filter.count() < 1: return categories = [] for fil in filter: categories += fil.categories.all() categories = list(set(categories)) return { 'filter': filter, 'categories': categories, get_varname(): context[get_varname()], }
def render_plugin_toolbar_config(context, plugin): content_renderer = context['cms_content_renderer'] instance, plugin_class = plugin.get_plugin_instance() if not instance: return '' with context.push(): content = content_renderer.render_editable_plugin( instance, context, plugin_class, ) # render_editable_plugin will populate the plugin # parents and children cache. placeholder_cache = content_renderer.get_rendered_plugins_cache(instance.placeholder) toolbar_js = get_plugin_toolbar_js( instance, request_language=content_renderer.request_language, children=placeholder_cache['plugin_children'][instance.plugin_type], parents=placeholder_cache['plugin_parents'][instance.plugin_type], ) varname = get_varname() toolbar_js = '<script>{}</script>'.format(toolbar_js) # Add the toolbar javascript for this plugin to the # sekizai "js" namespace. context[varname]['js'].append(toolbar_js) return mark_safe(content)
def render_plugin_toolbar_config(context, plugin): content_renderer = context['cms_content_renderer'] instance, plugin_class = plugin.get_plugin_instance() if not instance: return '' with context.push(): content = content_renderer.render_editable_plugin( instance, context, plugin_class, ) # render_editable_plugin will populate the plugin # parents and children cache. placeholder_cache = content_renderer.get_rendered_plugins_cache( instance.placeholder) toolbar_js = get_plugin_toolbar_js( instance, request_language=content_renderer.request_language, children=placeholder_cache['plugin_children'][ instance.plugin_type], parents=placeholder_cache['plugin_parents'][instance.plugin_type], ) varname = get_varname() toolbar_js = '<script>{}</script>'.format(toolbar_js) # Add the toolbar javascript for this plugin to the # sekizai "js" namespace. context[varname]['js'].append(toolbar_js) return mark_safe(content)
def _restore_sekizai(context, changes): varname = get_varname() sekizai_container = context[varname] for key, values in changes.items(): sekizai_namespace = sekizai_container[key] for value in values: sekizai_namespace.append(value)
def render_plugin_init_js(context, plugin): renderer = context['cms_renderer'] plugin_js = renderer.get_plugin_toolbar_js(plugin) # Add the toolbar javascript for this plugin to the # sekizai "js" namespace. context[get_varname()]['js'].append( '<script data-cms>{}</script>'.format(plugin_js))
def validate_context(context): """ Validates a given context. Returns True if the context is valid. Returns False if the context is invalid but the error should be silently ignored. Raises a TemplateSyntaxError if the context is invalid and we're in debug mode. """ try: template_debug = context.template.engine.debug except AttributeError: # Get the default engine debug value template_debug = template.Engine.get_default().debug if get_varname() in context: return True if not template_debug: return False raise template.TemplateSyntaxError( "You must enable the 'sekizai.context_processors.sekizai' template " "context processor or use 'sekizai.context.SekizaiContext' to " "render your templates." )
def test_watcher_add_namespace(self): context = SekizaiContext() watcher = Watcher(context) varname = get_varname() context[varname]['key'].append('value') changes = watcher.get_changes() self.assertEqual(changes, {'key': ['value']})
def restore_sekizai_context(context, changes): varname = get_varname() sekizai_container = context.get(varname) for key, values in changes.items(): sekizai_namespace = sekizai_container[key] for value in values: sekizai_namespace.append(value)
def render_tag(self, context, name, strip, nodelist): rendered_contents = nodelist.render(context) if strip: rendered_contents = rendered_contents.strip() varname = get_varname() context[varname][name].append(rendered_contents) return ""
def render_tag(self, context, datafile): from sekizai.helpers import get_varname from cmsplugin_cascade.strides import StrideContentRenderer jsonfile = finders.find(datafile) if not jsonfile: raise IOError("Unable to find file: {}".format(datafile)) with io.open(jsonfile) as fp: tree_data = json.load(fp) content_renderer = StrideContentRenderer(context['request']) with context.push(cms_content_renderer=content_renderer): content = content_renderer.render_cascade(context, tree_data) # some templates use Sekizai's templatetag `addtoblock` or `add_data`, which have to be re-added to the context cache = caches['default'] if cache: varname = get_varname() SEKIZAI_CONTENT_HOLDER = cache.get_or_set(varname, context.get(varname)) if SEKIZAI_CONTENT_HOLDER: for name in SEKIZAI_CONTENT_HOLDER: context[varname][name] = SEKIZAI_CONTENT_HOLDER[name] return content
def render_table(context, section): if not hasattr(section, 'table'): return context tables = section.table.all() if tables.count() < 1: return context image = None if not len(tables) > 1: if hasattr(section, 'image'): image = section.image return { 'tables': tables, 'image': image, 'section': section, get_varname(): context[get_varname()], }
def validate_context(context): """ Validates a given context. Returns True if the context is valid. Returns False if the context is invalid but the error should be silently ignored. Raises a TemplateSyntaxError if the context is invalid and we're in debug mode. """ try: template_debug = context.template.engine.debug except AttributeError: # Get the default engine debug value template_debug = template.Engine.get_default().debug if get_varname() in context: return True if not template_debug: return False raise template.TemplateSyntaxError( "You must enable the 'sekizai.context_processors.sekizai' template " "context processor or use 'sekizai.context.SekizaiContext' to " "render your templates.")
def merge_sekizai_data(context, sub_context): """Utility function to call in post_render to merge the sekizai data in the component context into the global context.""" varname = get_varname() for sub_block, sub_values in sub_context[varname].items(): for item in sub_values: if item not in context[varname][sub_block]: context[varname][sub_block].append(item)
def render_tag(self, context, name, variable, inner_nodelist, nodelist): rendered_contents = nodelist.render(context) varname = get_varname() data = context[varname][name] context.push() context[variable] = data inner_contents = inner_nodelist.render(context) context.pop() return '%s\n%s' % (inner_contents, rendered_contents)
def remember(self, context, count): #print (count) #print (count.__class__) name = 'css' varname = get_varname() context[varname]['responsive-img-count'] = '%s' % (count + 1) style_tag = self.style(300, count) context[varname][name].append(style_tag) if count == '1': context[varname][name].append('<style>%s</style>' % GLOBAL_STYLE)
def render_tag(self, context, image, ratio, nodelist): self.image = ResponsiveImage(image, ratio) varname = get_varname() count = context[varname].get('responsive-img-count', '1') try: count = int(count) except TypeError: count = 1 self.remember(context, count) return self.html(image, ratio, count)
def render_tag(self, context, name, postprocessor, nodelist): if not validate_context(context): return nodelist.render(context) rendered_contents = nodelist.render(context) varname = get_varname() data = '\n'.join(context[varname][name]) if postprocessor: func = import_processor(postprocessor) data = func(context, data, name) return f'{data}\n{rendered_contents}'
def render_tag(self, context, name, strip, preprocessor, nodelist): rendered_contents = nodelist.render(context) if strip: rendered_contents = rendered_contents.strip() if preprocessor: func = import_processor(preprocessor) rendered_contents = func(context, rendered_contents, name) varname = get_varname() context[varname][name].append(rendered_contents) return ""
def render_tag(self, context, name, postprocessor, nodelist): if not validate_context(context): return nodelist.render(context) rendered_contents = nodelist.render(context) varname = get_varname() data = context[varname][name].render() if postprocessor: func = import_processor(postprocessor) data = func(context, data, name) return u'%s\n%s' % (data.decode("utf-8"), rendered_contents)
def render_tag(self, context, name, postprocessor, nodelist): if not validate_context(context): return nodelist.render(context) rendered_contents = nodelist.render(context) varname = get_varname() data = context[varname][name].render() if postprocessor: func = import_processor(postprocessor) data = func(context, data, name) return '%s\n%s' % (data, rendered_contents)
def test_render_plugin_toolbar_config(self): """ Ensures that the render_plugin_toolbar_config tag sets the correct values in the sekizai context. """ page = self._getfirst() placeholder = page.placeholders.get(slot='body') parent_plugin = add_plugin(placeholder, 'SolarSystemPlugin', 'en') child_plugin_1 = add_plugin(placeholder, 'PlanetPlugin', 'en', target=parent_plugin) child_plugin_2 = add_plugin(placeholder, 'PlanetPlugin', 'en', target=parent_plugin) parent_plugin.child_plugin_instances = [ child_plugin_1, child_plugin_2, ] plugins = [ parent_plugin, child_plugin_1, child_plugin_2, ] with self.login_user_context(self.get_superuser()): context = self.get_context(path=page.get_absolute_url(), page=page) context['request'].toolbar = CMSToolbar(context['request']) context['request'].toolbar.edit_mode = True context[get_varname()] = defaultdict(UniqueSequence) content_renderer = context['cms_content_renderer'] output = content_renderer.render_plugin(instance=parent_plugin, context=context, placeholder=placeholder, editable=True) tag_format = '<template class="cms-plugin cms-plugin-start cms-plugin-{}">' for plugin in plugins: start_tag = tag_format.format(plugin.pk) self.assertIn(start_tag, output)
def test_render_plugin_toolbar_config(self): """ Ensures that the render_plugin_toolbar_config tag sets the correct values in the sekizai context. """ page = self._getfirst() placeholder = page.placeholders.get(slot='body') parent_plugin = add_plugin(placeholder, 'SolarSystemPlugin', 'en') child_plugin_1 = add_plugin(placeholder, 'PlanetPlugin', 'en', target=parent_plugin) child_plugin_2 = add_plugin(placeholder, 'PlanetPlugin', 'en', target=parent_plugin) parent_plugin.child_plugin_instances = [ child_plugin_1, child_plugin_2, ] plugins = [ parent_plugin, child_plugin_1, child_plugin_2, ] with self.login_user_context(self.get_superuser()): context = self.get_context(path=page.get_absolute_url(), page=page) context['request'].toolbar = CMSToolbar(context['request']) context['request'].toolbar.edit_mode = True context[get_varname()] = defaultdict(UniqueSequence) content_renderer = context['cms_content_renderer'] output = content_renderer.render_plugin( instance=parent_plugin, context=context, placeholder=placeholder, editable=True ) tag_format = '<template class="cms-plugin cms-plugin-start cms-plugin-{}">' for plugin in plugins: start_tag = tag_format.format(plugin.pk) self.assertIn(start_tag, output)
def validate_context(context): """ Validates a given context. Returns True if the context is valid. Returns False if the context is invalid but the error should be silently ignored. Raises a TemplateSyntaxError if the context is invalid and we're in debug mode. """ if get_varname() in context: return True if not settings.TEMPLATE_DEBUG: return False raise template.TemplateSyntaxError( "You must enable the 'sekizai.context_processors.sekizai' template " "context processor or use 'sekizai.context.SekizaiContext' to " "render your templates.")
def validate_context(context): """ Validates a given context. Returns True if the context is valid. Returns False if the context is invalid but the error should be silently ignored. Raises a TemplateSyntaxError if the context is invalid and we're in debug mode. """ if get_varname() in context: return True if not settings.TEMPLATE_DEBUG: return False raise template.TemplateSyntaxError( "You must enable the 'sekizai.context_processors.sekizai' template " "context processor or use 'sekizai.context.SekizaiContext' to " "render your templates." )
def render_addons(self, context): # django CMS 3.4 compatibility context[get_varname()]['css'].append(self.icon_css) return []
def test_alias_placeholder_is_not_editable(self): """ When a placeholder is aliased, it shouldn't render as editable in the structure mode. """ source_page = api.create_page( "Home", "col_two.html", "en", published=True, in_navigation=True, ) source_placeholder = source_page.placeholders.get(slot="col_left") style = api.add_plugin( source_placeholder, 'StylePlugin', 'en', tag_type='div', class_name='info', ) target_page = api.create_page( "Target", "col_two.html", "en", published=True, in_navigation=True, ) target_placeholder = target_page.placeholders.get(slot="col_left") alias = api.add_plugin( target_placeholder, 'AliasPlugin', 'en', alias_placeholder=source_placeholder, ) with self.login_user_context(self.get_superuser()): context = self.get_context(path=target_page.get_absolute_url(), page=target_page) context['request'].toolbar = CMSToolbar(context['request']) context['request'].toolbar.edit_mode = True context[get_varname()] = defaultdict(UniqueSequence) content_renderer = context['cms_content_renderer'] output = content_renderer.render_placeholder( target_placeholder, context=context, language='en', page=target_page, editable=True ) tag_format = '<template class="cms-plugin cms-plugin-start cms-plugin-{}">' expected_plugins = [alias] unexpected_plugins = [style] for plugin in expected_plugins: start_tag = tag_format.format(plugin.pk) self.assertIn(start_tag, output) for plugin in unexpected_plugins: start_tag = tag_format.format(plugin.pk) self.assertNotIn(start_tag, output) editable_placeholders = content_renderer.get_rendered_editable_placeholders() self.assertNotIn(source_placeholder,editable_placeholders)
def sekizai(request=None): """ Simple context processor which makes sure that the SekizaiDictionary is available in all templates. """ return {get_varname(): SekizaiDictionary()}
def data(self): return self.context.get(get_varname(), {})
def sekizai(request=None): """ Simple context processor which makes sure that the SekizaiDictionary is available in all templates. """ return {get_varname(): defaultdict(UniqueSequence)}
def sekizai_add_to_block(context, block, value): """Sekizai has no python API yet so gotta hack it.""" context[get_varname()][block].append(value)
def render_tag(self, context, key, value): varname = get_varname() context[varname][key].append(value) return ''
def render(self, context): seq = context[get_varname()][self.block_name] seq.data += [self.thingie] return ""
def render(self, context): print(context[get_varname()]) seq = context[get_varname()][self.block_name] return seq.data