我正在使用Django的user_passes_test装饰器来检查用户权限。
@user_passes_test(lambda u: has_add_permission(u, "project"))
def create_project(request):
......
我正在调用一个名为has_add_permission的回调函数,它接受两个参数——User和String。我想同时传递请求对象,这是否可能?此外,有人能告诉我如何直接在装饰器内部访问User对象吗?
我正在使用Django的user_passes_test装饰器来检查用户权限。
@user_passes_test(lambda u: has_add_permission(u, "project"))
def create_project(request):
......
我正在调用一个名为has_add_permission的回调函数,它接受两个参数——User和String。我想同时传递请求对象,这是否可能?此外,有人能告诉我如何直接在装饰器内部访问User对象吗?
不能将请求传递给user_passes_test
。要了解其原理和工作方式,请前往源代码:
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.
"""
def decorator(view_func):
@wraps(view_func, assigned=available_attrs(view_func))
def _wrapped_view(request, *args, **kwargs):
if test_func(request.user):
return view_func(request, *args, **kwargs)
path = request.build_absolute_uri()
# If the login url is the same scheme and net location then just
# use the path as the "next" url.
login_scheme, login_netloc = urlparse.urlparse(login_url or
settings.LOGIN_URL)[:2]
current_scheme, current_netloc = urlparse.urlparse(path)[:2]
if ((not login_scheme or login_scheme == current_scheme) and
(not login_netloc or login_netloc == current_netloc)):
path = request.get_full_path()
from django.contrib.auth.views import redirect_to_login
return redirect_to_login(path, login_url, redirect_field_name)
return _wrapped_view
return decorator
以下是user_passes_test
装饰器的代码。可以看到,装饰器接收一个测试函数(在你的情况下为lambda u: has_add_permission(u, "project")
)并将其作为参数传递给被装饰的函数。该测试函数只接收一个参数,即request.user
。当然,你也可以编写自己的装饰器(甚至直接复制此代码并进行修改),以便同时传递request
本身,但默认的user_passes_test
实现不支持这样做。
请注意,Django 1.9引入了UserPassesTestMixin
,它使用test_func
方法作为测试函数。这意味着请求在self.request
中可用。因此,您可以像这样做:
class MyView(UserPassesTestMixin, View):
def test_func(self):
return has_add_permission(self.request.user, self.request)
然而,这仅适用于基于类的视图。
user_passes_test
的编辑器更改为在 request
而不是 request.user
上运行的装饰函数并不过于困难。我在这篇博客文章中有一个简短的版本,介绍了一个视图装饰器装饰器,但为了记录,这是我的完整编辑代码:def request_passes_test(test_func, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME):
"""
Decorator for views that checks that the request passes the given test,
redirecting to the log-in page if necessary. The test should be a callable
that takes the request object and returns True if the request passes.
"""
def decorator(view_func):
@wraps(view_func, assigned=available_attrs(view_func))
def _wrapped_view(request, *args, **kwargs):
if test_func(request):
return view_func(request, *args, **kwargs)
path = request.build_absolute_uri()
# urlparse chokes on lazy objects in Python 3, force to str
resolved_login_url = force_str(
resolve_url(login_url or settings.LOGIN_URL))
# If the login url is the same scheme and net location then just
# use the path as the "next" url.
login_scheme, login_netloc = urlparse(resolved_login_url)[:2]
current_scheme, current_netloc = urlparse(path)[:2]
if ((not login_scheme or login_scheme == current_scheme) and
(not login_netloc or login_netloc == current_netloc)):
path = request.get_full_path()
from django.contrib.auth.views import redirect_to_login
return redirect_to_login(
path, resolved_login_url, redirect_field_name)
return _wrapped_view
return decorator
if test_func(request.user):
改成了 if test_func(request):
。