Created
August 21, 2012 18:21
-
-
Save alexisbellido/3418108 to your computer and use it in GitHub Desktop.
Django conditional cache for per-site cache based on user.is_authenticated()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
A decorator to bypass per-site cache if the user is authenticated. Based on django.views.decorators.cache.never_cache. | |
See: http://stackoverflow.com/questions/12060036/why-django-1-4-per-site-cache-does-not-work-correctly-with-cache-middleware-anon | |
""" | |
from django.utils.decorators import available_attrs | |
from django.utils.cache import add_never_cache_headers | |
from functools import wraps | |
def conditional_cache(view_func): | |
""" | |
Checks the user and if it's authenticated pass it through never_cache. | |
This version uses functools.wraps for the wrapper function. | |
""" | |
@wraps(view_func, assigned=available_attrs(view_func)) | |
def _wrapped_view_func(request, *args, **kwargs): | |
response = view_func(request, *args, **kwargs) | |
if request.user.is_authenticated(): | |
add_never_cache_headers(response) | |
return response | |
return _wrapped_view_func |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment