私は、ユーザーがシステム上で何かを行うための適切な権限を持っていることを確認するために、ビューに対して12ほどの権限ルックアップを持っています(つまり、ユーザーがプロファイルを編集できる場合、グループ管理者である場合は、適切なグループに属していることを確認してください。等)。
チェックは次のようになります。
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):
# ...
これは実際にはDjangoチュートリアルのコードです(私のものは少し醜いです)。チェックはデータベースを非常に集中的に使用し、複数のクエリを実行する場合があります。多くのユーザーが権限がチェックされたページを押すと、物事はすぐに非常に遅くなる可能性があります。
'TESTCACHE' + user.pk + 'testname'
私の質問は、(あなたの助けを借りて)キャッシュでキーを検索し、キーが存在しない場合はテストを実行してその結果を保存するuser_passes_testデコレータのラッパー(または置換)を構築できますか?
私はこれまでデコレータを書いたことがありませんがuser_passes_test
、文字列としてテストに合格するだけで、デコレータとほぼ同じに見えると思います。
@cached_user_passes_test('test_canvote')
def my_view(request):
# ...
そしていつものように、私が怒っているかどうか、またはDjangoがすでにこれを行っているかどうかを知らせてください(他の場所で問題が発生しています)。
編集:標準のデコレータはここにあります:http://code.djangoproject.com/browser/django/trunk/django/contrib/auth/decorators.py
user_passes_test
包むより交換する方が簡単かもしれないと思うので、ここから始めます。もちろん、その声明で私が間違っていると感じた場合は、私に知らせてください。
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)