def get_value(self, context, *tag_args, **tag_kwargs):
        request = self.get_request(context)
        output = None

        # Process arguments
        (slot,) = tag_args
        template_name = tag_kwargs.get('template') or None
        cachable = is_true(tag_kwargs.get('cachable', not bool(template_name)))  # default: True unless there is a template.

        if template_name and cachable and not extract_literal(self.kwargs['template']):
            # If the template name originates from a variable, it can change any time.
            # See PagePlaceholderNode.render_tag() why this is not allowed.
            raise TemplateSyntaxError("{0} tag does not allow 'cachable' for variable template names!".format(self.tag_name))

        # Caching will not happen when rendering via a template,
        # because there is no way to tell whether that can be expired/invalidated.
        try_cache = appsettings.FLUENT_CONTENTS_CACHE_OUTPUT \
                and appsettings.FLUENT_CONTENTS_CACHE_PLACEHOLDER_OUTPUT \
                and cachable

        if isinstance(slot, SharedContent):
            # Allow passing a sharedcontent, just like 'render_placeholder' does.
            sharedcontent = slot

            # See if there is cached output, avoid fetching the Placeholder via sharedcontents.contents.
            if try_cache:
                cache_key = get_shared_content_cache_key(sharedcontent)
                output = cache.get(cache_key)
        else:
            site = Site.objects.get_current()
            if try_cache:
                # See if there is output cached, try to avoid fetching the SharedContent + Placeholder model.
                # Have to perform 2 cache calls for this, because the placeholder output key is based on object IDs
                cache_key_ptr = get_shared_content_cache_key_ptr(int(site.pk), slot, language_code=get_language())
                cache_key = cache.get(cache_key_ptr)
                if cache_key is not None:
                    output = cache.get(cache_key)

            if output is None:
                # Get the placeholder
                try:
                    sharedcontent = SharedContent.objects.parent_site(site).get(slug=slot)
                except SharedContent.DoesNotExist:
                    return "<!-- shared content '{0}' does not yet exist -->".format(slot)

                # Now that we've fetched the object, the object key be generated.
                # No real need to check for output again, render_placeholder() does that already.
                if try_cache and not cache_key:
                    cache.set(cache_key_ptr, get_shared_content_cache_key(sharedcontent))

        if output is None:
            # Have to fetch + render it.
            output = self.render_shared_content(request, sharedcontent, template_name, cachable=cachable)

        rendering.register_frontend_media(request, output.media)  # Need to track frontend media here, as the template tag can't return it.
        return output.html
    def get_cache_keys(self):
        # When the shared content is saved, make sure all rendering output PTRs are cleared.
        # The 'slug' could have changed. Whether the Placeholder output is cleared,
        # depends on whether those objects are altered too.
        if self.is_cross_site or self._was_cross_site:
            sites = list(Site.objects.all().values_list('pk', flat=True))
        else:
            sites = [self.parent_site_id]

        keys = []
        for site_id in sites:
            for language_code, _ in settings.LANGUAGES:
                keys.append(get_shared_content_cache_key_ptr(site_id, self._old_slug, language_code))
        return keys
Example #3
0
    def get_cache_keys(self):
        # When the shared content is saved, make sure all rendering output PTRs are cleared.
        # The 'slug' could have changed. Whether the Placeholder output is cleared,
        # depends on whether those objects are altered too.
        if self.is_cross_site or self._was_cross_site:
            sites = list(Site.objects.all().values_list('pk', flat=True))
        else:
            sites = [self.parent_site_id]

        keys = []
        for site_id in sites:
            for language_code, _ in settings.LANGUAGES:
                keys.append(get_shared_content_cache_key_ptr(site_id, self._old_slug, language_code))
        return keys