def render_placeholder(placeholder, context_to_copy, name_fallback="Placeholder"):
    """
    Renders plugins for a placeholder on the given page using shallow copies of the 
    given context, and returns a string containing the rendered output.
    """
    from cms.plugins.utils import get_plugins

    context = context_to_copy
    context.push()
    request = context["request"]
    plugins = [plugin for plugin in get_plugins(request, placeholder)]
    page = get_page_from_placeholder_if_exists(placeholder)
    if page:
        template = page.template
    else:
        template = None
        fallback = getattr(django_settings, "CMS_PLACEHOLDER_LANG_FALLBACK", None)
        if not plugins and fallback:
            plugins = [plugin for plugin in get_plugins(request, placeholder, lang=fallback)]
    # Add extra context as defined in settings, but do not overwrite existing context variables,
    # since settings are general and database/template are specific
    # TODO this should actually happen as a plugin context processor, but these currently overwrite
    # existing context -- maybe change this order?
    slot = getattr(placeholder, "slot", None)
    extra_context = {}
    if slot:
        extra_context = settings.CMS_PLACEHOLDER_CONF.get("%s %s" % (template, slot), {}).get("extra_context", None)
        if not extra_context:
            extra_context = settings.CMS_PLACEHOLDER_CONF.get(slot, {}).get("extra_context", {})
    for key, value in extra_context.items():
        if not key in context:
            context[key] = value

    c = []

    # Prepend frontedit toolbar output if applicable
    edit = False
    if (
        ("edit" in request.GET or request.session.get("cms_edit", False))
        and "cms.middleware.toolbar.ToolbarMiddleware" in django_settings.MIDDLEWARE_CLASSES
        and request.user.is_staff
        and request.user.is_authenticated()
        and (not page or page.has_change_permission(request))
    ):
        edit = True
    if edit:
        from cms.middleware.toolbar import toolbar_plugin_processor

        processors = (toolbar_plugin_processor,)
    else:
        processors = None

    c.extend(render_plugins(plugins, context, placeholder, processors))
    content = "".join(c)
    if edit:
        content = render_placeholder_toolbar(placeholder, context, content, name_fallback)
    context.pop()
    return content
def render_placeholder(placeholder, context_to_copy, name_fallback="Placeholder"):
    """
    Renders plugins for a placeholder on the given page using shallow copies of the 
    given context, and returns a string containing the rendered output.
    """
    from cms.plugins.utils import get_plugins
    context = context_to_copy 
    context.push()
    request = context['request']
    lang = get_language_from_request(request)
    page = get_page_from_placeholder_if_exists(placeholder)
    if page:
        template = page.template
    else:
        template = None
    plugins = [plugin for plugin in get_plugins(request, placeholder, lang)]
    if (len(plugins)==0 and placeholder and lang != get_default_language() and
        get_placeholder_conf("language_fallback", placeholder.slot, template, False)):
        fallbacks = get_fallback_languages(lang)
        for l in fallbacks:
            plugins = [plugin for plugin in get_plugins(request, placeholder, l)]
            if plugins:
                break
    # Add extra context as defined in settings, but do not overwrite existing context variables,
    # since settings are general and database/template are specific
    # TODO this should actually happen as a plugin context processor, but these currently overwrite 
    # existing context -- maybe change this order?
    slot = getattr(placeholder, 'slot', None)
    extra_context = {}
    if slot:
        extra_context = get_placeholder_conf("extra_context", slot, template, {})
    for key, value in extra_context.items():
        if not key in context:
            context[key] = value

    content = []

    # Prepend frontedit toolbar output if applicable
    edit = False
    toolbar = getattr(request, 'toolbar', None)
    
    if (getattr(toolbar, 'edit_mode', False) and
        (not page or page.has_change_permission(request))):
            edit = True
    if edit:
        from cms.middleware.toolbar import toolbar_plugin_processor
        processors = (toolbar_plugin_processor,)
    else:
        processors = None 

    content.extend(render_plugins(plugins, context, placeholder, processors))
    content = "".join(content)
    if edit:
        content = render_placeholder_toolbar(placeholder, context, content, name_fallback)
    context.pop()
    return content
Example #3
0
def render_placeholder(placeholder, context_to_copy, name_fallback="Placeholder"):
    """
    Renders plugins for a placeholder on the given page using shallow copies of the 
    given context, and returns a string containing the rendered output.
    """
    from cms.plugins.utils import get_plugins
    context = context_to_copy 
    context.push()
    request = context['request']
    lang = get_language_from_request(request)
    plugins = [plugin for plugin in get_plugins(request, placeholder)]
    if not plugins and settings.CMS_LANGUAGE_FALLBACK and lang != get_default_language():
        fallbacks = get_fallback_languages(lang)
        for l in fallbacks:
            plugins = [plugin for plugin in get_plugins(request, placeholder, l)]
            if plugins:
                break

    page = get_page_from_placeholder_if_exists(placeholder)
    if page:
        template = page.template
    else:
        template = None
    # Add extra context as defined in settings, but do not overwrite existing context variables,
    # since settings are general and database/template are specific
    # TODO this should actually happen as a plugin context processor, but these currently overwrite 
    # existing context -- maybe change this order?
    slot = getattr(placeholder, 'slot', None)
    extra_context = {}
    if slot:
        extra_context = get_placeholder_conf("extra_context", slot, template, {})
    for key, value in extra_context.items():
        if not key in context:
            context[key] = value

    c = []

    # Prepend frontedit toolbar output if applicable
    edit = False
    toolbar = getattr(request, 'toolbar', None)
    
    if (getattr(toolbar, 'edit_mode', False) and
        (not page or page.has_change_permission(request))):
            edit = True
    if edit:
        from cms.middleware.toolbar import toolbar_plugin_processor
        processors = (toolbar_plugin_processor,)
    else:
        processors = None 

    c.extend(render_plugins(plugins, context, placeholder, processors))
    content = "".join(c)
    if edit:
        content = render_placeholder_toolbar(placeholder, context, content, name_fallback)
    context.pop()
    return content
Example #4
0
def get_placeholder_content(context, request, current_page, name, inherit):
    edit_mode = getattr(request, 'toolbar', None) and getattr(request.toolbar, 'edit_mode')
    pages = [current_page]
    # don't display inherited plugins in edit mode, so that the user doesn't
    # mistakenly edit/delete them. This is a fix for issue #1303. See the discussion
    # there for possible enhancements
    if inherit and not edit_mode:
        pages = chain([current_page], current_page.get_cached_ancestors(ascending=True))
    for page in pages:
        placeholder = _get_placeholder(current_page, page, context, name)
        if placeholder is None:
            continue
        if not get_plugins(request, placeholder):
            continue
        
        # @modified wej
        # ToDo: Add snippet processing logic
        content = render_placeholder(placeholder, context, name)
        if content:
            return content
            # if we reach this point, we have an empty or non-existant placeholder
            # call _get_placeholder again to get the placeholder properly rendered
            # in frontend editing
    placeholder = _get_placeholder(current_page, current_page, context, name)
    return render_placeholder(placeholder, context, name)
Example #5
0
 def get_content(self, request, current_page, context):
     pages = [current_page]
     if self.inherit:
         pages = chain([current_page],
                       current_page.get_cached_ancestors(ascending=True))
     for page in pages:
         template = get_template_from_request(request, page)
         placeholder = self._get_placeholder(current_page, page, context,
                                             self.name)
         if placeholder is None:
             continue
         if not get_plugins(request, placeholder):
             continue
         if hasattr(request, 'placeholder_media'):
             request.placeholder_media = reduce(operator.add, [
                 request.placeholder_media,
                 placeholder.get_media(request, context)
             ])
         #request.placeholder_media += placeholder.get_media(request, context)
         content = render_placeholder(placeholder, context)
         if content:
             return content, placeholder
     placeholder = self._get_placeholder(current_page, current_page,
                                         context, self.name)
     content = render_placeholder(placeholder, context)
     return content, placeholder
def render_placeholder(placeholder,
                       context_to_copy,
                       name_fallback="Placeholder"):
    """
    Renders plugins for a placeholder on the given page using shallow copies of the 
    given context, and returns a string containing the rendered output.
    """
    from cms.plugins.utils import get_plugins
    context = context_to_copy
    context.push()
    request = context['request']
    plugins = [plugin for plugin in get_plugins(request, placeholder)]
    page = get_page_from_placeholder_if_exists(placeholder)
    if page:
        template = page.template
    else:
        template = None
    # Add extra context as defined in settings, but do not overwrite existing context variables,
    # since settings are general and database/template are specific
    # TODO this should actually happen as a plugin context processor, but these currently overwrite
    # existing context -- maybe change this order?
    slot = getattr(placeholder, 'slot', None)
    extra_context = {}
    if slot:
        extra_context = settings.CMS_PLACEHOLDER_CONF.get(
            "%s %s" % (template, slot), {}).get("extra_context", None)
        if not extra_context:
            extra_context = settings.CMS_PLACEHOLDER_CONF.get(slot, {}).get(
                "extra_context", {})
    for key, value in extra_context.items():
        if not key in context:
            context[key] = value

    c = []

    # Prepend frontedit toolbar output if applicable
    edit = False
    if ("edit" in request.GET or request.session.get("cms_edit", False)) and \
        'cms.middleware.toolbar.ToolbarMiddleware' in django_settings.MIDDLEWARE_CLASSES and \
        request.user.is_staff and request.user.is_authenticated() and \
        (not page or page.has_change_permission(request)):
        edit = True
    if edit:
        from cms.middleware.toolbar import toolbar_plugin_processor
        processors = (toolbar_plugin_processor, )
    else:
        processors = None

    c.extend(render_plugins(plugins, context, placeholder, processors))
    content = "".join(c)
    if edit:
        content = render_placeholder_toolbar(placeholder, context, content,
                                             name_fallback)
    context.pop()
    return content
 def render(self, context, instance, placeholder):
     # TODO: once we drop 2.3.x support we can just use the "render_plugin" templatetag
     #       instead of rendering html here.
     plugins = get_plugins(context['request'], instance.stack.content)
     processors = ()
     html_content = mark_safe(u"".join(render_plugins(plugins, context, placeholder, processors)))
     context.update({
         'instance': instance,
         'placeholder': placeholder,
         'content': html_content,
     })
     return context
Example #8
0
def render_plugins_for_context(placeholder_name, page, context_to_copy, width=None):
    """
    renders plugins for the given named placedholder and page using shallow copies of the 
    given context
    """
    context = copy.copy(context_to_copy) 
    l = get_language_from_request(context['request'])
    request = context['request']
    plugins = [plugin for plugin in get_plugins(request, page) if plugin.placeholder == placeholder_name]
    template = page.template
    extra_context = settings.CMS_PLACEHOLDER_CONF.get("%s %s" % (template, placeholder_name), {}).get("extra_context", None)
    if not extra_context:
        extra_context = settings.CMS_PLACEHOLDER_CONF.get(placeholder_name, {}).get("extra_context", None)
    if extra_context:
        context.update(extra_context)
    if width:
        # this may overwrite previously defined key [width] from settings.CMS_PLACEHOLDER_CONF
        try:
            width = int(width)
            context.update({'width': width,})
        except ValueError:
            pass
    c = []
    edit = False
    if ("edit" in request.GET or request.session.get("cms_edit", False)) and \
            'cms.middleware.toolbar.ToolbarMiddleware' in django_settings.MIDDLEWARE_CLASSES and \
            request.user.is_staff and request.user.is_authenticated() and \
            page.has_change_permission(request):
        edit = True
    if edit:
        installed_plugins = plugin_pool.get_all_plugins(placeholder_name, page)
        name = settings.CMS_PLACEHOLDER_CONF.get("%s %s" % (template, placeholder_name), {}).get("name", None)
        if not name:
            name = settings.CMS_PLACEHOLDER_CONF.get(placeholder_name, {}).get("name", None)
        if not name:
            name = placeholder_name
        name = title(name)
        c.append(render_to_string("cms/toolbar/add_plugins.html", {'installed_plugins':installed_plugins,
                                                               'language':l,
                                                               'placeholder_label':name,
                                                               'placeholder_name':placeholder_name,
                                                               'page':page,
                                                               }))
        from cms.middleware.toolbar import toolbar_plugin_processor
        processors = (toolbar_plugin_processor,)
    else:
        processors = None 
        
    c.extend(render_plugins(plugins, context, placeholder_name, processors))
    
    return "".join(c)
Example #9
0
def render_placeholder(placeholder, context_to_copy, name_fallback="Placeholder", lang=None):
    """
    Renders plugins for a placeholder on the given page using shallow copies of the 
    given context, and returns a string containing the rendered output.
    """
    from cms.plugins.utils import get_plugins

    context = context_to_copy
    context.push()
    request = context["request"]
    plugins = [plugin for plugin in get_plugins(request, placeholder, lang=lang)]
    page = placeholder.page if placeholder else None
    if page:
        template = page.template
    else:
        template = None
    # Add extra context as defined in settings, but do not overwrite existing context variables,
    # since settings are general and database/template are specific
    # TODO this should actually happen as a plugin context processor, but these currently overwrite
    # existing context -- maybe change this order?
    slot = getattr(placeholder, "slot", None)
    extra_context = {}
    if slot:
        extra_context = get_placeholder_conf("extra_context", slot, template, {})
    for key, value in extra_context.items():
        if not key in context:
            context[key] = value

    content = []

    # Prepend frontedit toolbar output if applicable
    edit = False
    toolbar = getattr(request, "toolbar", None)

    if getattr(toolbar, "edit_mode", False) and (not page or page.has_change_permission(request)):
        edit = True
    if edit:
        from cms.middleware.toolbar import toolbar_plugin_processor

        processors = (toolbar_plugin_processor,)
    else:
        processors = None

    content.extend(render_plugins(plugins, context, placeholder, processors))
    content = "".join(content)
    if edit:
        content = render_placeholder_toolbar(placeholder, context, content, name_fallback)
    context.pop()
    return content
Example #10
0
 def get_content(self, request, page, context):
     from cms.utils.plugins import get_placeholders
     pages = [page]
     if self.inherit:
         pages = chain([page], page.get_cached_ancestors(ascending=True))
     for page in pages:
         template = get_template_from_request(request, page)
         placeholder = page.placeholders.filter(slot__in=get_placeholders(template)).get(slot=self.name)
         if not get_plugins(request, placeholder):
             continue
         request.placeholder_media += placeholder.get_media(request, context)
         content = render_placeholder(placeholder, context)
         if content:
             return content
     return ''
Example #11
0
def get_placeholder_content(context, request, current_page, name, inherit):
    pages = [current_page]
    if inherit:
        pages = chain([current_page], current_page.get_cached_ancestors(ascending=True))
    for page in pages:
        placeholder = _get_placeholder(current_page, page, context, name)
        if placeholder is None:
            continue
        if not get_plugins(request, placeholder):
            continue
        content = render_placeholder(placeholder, context, name)
        if content:
            return content
    placeholder = _get_placeholder(current_page, current_page, context, name)
    return render_placeholder(placeholder, context, name)
Example #12
0
 def save_model(self, request, entry, form, change):
     """Fill the content field with the interpretation
     of the placeholder"""
     context = RequestContext(request)
     #dm
     processors = iterload_objects(settings.CMS_PLUGIN_PROCESSORS)
     plugins = [plugin for plugin in get_plugins(request, entry.content_placeholder)]
     render = render_plugins(plugins, context, entry.content_placeholder)
     content = "".join(render)
     entry.placeholder_render = content
     #entry.placeholder_render = render_placeholder(entry.content_placeholder, context )
     #entry.content = render_placeholder(entry.content_placeholder, context)
     entry.save()
     super(EntryPlaceholderAdmin, self).save_model(
         request, entry, form, change)
Example #13
0
def get_placeholder_content(context, request, current_page, name, inherit):
    pages = [current_page]
    if inherit:
        pages = chain([current_page],
                      current_page.get_cached_ancestors(ascending=True))
    for page in pages:
        placeholder = _get_placeholder(current_page, page, context, name)
        if placeholder is None:
            continue
        if not get_plugins(request, placeholder):
            continue
        content = render_placeholder(placeholder, context, name)
        if content:
            return content
    placeholder = _get_placeholder(current_page, current_page, context, name)
    return render_placeholder(placeholder, context, name)
Example #14
0
def render_plugins_for_context(placeholder_name, page, context_to_copy, theme=None):
    """
    renders plugins for the given named placedholder and page using shallow copies of the 
    given context
    """
    context = copy.copy(context_to_copy)
    l = get_language_from_request(context["request"])
    request = context["request"]
    plugins = [plugin for plugin in get_plugins(request, page) if plugin.placeholder == placeholder_name]
    if settings.CMS_PLACEHOLDER_CONF and placeholder_name in settings.CMS_PLACEHOLDER_CONF:
        if "extra_context" in settings.CMS_PLACEHOLDER_CONF[placeholder_name]:
            context.update(settings.CMS_PLACEHOLDER_CONF[placeholder_name]["extra_context"])
    if theme:
        # this may overwrite previously defined key [theme] from settings.CMS_PLACEHOLDER_CONF
        context.update({"theme": theme})
    c = []
    edit = False
    if (
        ("edit" in request.GET or request.session.get("cms_edit", False))
        and "cms.middleware.toolbar.ToolbarMiddleware" in django_settings.MIDDLEWARE_CLASSES
        and request.user.is_staff
        and request.user.is_authenticated
    ):
        edit = True
    if edit:
        installed_plugins = plugin_pool.get_all_plugins(placeholder_name, page)
        name = placeholder_name
        if settings.CMS_PLACEHOLDER_CONF and placeholder_name in settings.CMS_PLACEHOLDER_CONF:
            if "name" in settings.CMS_PLACEHOLDER_CONF[placeholder_name]:
                name = settings.CMS_PLACEHOLDER_CONF[placeholder_name]["name"]
        name = title(name)
        c.append(
            render_to_string(
                "cms/toolbar/add_plugins.html",
                {
                    "installed_plugins": installed_plugins,
                    "language": request.LANGUAGE_CODE,
                    "placeholder_label": name,
                    "placeholder_name": placeholder_name,
                    "page": page,
                },
            )
        )
    for index, plugin in enumerate(plugins):
        context["plugin_index"] = index
        c.append(plugin.render_plugin(copy.copy(context), placeholder_name, edit=edit))
    return "".join(c)
    def render(self, context, instance, placeholder):
        """
        Render the latest entries
        """
        placeholder = instance.links_page.placeholders.get(
            slot=instance.placeholder_name)
        links = get_plugins(context["request"], placeholder).filter(
            plugin_type="LinkPlugin")
        if instance.count >= 0:
            links = links[:instance.count]

        context.update({
                'instance': instance,
                'links': links,
                'placeholder': placeholder,
                })
        return context
Example #16
0
 def get_content(self, request, current_page, context):
     pages = [current_page]
     if self.inherit:
         pages = chain([current_page], current_page.get_cached_ancestors(ascending=True))
     for page in pages:
         template = get_template_from_request(request, page)
         placeholder = self._get_placeholder(current_page, page, context, self.name)
         if placeholder is None:
             continue
         if not get_plugins(request, placeholder):
             continue
         if hasattr(request, 'placeholder_media'):
             request.placeholder_media = reduce(operator.add, [request.placeholder_media, placeholder.get_media(request, context)])
         #request.placeholder_media += placeholder.get_media(request, context)
         content = render_placeholder(placeholder, context)
         if content:
             return content, placeholder
     return '', None
Example #17
0
 def save_model(self, request, entry, form, change):
     """Fill the content field with the interpretation
     of the placeholder"""
     context = RequestContext(request)
     #dm
     processors = iterload_objects(settings.CMS_PLUGIN_PROCESSORS)
     plugins = [
         plugin
         for plugin in get_plugins(request, entry.content_placeholder)
     ]
     render = render_plugins(plugins, context, entry.content_placeholder)
     content = "".join(render)
     entry.placeholder_render = content
     #entry.placeholder_render = render_placeholder(entry.content_placeholder, context )
     #entry.content = render_placeholder(entry.content_placeholder, context)
     entry.save()
     super(EntryPlaceholderAdmin, self).save_model(request, entry, form,
                                                   change)
Example #18
0
def get_placeholder_content(context, request, current_page, name, inherit):
    pages = [current_page]
    if inherit:
        pages = chain([current_page], current_page.get_cached_ancestors(ascending=True))
    for page in pages:
        placeholder = _get_placeholder(current_page, page, context, name)
        if placeholder is None:
            continue
        if not get_plugins(request, placeholder):
            continue
        if hasattr(request, 'placeholder_media'):
            request.placeholder_media = reduce(operator.add, [request.placeholder_media, placeholder.get_media(request, context)])
        #request.placeholder_media += placeholder.get_media(request, context)
        content = render_placeholder(placeholder, context, name)
        if content:
            return content
    placeholder = _get_placeholder(current_page, current_page, context, name)
    return render_placeholder(placeholder, context, name)
Example #19
0
def render_plugins_for_context(placeholder_name, page, context_to_copy, width=None):
    """
    renders plugins for the given named placedholder and page using shallow copies of the 
    given context
    """
    context = copy.copy(context_to_copy) 
    l = get_language_from_request(context['request'])
    request = context['request']
    plugins = [plugin for plugin in get_plugins(request, page) if plugin.placeholder == placeholder_name]
    if settings.CMS_PLACEHOLDER_CONF and placeholder_name in settings.CMS_PLACEHOLDER_CONF:
        if "extra_context" in settings.CMS_PLACEHOLDER_CONF[placeholder_name]:
            context.update(settings.CMS_PLACEHOLDER_CONF[placeholder_name]["extra_context"])
    if width:
        # this may overwrite previously defined key [width] from settings.CMS_PLACEHOLDER_CONF
        try:
            width = int(width)
            context.update({'width': width,})
        except ValueError:
            pass
    c = []
    edit = False
    if ("edit" in request.GET or request.session.get("cms_edit", False)) and 'cms.middleware.toolbar.ToolbarMiddleware' in django_settings.MIDDLEWARE_CLASSES and request.user.is_staff and request.user.is_authenticated:
        edit = True
    if edit:
        installed_plugins = plugin_pool.get_all_plugins(placeholder_name, page)
        name = placeholder_name
        if settings.CMS_PLACEHOLDER_CONF and placeholder_name in settings.CMS_PLACEHOLDER_CONF:
            if "name" in settings.CMS_PLACEHOLDER_CONF[placeholder_name]:
                name = settings.CMS_PLACEHOLDER_CONF[placeholder_name]['name']
        name = title(name)
        c.append(render_to_string("cms/toolbar/add_plugins.html", {'installed_plugins':installed_plugins,
                                                               'language':request.LANGUAGE_CODE,
                                                               'placeholder_label':name,
                                                               'placeholder_name':placeholder_name,
                                                               'page':page,
                                                               }))
    for index, plugin in enumerate(plugins):
        context['plugin_index'] = index
        c.append(plugin.render_plugin(copy.copy(context), placeholder_name, edit=edit))
    return "".join(c)
Example #20
0
def get_placeholder_content(context, request, current_page, name, inherit):
    edit_mode = getattr(request, 'toolbar', None) and getattr(request.toolbar, 'edit_mode')
    pages = [current_page]
    # don't display inherited plugins in edit mode, so that the user doesn't
    # mistakenly edit/delete them. This is a fix for issue #1303. See the discussion
    # there for possible enhancements
    if inherit and not edit_mode:
        pages = chain([current_page], current_page.get_cached_ancestors(ascending=True))
    for page in pages:
        placeholder = _get_placeholder(current_page, page, context, name)
        if placeholder is None:
            continue
        if not get_plugins(request, placeholder):
            continue
        content = render_placeholder(placeholder, context, name)
        if content:
            return content
            # if we reach this point, we have an empty or non-existant placeholder
            # call _get_placeholder again to get the placeholder properly rendered
            # in frontend editing
    placeholder = _get_placeholder(current_page, current_page, context, name)
    return render_placeholder(placeholder, context, name)
Example #21
0
def get_placeholder_content(context, request, current_page, name, inherit):
    pages = [current_page]
    if inherit:
        pages = chain([current_page],
                      current_page.get_cached_ancestors(ascending=True))
    for page in pages:
        placeholder = _get_placeholder(current_page, page, context, name)
        if placeholder is None:
            continue
        if not get_plugins(request, placeholder):
            continue
        if hasattr(request, 'placeholder_media'):
            request.placeholder_media = reduce(operator.add, [
                request.placeholder_media,
                placeholder.get_media(request, context)
            ])
        #request.placeholder_media += placeholder.get_media(request, context)
        content = render_placeholder(placeholder, context, name)
        if content:
            return content
    placeholder = _get_placeholder(current_page, current_page, context, name)
    return render_placeholder(placeholder, context, name)
Example #22
0
 def render_tag(self, context, code, varname):
     # TODO: language override (the reason this is not implemented, is that language selection is buried way
     #       down somewhere in some method called in render_plugins. There it gets extracted from the request
     #       and a language in request.GET always overrides everything.)
     if not code:
         # an empty string was passed in or the variable is not available in the context
         return ''
     # TODO: caching?
     if isinstance(code, Stack):
         stack = code
     else:
         stack, __ = stack_models.Stack.objects.get_or_create(code=code, defaults={'name': code})
     # TODO: once we drop 2.3.x support we can just use the "render_plugin" templatetag
     #       instead of rendering html here.
     placeholder = stack.content
     plugins = get_plugins(context['request'], placeholder)
     processors = ()
     rendered_placeholder = mark_safe("".join(render_plugins(plugins, context, placeholder, processors)))
     if varname:
         context[varname] = rendered_placeholder
         rendered_placeholder = u''
     return rendered_placeholder
Example #23
0
 def render_tag(self, context, code, varname):
     # TODO: language override (the reason this is not implemented, is that language selection is buried way
     #       down somewhere in some method called in render_plugins. There it gets extracted from the request
     #       and a language in request.GET always overrides everything.)
     if not code:
         # an empty string was passed in or the variable is not available in the context
         return ''
     # TODO: caching?
     if isinstance(code, Stack):
         stack = code
     else:
         stack, __ = stack_models.Stack.objects.get_or_create(
             code=code, defaults={'name': code})
     # TODO: once we drop 2.3.x support we can just use the "render_plugin" templatetag
     #       instead of rendering html here.
     placeholder = stack.content
     plugins = get_plugins(context['request'], placeholder)
     processors = ()
     rendered_placeholder = mark_safe("".join(
         render_plugins(plugins, context, placeholder, processors)))
     if varname:
         context[varname] = rendered_placeholder
         rendered_placeholder = u''
     return rendered_placeholder
Example #24
0
def render_newsy_placeholder(placeholder, context, name_fallback="Placeholder"):
    """
    Renders plugins for a placeholder on the given page using shallow copies of the 
    given context, and returns a string containing the rendered output.
    """
    log.debug('render_newsy_placeholder(placeholder=%s)' % 
              (unicode(placeholder),))
    request = context.get('request', None)
    context.push()
    plugins = list(get_plugins(request, placeholder))
    page = get_newsitem_from_placeholder_if_exists(placeholder)
    if page:
        template = page.template
    else:
        template = None
    # Add extra context as defined in settings, but do not overwrite existing context variables,
    # since settings are general and database/template are specific
    # TODO this should actually happen as a plugin context processor, but these currently overwrite 
    # existing context -- maybe change this order?
    slot = getattr(placeholder, 'slot', None)
    extra_context = {}
    if slot:
        extra_context = get_cms_setting('PLACEHOLDER_CONF').get("%s %s" % (template, slot), {}).get("extra_context", None)
        if not extra_context:
            extra_context = get_cms_setting('PLACEHOLDER_CONF').get(slot, {}).get("extra_context", {})
    for key, value in extra_context.items():
        if not key in context:
            context[key] = value

    c = []

    processors = None 
    c.extend(render_plugins(plugins, context, placeholder, processors))
    content = "".join(c)
    context.pop()
    return content
 def _cache_plugins_for_placeholder(self, placeholder):
     """
     Calls get_plugins which sets the cache attribute on the given
     placeholder instance.
     """
     get_plugins(self.request, placeholder)
def render_placeholder(placeholder, context_to_copy):
    """
    Renders plugins for a placeholder on the given page using shallow copies of the 
    given context, and returns a string containing the rendered output.
    """
    from cms.plugins.utils import get_plugins

    context = copy.copy(context_to_copy)
    request = context["request"]
    plugins = [plugin for plugin in get_plugins(request, placeholder)]
    page = get_page_from_placeholder_if_exists(placeholder)
    if page:
        template = page.template
    else:
        template = None
    # Add extra context as defined in settings, but do not overwrite existing context variables,
    # since settings are general and database/template are specific
    # TODO this should actually happen as a plugin context processor, but these currently overwrite
    # existing context -- maybe change this order?
    extra_context = settings.CMS_PLACEHOLDER_CONF.get("%s %s" % (template, placeholder.slot), {}).get(
        "extra_context", None
    )
    if not extra_context:
        extra_context = settings.CMS_PLACEHOLDER_CONF.get(placeholder.slot, {}).get("extra_context", {})
    for key, value in extra_context.items():
        if not key in context:
            context[key] = value

    c = []

    # Prepend frontedit toolbar output if applicable
    edit = False
    if (
        ("edit" in request.GET or request.session.get("cms_edit", False))
        and "cms.middleware.toolbar.ToolbarMiddleware" in django_settings.MIDDLEWARE_CLASSES
        and request.user.is_staff
        and request.user.is_authenticated()
        and (not page or page.has_change_permission(request))
    ):
        edit = True
    if edit:
        from cms.plugin_pool import plugin_pool

        installed_plugins = plugin_pool.get_all_plugins(placeholder, page)
        name = settings.CMS_PLACEHOLDER_CONF.get("%s %s" % (template, placeholder.slot), {}).get("name", None)
        if not name:
            name = settings.CMS_PLACEHOLDER_CONF.get(placeholder.slot, {}).get("name", None)
        if not name:
            name = placeholder.slot
        name = title(name)
        c.append(
            render_to_string(
                "cms/toolbar/add_plugins.html",
                {
                    "installed_plugins": installed_plugins,
                    "language": get_language_from_request(request),
                    "placeholder_label": name,
                    "placeholder": placeholder,
                    "page": page,
                },
            )
        )
        from cms.middleware.toolbar import toolbar_plugin_processor

        processors = (toolbar_plugin_processor,)
    else:
        processors = None

    c.extend(render_plugins(plugins, context, placeholder, processors))
    return "".join(c)
def render_placeholder(placeholder,
                       context_to_copy,
                       name_fallback="Placeholder",
                       lang=None):
    """
    Renders plugins for a placeholder on the given page using shallow copies of the
    given context, and returns a string containing the rendered output.
    """
    from cms.plugins.utils import get_plugins

    context = context_to_copy
    context.push()
    request = context['request']
    page = placeholder.page if placeholder else None
    if page:
        template = page.template
    else:
        template = None
    # It's kind of duplicate of the similar call in `get_plugins`, but it's required
    # to have a valid language in this function for `get_fallback_languages` to work
    if not lang:
        lang = get_language_from_request(request)
    plugins = [
        plugin for plugin in get_plugins(request, placeholder, lang=lang)
    ]
    # If no plugin is present in the current placeholder we loop in the fallback languages
    # and get the first available set of plugins
    if (len(plugins) == 0 and placeholder and get_placeholder_conf(
            "language_fallback", placeholder.slot, template, False)):
        fallbacks = get_fallback_languages(lang)
        for fallback_language in fallbacks:
            plugins = [
                plugin for plugin in get_plugins(request, placeholder,
                                                 fallback_language)
            ]
            if plugins:
                break
    # Add extra context as defined in settings, but do not overwrite existing context variables,
    # since settings are general and database/template are specific
    # TODO this should actually happen as a plugin context processor, but these currently overwrite
    # existing context -- maybe change this order?
    slot = getattr(placeholder, 'slot', None)
    extra_context = {}
    if slot:
        extra_context = get_placeholder_conf("extra_context", slot, template,
                                             {})
    for key, value in extra_context.items():
        if not key in context:
            context[key] = value

    content = []

    # Prepend frontedit toolbar output if applicable
    edit = False
    toolbar = getattr(request, 'toolbar', None)

    if (getattr(toolbar, 'edit_mode', False)
            and (not page or page.has_change_permission(request))):
        edit = True
    if edit:
        from cms.middleware.toolbar import toolbar_plugin_processor

        processors = (toolbar_plugin_processor, )
    else:
        processors = None

    content.extend(render_plugins(plugins, context, placeholder, processors))
    toolbar_content = ''
    draggable_content = ''
    if edit:
        toolbar_content = mark_safe(
            render_placeholder_toolbar(placeholder,
                                       context,
                                       '',
                                       name_fallback=name_fallback))
        draggable_content = mark_safe(render_dragables(plugins, slot, request))
    content = mark_safe("".join(content))

    result = render_to_string(
        "cms/toolbar/placeholder.html", {
            'plugins': content,
            "bar": toolbar_content,
            "draggables": draggable_content,
            'edit': edit
        })
    context.pop()
    return result
Example #28
0
def render_placeholder(placeholder, context_to_copy, name_fallback="Placeholder", lang=None):
    """
    Renders plugins for a placeholder on the given page using shallow copies of the
    given context, and returns a string containing the rendered output.
    """
    from cms.plugins.utils import get_plugins

    context = context_to_copy
    context.push()
    request = context["request"]
    page = placeholder.page if placeholder else None
    if page:
        template = page.template
    else:
        template = None
    # It's kind of duplicate of the similar call in `get_plugins`, but it's required
    # to have a valid language in this function for `get_fallback_languages` to work
    if not lang:
        lang = get_language_from_request(request)
    plugins = [plugin for plugin in get_plugins(request, placeholder, lang=lang)]
    # If no plugin is present in the current placeholder we loop in the fallback languages
    # and get the first available set of plugins
    if (
        len(plugins) == 0
        and placeholder
        and get_placeholder_conf("language_fallback", placeholder.slot, template, False)
    ):
        fallbacks = get_fallback_languages(lang)
        for fallback_language in fallbacks:
            plugins = [plugin for plugin in get_plugins(request, placeholder, fallback_language)]
            if plugins:
                break
    # Add extra context as defined in settings, but do not overwrite existing context variables,
    # since settings are general and database/template are specific
    # TODO this should actually happen as a plugin context processor, but these currently overwrite
    # existing context -- maybe change this order?
    slot = getattr(placeholder, "slot", None)
    extra_context = {}
    if slot:
        extra_context = get_placeholder_conf("extra_context", slot, template, {})
    for key, value in extra_context.items():
        if not key in context:
            context[key] = value

    content = []

    # Prepend frontedit toolbar output if applicable
    edit = False
    toolbar = getattr(request, "toolbar", None)

    if getattr(toolbar, "edit_mode", False) and (not page or page.has_change_permission(request)):
        edit = True
    if edit:
        from cms.middleware.toolbar import toolbar_plugin_processor

        processors = (toolbar_plugin_processor,)
    else:
        processors = None

    content.extend(render_plugins(plugins, context, placeholder, processors))
    toolbar_content = ""
    draggable_content = ""
    if edit:
        toolbar_content = mark_safe(render_placeholder_toolbar(placeholder, context, "", name_fallback=name_fallback))
        draggable_content = mark_safe(render_dragables(plugins, slot, request))
    content = mark_safe("".join(content))

    result = render_to_string(
        "cms/toolbar/placeholder.html",
        {"plugins": content, "bar": toolbar_content, "draggables": draggable_content, "edit": edit},
    )
    context.pop()
    return result