def doc_page_cache()

in kitsune/wiki/views.py [0:0]


def doc_page_cache(view):
    """Decorator that caches the document page HTML."""

    @wraps(view)
    def _doc_page_cache_view(request, document_slug, *args, **kwargs):
        # We skip caching for authed users or if redirect=no
        # is in the query string or if we show the aaq widget
        if (
            request.user.is_authenticated
            or request.GET.get("redirect") == "no"
            or request.session.get("product_slug")
            or settings.DEV
        ):
            return view(request, document_slug, *args, **kwargs)

        cache_key = doc_html_cache_key(locale=request.LANGUAGE_CODE, slug=document_slug)

        html, headers = cache.get(cache_key, (None, None))
        if html is not None:
            res = HttpResponse(html)
            for key, val in list(headers.items()):
                res[key] = val
            return res

        response = view(request, document_slug, *args, **kwargs)

        # We only cache if the response returns HTTP 200 and depends
        # only on the URL, not anything in the request headers.
        if (response.status_code == 200) and ("vary" not in response):
            cache.set(cache_key, (response.content, dict(list(response.headers.items()))))

        return response

    return _doc_page_cache_view