缓存非视图返回

Oli*_*Oli 5 python django decorator django-cache

我对视图有十几个权限查询,确保用户具有在系统上执行某些操作的正确权限(即确保他们在正确的组中,如果他们可以编辑他们的个人资料,如果他们是组管理员,等等).

支票可能如下所示:

from django.contrib.auth.decorators import user_passes_test

test_canvote = lambda u: u.has_perm('polls.can_vote')

@user_passes_test(test_canvote)
def my_view(request):
    # ...
Run Code Online (Sandbox Code Playgroud)

这实际上是来自Django教程的代码(我的有点丑陋).有时检查是数据库密集型的,会触发多个查询.由于许多用户访问权限检查页面,事情很快就会变得很慢.

我的问题是,我可以(在你的帮助下)为user_passes_test装饰器构建一个包装器(或替换件),该装饰器在缓存中搜索一个键'TESTCACHE' + user.pk + 'testname',如果它不存在,则执行测试并保存其结果.

我之前从未写过装饰器,但我想它看起来几乎与那个装饰器相同user_passes_test,只是将测试作为字符串传递:

@cached_user_passes_test('test_canvote')
def my_view(request):
   # ...
Run Code Online (Sandbox Code Playgroud)

和以前一样,让我知道我是否疯了,或者Django是否已经为我做过这样的事情(所以我在其他地方遇到了问题).

Edit: The standard decorators can be found here: http://code.djangoproject.com/browser/django/trunk/django/contrib/auth/decorators.py

I think it might be easier replacing user_passes_test than wrapping it so here's the starting point. Of course, if you feel I'm incorrect in that statement, let me know:

try:
    from functools import update_wrapper, wraps
except ImportError:
    from django.utils.functional import update_wrapper, wraps  # Python 2.3, 2.4 fallback.

from django.contrib.auth import REDIRECT_FIELD_NAME
from django.http import HttpResponseRedirect
from django.utils.http import urlquote
from django.utils.decorators import auto_adapt_to_methods

def user_passes_test(test_func, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME):
    """
    Decorator for views that checks that the user passes the given test,
    redirecting to the log-in page if necessary. The test should be a callable
    that takes the user object and returns True if the user passes.
    """
    if not login_url:
        from django.conf import settings
        login_url = settings.LOGIN_URL

    def decorator(view_func):
        def _wrapped_view(request, *args, **kwargs):
            if test_func(request.user):
                return view_func(request, *args, **kwargs)
            path = urlquote(request.get_full_path())
            tup = login_url, redirect_field_name, path
            return HttpResponseRedirect('%s?%s=%s' % tup)
        return wraps(view_func)(_wrapped_view)
    return auto_adapt_to_methods(decorator)
Run Code Online (Sandbox Code Playgroud)

ara*_*818 1

您可能需要序列化该函数(当我使用它作为缓存的密钥时我没有这样做),但类似这样的事情应该可以工作:

from django.core.cache import cache

def cached_user_passes_test(test_func, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME):
    if not login_url:
        from django.conf import settings
        login_url = settings.LOGIN_URL

    def decorator(view_func):
        def _wrapped_view(request, *args, **kwargs):
            key = str(test_func) + str(request.user)
            cached_test_result = cache.get(key)
            if cached_test_result != None:
                test_result = cached_test_result
            else:
                test_result = test_func(request.user)
                cache.set(key, test_result, 60)       

            if test_result:
                return view_func(request, *args, **kwargs)
            path = urlquote(request.get_full_path())
            tup = login_url, redirect_field_name, path
            return HttpResponseRedirect('%s?%s=%s' % tup)
        return wraps(view_func)(_wrapped_view)
    return auto_adapt_to_methods(decorator)
Run Code Online (Sandbox Code Playgroud)