Example #1
0
def get_cache_key(request, page, lang, site_id, title):
    """
    Create the cache key for the current page and tag type
    """
    from cms.models import Page
    from cms.templatetags.cms_tags import _get_page_by_untyped_arg, _get_cache_key
    if not isinstance(page, Page):
        page = _get_page_by_untyped_arg(page, request, site_id)
    if not site_id:
        site_id = page.site_id
    if not title:
        return _get_cache_key('page_tags', page, '', site_id) + '_type:tags_list'
    else:
        return _get_cache_key('title_tags', page, lang, site_id) + '_type:tags_list'
Example #2
0
def get_cache_key(page, language):
    """
    Create the cache key for the current page and language
    """
    from cms.templatetags.cms_tags import _get_cache_key
    site_id = page.site_id
    return _get_cache_key('page_meta', page, language, site_id)
Example #3
0
def get_cache_key(page, language):
    """
    Create the cache key for the current page and language
    """
    from cms.templatetags.cms_tags import _get_cache_key
    site_id = page.site_id
    return _get_cache_key('page_meta', page, language, site_id)
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': ''}
Example #5
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': ''}
Example #6
0
def get_page(request, page_lookup, lang, site=None):
    site_id = cms_tags.get_site_id(site)
    
    if page_lookup:
        if isinstance(page_lookup, Page):
            return page_lookup
        else:
            cache_key = cms_tags._get_cache_key('tags_page_url', page_lookup, lang, site_id)+'_type:absolute_url'
            page = cache.get(cache_key)
            if not page:
                page = Page.objects.get(site=site_id, reverse_id=page_lookup)
                cache.set(cache_key, page, settings.CMS_CACHE_DURATIONS['content'])

            return page
    else:
        return None
Example #7
0
    def test_show_uncached_placeholder_tag_no_use_cache(self):
        """
        Tests that {% show_uncached_placeholder %} does not populate cache.
        """
        template = '{% load cms_tags %}<h1>{% show_uncached_placeholder "sub" test_page %}</h1>'

        base_key = _get_cache_key('_show_placeholder_for_page', self.test_page, "en",
                                  self.test_page.site_id)
        cache_key = _clean_key('%s_placeholder:%s' % (base_key, "sub"))

        cache_value_before = cache.get(cache_key)
        output = self.render(template, self.test_page, {'test_page': self.test_page})
        cache_value_after = cache.get(cache_key)

        self.assertEqual(output, '<h1>%s</h1>' % self.test_data['text_sub'])
        self.assertEqual(cache_value_before, cache_value_after)
        self.assertIsNone(cache_value_after)
Example #8
0
    def test_show_uncached_placeholder_tag_no_use_cache(self):
        """
        Tests that {% show_uncached_placeholder %} does not populate cache.
        """
        template = '{% load cms_tags %}<h1>{% show_uncached_placeholder "sub" test_page %}</h1>'

        base_key = _get_cache_key('_show_placeholder_for_page', self.test_page,
                                  "en", self.test_page.site_id)
        cache_key = _clean_key('%s_placeholder:%s' % (base_key, "sub"))

        cache_value_before = cache.get(cache_key)
        output = self.render(template, self.test_page,
                             {'test_page': self.test_page})
        cache_value_after = cache.get(cache_key)

        self.assertEqual(output, '<h1>%s</h1>' % self.test_data['text_sub'])
        self.assertEqual(cache_value_before, cache_value_after)
        self.assertIsNone(cache_value_after)
Example #9
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': ''}