现在我正在使用这个应用程序进行权限检查:django-rules
然而,它已经一年多没有更新了,并且没有用于“新”(自 django 1.3 以来)基于类的视图的装饰器。我希望能够像这样在 urls.py 中使用:
url(r'^casos/(?P<pk>\d+)/editar/$', rules_permission_required('lawsuits.logical_check', raise_exception=True)(CaseUpdateView.as_view()), name='case_edit'),
我不知道如何从装饰器的基于类的视图中获取对象。你们有什么想法吗?这是我到目前为止所拥有的:
from django.utils.decorators import available_attrs
def rules_permission_required(perm, queryset=None, login_url=None, raise_exception=False):
def wrapper(view_func):
@wraps(view_func, assigned=available_attrs(view_func))
def inner(request, *args, **kwargs):
#view_func is the class based view -> <function MyEditView at 0x94e54c4>
print view_func.get_object() # doesnt work
print view_func(request, *args, **kwargs).get_object() # doesnt work either
#any ideas?
if not request.user.has_perm(perm, obj=obj):
return redirect_to_login(request, login_url, raise_exception)
return view_func(request, *args, **kwargs)
return inner
return wrapper
提前谢谢了!