Ejemplo n.º 1
0
def _show_placeholder_for_page(context,
                               placeholder_name,
                               page_lookup,
                               lang=None,
                               site=None,
                               cache_result=True):
    """
    Shows the content of a page with a placeholder name and given lookup
    arguments in the given language.
    This is useful if you want to have some more or less static content that is
    shared among many pages, such as a footer.

    See _get_page_by_untyped_arg() for detailed information on the allowed types
    and their interpretation for the page_lookup argument.
    """
    cache_result = False
    validate_placeholder_name(placeholder_name)

    request = context.get('request', False)
    site_id = get_site_id(site)

    if not request:
        return {'content': ''}
    if lang is None:
        lang = get_language_from_request(request)

    if cache_result:
        base_key = _get_cache_key('_show_placeholder_for_page', page_lookup,
                                  lang, site_id)
        cache_key = _clean_key('%s_placeholder:%s' %
                               (base_key, placeholder_name))
        cached_value = cache.get(cache_key)
        if isinstance(cached_value, dict):  # new style
            _restore_sekizai(context, cached_value['sekizai'])
            return {'content': mark_safe(cached_value['content'])}
        elif isinstance(cached_value, basestring):  # old style
            return {'content': mark_safe(cached_value)}

    page = _get_page_by_untyped_arg(page_lookup, request, site_id)
    if not page:
        return {'content': ''}
    try:
        placeholder = page.placeholders.get(slot=placeholder_name)
    except PlaceholderModel.DoesNotExist:
        if settings.DEBUG:
            raise
        return {'content': ''}
    watcher = Watcher(context)
    content = render_placeholder(placeholder, context, placeholder_name)
    changes = watcher.get_changes()
    if cache_result:
        cache.set(cache_key, {
            'content': content,
            'sekizai': changes
        },
                  get_cms_setting('CACHE_DURATIONS')['content'])

    if content:
        return {'content': mark_safe(content.strip())}
    return {'content': ''}
Ejemplo n.º 2
0
 def __init__(self, slotname, default_width=None, actions=PlaceholderNoAction, **kwargs):
     validate_placeholder_name(slotname)
     self.slotname = slotname
     self.default_width = default_width
     self.actions = actions()
     kwargs.update({'null':True}) # always allow Null
     super(PlaceholderField, self).__init__(Placeholder, **kwargs)
Ejemplo n.º 3
0
    def render_tag(self, context, name, extra_bits, nodelist=None):
        validate_placeholder_name(name)
        width = None
        inherit = False
        for bit in extra_bits:
            if bit == 'inherit':
                inherit = True
            elif bit.isdigit():
                width = int(bit)
                import warnings

                warnings.warn(
                    "The width parameter for the placeholder tag is deprecated.",
                    DeprecationWarning)
        if not 'request' in context:
            return ''
        request = context['request']
        if width:
            context.update({'width': width})

        page = request.current_page
        if not page or page == 'dummy':
            if nodelist:
                return nodelist.render(context)

            return ''

        content = get_placeholder_content(context, request, page, name,
                                          inherit)
        if not content and nodelist:
            return nodelist.render(context)
        return content
Ejemplo n.º 4
0
 def render_tag(self, context, name, extra_bits, nodelist=None):
     validate_placeholder_name(name)
     inherit = False
     for bit in extra_bits:
         if bit == 'inherit':
             inherit = True
     if not 'request' in context:
         return ''
     request = context['request']
     page = request.current_page
     if not page or page == 'dummy':
         if nodelist:
             return nodelist.render(context)
         return ''
     content = ''
     try:
         content = get_placeholder_content(context, request, page, name, inherit, nodelist)
     except PlaceholderNotFound:
         if nodelist:
             return nodelist.render(context)
     if not content:
         if nodelist:
             return nodelist.render(context)
         return ''
     return content
Ejemplo n.º 5
0
 def __init__(self, slotname, default_width=None, actions=PlaceholderNoAction, **kwargs):
     validate_placeholder_name(slotname)
     self.slotname = slotname
     self.default_width = default_width
     self.actions = actions()
     kwargs.update({'null': True})  # always allow Null
     super(PlaceholderField, self).__init__(Placeholder, **kwargs)
Ejemplo n.º 6
0
 def render_tag(self, context, name, extra_bits, nodelist=None):
     validate_placeholder_name(name)
     inherit = False
     for bit in extra_bits:
         if bit == 'inherit':
             inherit = True
     if not 'request' in context:
         return ''
     request = context['request']
     page = request.current_page
     if not page or page == 'dummy':
         if nodelist:
             return nodelist.render(context)
         return ''
     content = ''
     try:
         content = get_placeholder_content(context, request, page, name,
                                           inherit, nodelist)
     except PlaceholderNotFound:
         if nodelist:
             return nodelist.render(context)
     if not content:
         if nodelist:
             return nodelist.render(context)
         return ''
     return content
Ejemplo n.º 7
0
    def render_tag(self, context, name, extra_bits, nodelist=None):
        request = context.get('request')

        if not request:
            return ''

        validate_placeholder_name(name)

        toolbar = get_toolbar_from_request(request)
        renderer = toolbar.get_content_renderer()
        inherit = 'inherit' in extra_bits

        try:
            content = renderer.render_page_placeholder(
                slot=name,
                context=context,
                inherit=inherit,
                nodelist=nodelist,
            )
        except PlaceholderNotFound:
            content = ''

        if not content and nodelist:
            return nodelist.render(context)
        return content
Ejemplo n.º 8
0
 def _get_placeholder_slot(self, model_instance):
     if callable(self.slotname):
         slotname = self.slotname(model_instance)
         validate_placeholder_name(slotname)
     else:
         slotname = self.slotname
     return slotname
Ejemplo n.º 9
0
 def _get_placeholder_slot(self, model_instance):
     if callable(self.slotname):
         slotname = self.slotname(model_instance)
         validate_placeholder_name(slotname)
     else:
         slotname = self.slotname
     return slotname
Ejemplo n.º 10
0
    def render_tag(self, context, name, extra_bits, nodelist=None):
        validate_placeholder_name(name)
        width = None
        inherit = False
        for bit in extra_bits:
            if bit == 'inherit':
                inherit = True
            elif bit.isdigit():
                width = int(bit)
                import warnings

                warnings.warn(
                    "The width parameter for the placeholder tag is deprecated.",
                    DeprecationWarning
                )
        if not 'request' in context:
            return ''
        request = context['request']
        if width:
            context.update({'width': width})

        page = request.current_page
        if not page or page == 'dummy':
            if nodelist:
                return nodelist.render(context)

            return ''

        content = get_placeholder_content(context, request, page, name, inherit)
        if not content and nodelist:
            return nodelist.render(context)
        return content
    def render_tag(self, context, static_code, extra_bits, nodelist=None):
        request = context.get('request')

        if not static_code or not request:
            # an empty string was passed in or the variable is not available in the context
            if nodelist:
                return nodelist.render(context)
            return ''

        validate_placeholder_name(static_code)

        toolbar = get_toolbar_from_request(request)
        renderer = toolbar.get_content_renderer()
        alias = self._get_alias(request, static_code, extra_bits)

        if not alias:
            return ''

        # Get draft contents in edit or preview mode?
        get_draft_content = False
        if toolbar.edit_mode_active or toolbar.preview_mode_active:
            get_draft_content = True

        language = get_language_from_request(request)
        placeholder = alias.get_placeholder(language=language, show_draft_content=get_draft_content)

        if placeholder:
            content = renderer.render_placeholder(
                placeholder=placeholder,
                context=context,
                nodelist=nodelist,
            )
            return content
        return ''
Ejemplo n.º 12
0
def _show_placeholder_by_id(context, placeholder_name, reverse_id,
                            lang=None, site=None, use_cache=True):
    validate_placeholder_name(placeholder_name)

    content_renderer = context.get('cms_content_renderer')

    site_id = get_site_id(site)

    if not content_renderer:
        return ''

    page = _get_page_by_untyped_arg(reverse_id, content_renderer.request, site_id)

    if not page:
        return ''

    try:
        placeholder = page.placeholders.get(slot=placeholder_name)
    except PlaceholderModel.DoesNotExist:
        if settings.DEBUG:
            raise
        return ''

    content_renderer = context['cms_content_renderer']
    content = content_renderer.render_placeholder(
        placeholder=placeholder,
        context=context,
        language=lang,
        page=page,
        editable=False,
        use_cache=use_cache,
    )
    return content
Ejemplo n.º 13
0
def _show_placeholder_for_page(context,
                               placeholder_name,
                               page_lookup,
                               lang=None,
                               site=None,
                               cache_result=True):
    """
    Shows the content of a page with a placeholder name and given lookup
    arguments in the given language.
    This is useful if you want to have some more or less static content that is
    shared among many pages, such as a footer.

    See _get_page_by_untyped_arg() for detailed information on the allowed types
    and their interpretation for the page_lookup argument.
    """
    validate_placeholder_name(placeholder_name)

    request = context.get('request', False)
    site_id = get_site_id(site)

    if not request:
        return {'content': ''}
    if lang is None:
        lang = get_language_from_request(request)

    content = None

    if cache_result:
        base_key = _get_cache_key('_show_placeholder_for_page', page_lookup,
                                  lang, site_id)
        cache_key = _clean_key('%s_placeholder:%s' %
                               (base_key, placeholder_name))
        content = cache.get(cache_key)

    if not content:
        page = _get_page_by_untyped_arg(page_lookup, request, site_id)
        if not page:
            return {'content': ''}
        try:
            placeholder = page.placeholders.get(slot=placeholder_name)
        except PlaceholderModel.DoesNotExist:
            if settings.DEBUG:
                raise
            return {'content': ''}
        baseqs = get_cmsplugin_queryset(request)
        plugins = baseqs.filter(
            placeholder=placeholder,
            language=lang,
            placeholder__slot__iexact=placeholder_name,
            parent__isnull=True).order_by('position').select_related()
        c = render_plugins(plugins, context, placeholder)
        content = "".join(c)

    if cache_result:
        cache.set(cache_key, content, settings.CMS_CACHE_DURATIONS['content'])

    if content:
        return {'content': mark_safe(content)}
    return {'content': ''}
Ejemplo n.º 14
0
 def __init__(self, slotname, default_width=None, actions=PlaceholderNoAction, **kwargs):
     validate_placeholder_name(slotname)
     if kwargs.get('related_name', None) == '+':
         raise ValueError("PlaceholderField does not support disabling of related names via '+'.")
     self.slotname = slotname
     self.default_width = default_width
     self.actions = actions()
     kwargs.update({'null': True})  # always allow Null
     super(PlaceholderField, self).__init__(Placeholder, **kwargs)
Ejemplo n.º 15
0
    def _get_placeholder_slot(self, model_instance):
        from cms.utils.placeholder import validate_placeholder_name

        if callable(self.slotname):
            slotname = self.slotname(model_instance)
            validate_placeholder_name(slotname)
        else:
            slotname = self.slotname
        return slotname
Ejemplo n.º 16
0
    def _get_placeholder_slot(self, model_instance):
        from cms.utils.placeholder import validate_placeholder_name

        if callable(self.slotname):
            slotname = self.slotname(model_instance)
            validate_placeholder_name(slotname)
        else:
            slotname = self.slotname
        return slotname
Ejemplo n.º 17
0
def _show_placeholder_attr_for_page(context,
                                    placeholder_name,
                                    plugin_class_name,
                                    plugin_attr,
                                    page_lookup,
                                    lang=None,
                                    site=None,
                                    cache_result=True):

    validate_placeholder_name(placeholder_name)

    request = context.get('request', False)
    site_id = get_site_id(site)

    if not request:
        return {'content': ''}
    if lang is None:
        lang = get_language_from_request(request)

    if cache_result:
        base_key = _get_cache_key('_show_placeholder_for_page', page_lookup,
                                  lang, site_id)

        cache_key = _clean_key('%s_placeholder:%s' %
                               (base_key, placeholder_name)
                               ) + plugin_class_name + '.' + plugin_attr

        cached_value = cache.get(cache_key)
        if isinstance(cached_value, dict):  # new style
            _restore_sekizai(context, cached_value['sekizai'])
            return {'content': mark_safe(cached_value['content'])}
        elif isinstance(cached_value, string_types):  # old style
            return {'content': mark_safe(cached_value)}

    page = _get_page_by_untyped_arg(page_lookup, request, site_id)
    if not page:
        return {'content': ''}
    watcher = Watcher(context)

    placeholder = _get_placeholder(page, page, context, placeholder_name)
    content = get_placholder_attr(placeholder, placeholder_name,
                                  plugin_class_name, plugin_attr)

    changes = watcher.get_changes()

    if cache_result:
        cache.set(cache_key, {
            'content': content,
            'sekizai': changes
        },
                  get_cms_setting('CACHE_DURATIONS')['content'])

    if content:
        return {'content': mark_safe(content)}

    return {'content': ''}
def _show_placeholder_attr_for_page(
        context, placeholder_name,
        plugin_class_name, plugin_attr,
        page_lookup, lang=None,
        site=None, cache_result=True):

    validate_placeholder_name(placeholder_name)

    request = context.get('request', False)
    site_id = get_site_id(site)

    if not request:
        return {'content': ''}
    if lang is None:
        lang = get_language_from_request(request)

    if cache_result:
        base_key = _get_cache_key(
            '_show_placeholder_for_page', page_lookup, lang, site_id)

        cache_key = _clean_key(
            '%s_placeholder:%s' % (
                base_key, placeholder_name
            )) + plugin_class_name + '.' + plugin_attr

        cached_value = cache.get(cache_key)
        if isinstance(cached_value, dict):  # new style
            _restore_sekizai(context, cached_value['sekizai'])
            return {'content': mark_safe(cached_value['content'])}
        elif isinstance(cached_value, string_types):  # old style
            return {'content': mark_safe(cached_value)}

    page = _get_page_by_untyped_arg(page_lookup, request, site_id)
    if not page:
        return {'content': ''}
    watcher = Watcher(context)

    placeholder = _get_placeholder(page, page, context, placeholder_name)
    content = get_placholder_attr(
        placeholder, placeholder_name, plugin_class_name, plugin_attr)

    changes = watcher.get_changes()

    if cache_result:
        cache.set(
            cache_key,
            {
                'content': content,
                'sekizai': changes
            }, get_cms_setting('CACHE_DURATIONS')['content'])

    if content:
        return {'content': mark_safe(content)}

    return {'content': ''}
Ejemplo n.º 19
0
def _show_placeholder_for_page(context, placeholder_name, page_lookup, lang=None,
        site=None, cache_result=True):
    """
    Shows the content of a page with a placeholder name and given lookup
    arguments in the given language.
    This is useful if you want to have some more or less static content that is
    shared among many pages, such as a footer.

    See _get_page_by_untyped_arg() for detailed information on the allowed types
    and their interpretation for the page_lookup argument.
    """
    validate_placeholder_name(placeholder_name)

    request = context.get('request', False)
    site_id = get_site_id(site)

    if not request:
        return {'content': ''}
    if lang is None:
        lang = get_language_from_request(request)

    content = None

    if cache_result:
        base_key = _get_cache_key('_show_placeholder_for_page', page_lookup, lang, site_id)
        cache_key = _clean_key('%s_placeholder:%s' % (base_key, placeholder_name))
        content = cache.get(cache_key)

    if not content:
        page = _get_page_by_untyped_arg(page_lookup, request, site_id)
        if not page:
            return {'content': ''}
        try:
            placeholder = page.placeholders.get(slot=placeholder_name)
        except PlaceholderModel.DoesNotExist:
            if settings.DEBUG:
                raise
            return {'content': ''}
        baseqs = get_cmsplugin_queryset(request)
        plugins = baseqs.filter(
            placeholder=placeholder,
            language=lang,
            placeholder__slot__iexact=placeholder_name,
            parent__isnull=True
        ).order_by('position').select_related()
        c = render_plugins(plugins, context, placeholder)
        content = "".join(c)

    if cache_result:
        cache.set(cache_key, content, settings.CMS_CACHE_DURATIONS['content'])

    if content:
        return {'content': mark_safe(content)}
    return {'content': ''}
Ejemplo n.º 20
0
def get_placeholders(template):
    compiled_template = get_template(template)
    placeholders = _scan_placeholders(compiled_template.nodelist)
    clean_placeholders = []
    for placeholder in placeholders:
        if placeholder in clean_placeholders:
            warnings.warn("Duplicate placeholder found: `%s`" % placeholder, DuplicatePlaceholderWarning)
        else:
            validate_placeholder_name(placeholder)
            clean_placeholders.append(placeholder)
    return clean_placeholders
Ejemplo n.º 21
0
def get_placeholders(template):
    compiled_template = get_template(template)
    placeholders = _scan_placeholders(compiled_template.nodelist)
    clean_placeholders = []
    for placeholder in placeholders:
        if placeholder in clean_placeholders:
            warnings.warn("Duplicate placeholder found: `%s`" % placeholder, DuplicatePlaceholderWarning)
        else:
            validate_placeholder_name(placeholder)
            clean_placeholders.append(placeholder)
    return clean_placeholders
Ejemplo n.º 22
0
def _show_content_for_page(context, placeholder_name, page_lookup, lang=None,
                               site=None, cache_result=True, content_max_length=None):
    """
    Shows the content of a page as content of placeholder with name 'content' and given lookup
    arguments in the given language.
    This is useful if you want to have some more or less static content that is
    shared among many pages, such as a footer.

    See _get_page_by_untyped_arg() for detailed information on the allowed types
    and their interpretation for the page_lookup argument.
    """
    validate_placeholder_name(placeholder_name)

    request = context.get('request', False)
    site_id = get_site_id(site)

    if not request:
        return {'content': ''}
    if lang is None:
        lang = get_language_from_request(request)

    if cache_result:
        base_key = _get_cache_key('_show_content_for_page', page_lookup, lang, site_id)
        cache_key = _clean_key('%s_placeholder:%s' % (base_key, placeholder_name))
        cached_value = cache.get(cache_key)
        if isinstance(cached_value, dict): # new style
            _restore_sekizai(context, cached_value['sekizai'])
            return {'content': mark_safe(cached_value['content'])}
        elif isinstance(cached_value, basestring): # old style
            return {'content': mark_safe(cached_value)}

    page = _get_page_by_untyped_arg(page_lookup, request, site_id)
    if not page:
        return {'content': ''}
    try:
        placeholder = page.placeholders.get(slot=placeholder_name)
    except PlaceholderModel.DoesNotExist:
        if settings.DEBUG:
            raise
        return {'content': ''}
    watcher = Watcher(context)
    content = render_placeholder(placeholder, context, placeholder_name)
     
    if content_max_length:
        content = remove_tags(content)[:content_max_length]
     
    changes = watcher.get_changes()
    if cache_result:
        cache.set(cache_key, {'content': content, 'sekizai': changes}, get_cms_setting('CACHE_DURATIONS')['content'])

    if content:
        return {'content': mark_safe(content)}
    return {'content': ''}
Ejemplo n.º 23
0
 def __init__(self, slotname, default_width=None, actions=PlaceholderNoAction, **kwargs):
     if kwargs.get('related_name', None) == '+':
         raise ValueError("PlaceholderField does not support disabling of related names via '+'.")
     if not callable(slotname):
         validate_placeholder_name(slotname)
     self.slotname = slotname
     self.default_width = default_width
     self.actions = actions()
     if 'to' in kwargs:
         del(kwargs['to'])
     kwargs.update({'null': True})  # always allow Null
     kwargs.update({'editable': False}) # never allow edits in admin
     super(PlaceholderField, self).__init__('cms.Placeholder', **kwargs)
Ejemplo n.º 24
0
def get_placeholders(template):
    compiled_template = get_template(template)
    placeholders = _scan_placeholders(compiled_template.nodelist)
    clean_placeholders = []
    for placeholder in placeholders:
        if placeholder in clean_placeholders:
            warnings.warn("Duplicate {{% placeholder \"{0}\" %}} "
                          "in template {1}."
                          .format(placeholder, template, placeholder),
                          DuplicatePlaceholderWarning)
        else:
            validate_placeholder_name(placeholder)
            clean_placeholders.append(placeholder)
    return clean_placeholders
Ejemplo n.º 25
0
def get_placeholders(template):
    compiled_template = get_template(template)
    placeholders = _scan_placeholders(compiled_template.nodelist)
    clean_placeholders = []
    for placeholder in placeholders:
        if placeholder in clean_placeholders:
            warnings.warn(
                "Duplicate {{% placeholder \"{0}\" %}} "
                "in template {1}.".format(placeholder, template, placeholder),
                DuplicatePlaceholderWarning)
        else:
            validate_placeholder_name(placeholder)
            clean_placeholders.append(placeholder)
    return clean_placeholders
Ejemplo n.º 26
0
 def __init__(self, slotname, default_width=None, actions=PlaceholderNoAction, **kwargs):
     if kwargs.get('related_name', None) == '+':
         raise ValueError("PlaceholderField does not support disabling of related names via '+'.")
     if not callable(slotname):
         validate_placeholder_name(slotname)
     self.slotname = slotname
     self.default_width = default_width
     self.actions = actions()
     kwargs.update({'null': True})  # always allow Null
     kwargs.update({'editable': False}) # never allow edits in admin
     # We hard-code the `to` argument for ForeignKey.__init__
     # since a PlaceholderField can only be a ForeignKey to a Placeholder
     kwargs['to'] = 'cms.Placeholder'
     super(PlaceholderField, self).__init__(**kwargs)
Ejemplo n.º 27
0
    def render_tag(self,
                   context,
                   name,
                   page_lookup,
                   extra_bits,
                   nodelist=None):
        validate_placeholder_name(name)

        request = context.get("request")

        if request:
            page = _get_page_by_untyped_arg(page_lookup, request,
                                            get_site_id(None))

            toolbar = get_toolbar_from_request(request)
            renderer = toolbar.get_content_renderer()

            inherit = "inherit" in extra_bits

            # A placeholder is only editable on its own page
            editable = page == request.current_page

            try:
                content = renderer.render_page_placeholder(
                    slot=name,
                    context=context,
                    inherit=inherit,
                    page=page,
                    nodelist=nodelist,
                    editable=editable,
                )
            except PlaceholderNotFound:
                content = ""
        else:
            content = ""

        if not content and nodelist:
            content = nodelist.render(context)

        if "as" in extra_bits:
            try:
                varname = extra_bits[extra_bits.index("as") + 1]
            except IndexError:
                raise template.TemplateSyntaxError(
                    'the "as" word should be followed by the variable name')
            context[varname] = content
            return ""

        return content
Ejemplo n.º 28
0
def _show_placeholder_for_page(context, placeholder_name, page_lookup, lang=None,
                               site=None, cache_result=True):
    """
    Shows the content of a page with a placeholder name and given lookup
    arguments in the given language.
    This is useful if you want to have some more or less static content that is
    shared among many pages, such as a footer.

    See _get_page_by_untyped_arg() for detailed information on the allowed types
    and their interpretation for the page_lookup argument.
    """
    validate_placeholder_name(placeholder_name)

    if DJANGO_1_7:
        request = context.get('request', False)
    else:
        request = context.request

    site_id = get_site_id(site)

    if not request:
        return {'content': ''}
    if lang is None:
        lang = get_language_from_request(request)

    if cache_result:
        cached_value = get_placeholder_page_cache(page_lookup, lang, site_id, placeholder_name)
        if cached_value:
            restore_sekizai_context(context, cached_value['sekizai'])
            return {'content': mark_safe(cached_value['content'])}
    page = _get_page_by_untyped_arg(page_lookup, request, site_id)
    if not page:
        return {'content': ''}
    try:
        placeholder = page.placeholders.get(slot=placeholder_name)
    except PlaceholderModel.DoesNotExist:
        if settings.DEBUG:
            raise
        return {'content': ''}
    watcher = Watcher(context)
    content = render_placeholder(placeholder, context, placeholder_name, use_cache=cache_result)
    changes = watcher.get_changes()
    if cache_result:
        set_placeholder_page_cache(page_lookup, lang, site_id, placeholder_name,
                                   {'content': content, 'sekizai': changes})

    if content:
        return {'content': mark_safe(content)}
    return {'content': ''}
Ejemplo n.º 29
0
def _show_placeholder_for_page(context, placeholder_name, page_lookup, lang=None,
                               site=None, cache_result=True):
    """
    Shows the content of a page with a placeholder name and given lookup
    arguments in the given language.
    This is useful if you want to have some more or less static content that is
    shared among many pages, such as a footer.

    See _get_page_by_untyped_arg() for detailed information on the allowed types
    and their interpretation for the page_lookup argument.
    """
    validate_placeholder_name(placeholder_name)

    request = context.get('request', False)

    site_id = get_site_id(site)

    if not request:
        return {'content': ''}
    if lang is None:
        lang = get_language_from_request(request)

    page = _get_page_by_untyped_arg(page_lookup, request, site_id)
    if not page:
        return {'content': ''}
    try:
        placeholder = page.placeholders.get(slot=placeholder_name)
    except PlaceholderModel.DoesNotExist:
        if settings.DEBUG:
            raise
        return {'content': ''}

    if cache_result:
        cached_value = get_placeholder_cache(placeholder, lang, site_id, request)
        if cached_value:
            restore_sekizai_context(context, cached_value['sekizai'])
            return {'content': mark_safe(cached_value['content'])}
    watcher = Watcher(context)
    content = render_placeholder(placeholder, context, placeholder_name, lang=lang,
                                 use_cache=cache_result)
    changes = watcher.get_changes()

    edit_mode = hasattr(request, 'toolbar') and getattr(request.toolbar, 'edit_mode', False)
    if not edit_mode and placeholder and placeholder.cache_placeholder and get_cms_setting('PLACEHOLDER_CACHE') and cache_result:  # noqa
        set_placeholder_cache(placeholder, lang, site_id, {'content': content, 'sekizai': changes}, request)
    if content:
        return {'content': mark_safe(content)}
    return {'content': ''}
Ejemplo n.º 30
0
    def render_tag(self, context, name, extra_bits, nodelist=None):
        validate_placeholder_name(name)

        content_renderer = context.get('cms_content_renderer')

        if not content_renderer:
            return ''

        inherit = 'inherit' in extra_bits
        content = content_renderer.render_page_placeholder(
            slot=name,
            context=context,
            inherit=inherit,
            nodelist=nodelist,
        )
        return content
Ejemplo n.º 31
0
    def render_tag(self, context, name, extra_bits, nodelist=None):
        validate_placeholder_name(name)

        content_renderer = context.get('cms_content_renderer')

        if not content_renderer:
            return ''

        inherit = 'inherit' in extra_bits
        content = content_renderer.render_page_placeholder(
            slot=name,
            context=context,
            inherit=inherit,
            nodelist=nodelist,
        )
        return content
Ejemplo n.º 32
0
def _show_placeholder_for_page(context, placeholder_name, page_lookup, lang=None, site=None, cache_result=True):
    """
    Shows the content of a page with a placeholder name and given lookup
    arguments in the given language.
    This is useful if you want to have some more or less static content that is
    shared among many pages, such as a footer.

    See _get_page_by_untyped_arg() for detailed information on the allowed types
    and their interpretation for the page_lookup argument.
    """
    validate_placeholder_name(placeholder_name)

    request = context.get("request", False)
    site_id = get_site_id(site)

    if not request:
        return {"content": ""}
    if lang is None:
        lang = get_language_from_request(request)

    content = None

    if cache_result:
        base_key = _get_cache_key("_show_placeholder_for_page", page_lookup, lang, site_id)
        cache_key = _clean_key("%s_placeholder:%s" % (base_key, placeholder_name))
        content = cache.get(cache_key)

    if not content:
        page = _get_page_by_untyped_arg(page_lookup, request, site_id)
        if not page:
            return {"content": ""}
        try:
            placeholder = page.placeholders.get(slot=placeholder_name)
        except PlaceholderModel.DoesNotExist:
            if settings.DEBUG:
                raise
            return {"content": ""}
        content = render_placeholder(placeholder, context, placeholder_name)
    if cache_result:
        cache.set(cache_key, content, get_cms_setting("CACHE_DURATIONS")["content"])

    if content:
        return {"content": mark_safe(content)}
    return {"content": ""}
Ejemplo n.º 33
0
 def __init__(self,
              slotname,
              default_width=None,
              actions=PlaceholderNoAction,
              **kwargs):
     if kwargs.get('related_name', None) == '+':
         raise ValueError(
             "PlaceholderField does not support disabling of related names via '+'."
         )
     if not callable(slotname):
         validate_placeholder_name(slotname)
     self.slotname = slotname
     self.default_width = default_width
     self.actions = actions()
     if 'to' in kwargs:
         del (kwargs['to'])
     kwargs.update({'null': True})  # always allow Null
     kwargs.update({'editable': False})  # never allow edits in admin
     super(PlaceholderField, self).__init__(Placeholder, **kwargs)
Ejemplo n.º 34
0
def _show_placeholder_by_id(context,
                            placeholder_name,
                            reverse_id,
                            lang=None,
                            site=None,
                            use_cache=True):
    validate_placeholder_name(placeholder_name)

    request = context['request']
    toolbar = get_toolbar_from_request(request)
    renderer = toolbar.get_content_renderer()

    if site:
        # Backwards compatibility.
        # Assume user passed in a pk directly.
        site_id = getattr(site, 'pk', site)
    else:
        site_id = renderer.current_site.pk

    page = _get_page_by_untyped_arg(reverse_id, request, site_id)

    if not page:
        return ''

    try:
        placeholder = page.placeholders.get(slot=placeholder_name)
    except PlaceholderModel.DoesNotExist:
        if settings.DEBUG:
            raise
        return ''
    else:
        # save a query. cache the page.
        placeholder.page = page

    content = renderer.render_placeholder(
        placeholder=placeholder,
        context=context,
        language=lang,
        page=page,
        editable=False,
        use_cache=use_cache,
    )
    return content
Ejemplo n.º 35
0
 def __init__(self,
              slotname,
              default_width=None,
              actions=PlaceholderNoAction,
              **kwargs):
     if kwargs.get('related_name', None) == '+':
         raise ValueError(
             "PlaceholderField does not support disabling of related names via '+'."
         )
     if not callable(slotname):
         validate_placeholder_name(slotname)
     self.slotname = slotname
     self.default_width = default_width
     self.actions = actions()
     kwargs.update({'null': True})  # always allow Null
     kwargs.update({'editable': False})  # never allow edits in admin
     # We hard-code the `to` argument for ForeignKey.__init__
     # since a PlaceholderField can only be a ForeignKey to a Placeholder
     kwargs['to'] = 'cms.Placeholder'
     super(PlaceholderField, self).__init__(**kwargs)
Ejemplo n.º 36
0
    def render_tag(self,
                   context,
                   name,
                   plugin_class_name,
                   plugin_attr,
                   extra_bits,
                   nodelist=None):
        validate_placeholder_name(name)
        width = None
        for bit in extra_bits:
            if bit == 'inherit':
                pass
            elif bit.isdigit():
                width = int(bit)
                import warnings

                warnings.warn(
                    "The width parameter for the placeholder " +
                    "tag is deprecated.", DeprecationWarning)
        if not 'request' in context:
            return ''
        request = context['request']
        if width:
            context.update({'width': width})

        page = request.current_page
        if not page or page == 'dummy':
            if nodelist:
                return nodelist.render(context)

            return ''

        placeholder = _get_placeholder(page, page, context, name)

        res = get_placholder_attr(placeholder, name, plugin_class_name,
                                  plugin_attr)

        return res
Ejemplo n.º 37
0
def get_placeholders(template):
    from ..templatetags.cms_articles import ArticlePlaceholder

    compiled_template = get_template(template)

    placeholders = []
    nodes = _scan_placeholders(_get_nodelist(compiled_template), ArticlePlaceholder)
    clean_placeholders = []

    for node in nodes:
        placeholder = node.get_declaration()
        slot = placeholder.slot

        if slot in clean_placeholders:
            warnings.warn("Duplicate {{% placeholder \"{0}\" %}} "
                          "in template {1}."
                          .format(slot, template, slot),
                          DuplicatePlaceholderWarning)
        else:
            validate_placeholder_name(slot)
            placeholders.append(placeholder)
            clean_placeholders.append(slot)
    return placeholders
    def render_tag(
            self, context,
            name, plugin_class_name, plugin_attr,
            extra_bits, nodelist=None):
        validate_placeholder_name(name)
        width = None
        for bit in extra_bits:
            if bit == 'inherit':
                pass
            elif bit.isdigit():
                width = int(bit)
                import warnings

                warnings.warn(
                    "The width parameter for the placeholder " +
                    "tag is deprecated.",
                    DeprecationWarning
                )
        if not 'request' in context:
            return ''
        request = context['request']
        if width:
            context.update({'width': width})

        page = request.current_page
        if not page or page == 'dummy':
            if nodelist:
                return nodelist.render(context)

            return ''

        placeholder = _get_placeholder(page, page, context, name)

        res = get_placholder_attr(
            placeholder, name, plugin_class_name, plugin_attr)

        return res
Ejemplo n.º 39
0
def get_placeholders(template):
    from ..templatetags.cms_articles import ArticlePlaceholder

    compiled_template = get_template(template)

    placeholders = []
    nodes = _scan_placeholders(_get_nodelist(compiled_template),
                               ArticlePlaceholder)
    clean_placeholders = []

    for node in nodes:
        placeholder = node.get_declaration()
        slot = placeholder.slot

        if slot in clean_placeholders:
            warnings.warn(
                "Duplicate {{% placeholder \"{0}\" %}} "
                "in template {1}.".format(slot, template, slot),
                DuplicatePlaceholderWarning)
        else:
            validate_placeholder_name(slot)
            placeholders.append(placeholder)
            clean_placeholders.append(slot)
    return placeholders
Ejemplo n.º 40
0
    def render_tag(self, context, name, extra_bits, nodelist=None):
        validate_placeholder_name(name)

        content_renderer = context.get('cms_content_renderer')
        # content_renderer == cms.plugin_rendering.ContentRenderer() instance

        if not content_renderer:
            log.debug("No content renderer from context -> render nothing")
            return ''

        edit_mode = content_renderer.user_is_on_edit_mode()
        if not edit_mode:
            log.debug("User is not on edit mode: catch all placeholder plugins.")
            el_pagination_renderer = ElPaginationContentRenderer(content_renderer.render_plugin)
            origin_render_plugin = content_renderer.render_plugin
            content_renderer.render_plugin = el_pagination_renderer

            # deactivate the placeholder cache, oterhwise the "all_plugins" list will be
            # empty on next request ;)
            content_renderer.placeholder_cache_is_enabled = lambda: False

        content = super(ElPaginationPlaceholder, self).render_tag(context, name, extra_bits, nodelist=nodelist)
        if edit_mode:
            log.debug("User is on edit mode: return origin content and empty 'all_plugins' list.")
            context["all_plugins"]=[]
            return content
        else:
            content_renderer.render_plugin = origin_render_plugin

        all_plugins = el_pagination_renderer.all_plugins
        all_plugins_count=len(all_plugins)
        context["all_plugins"] = all_plugins
        context["all_plugins_count"] = all_plugins_count

        log.debug("Add %i items to context['all_plugins']", all_plugins_count)
        return ""