python - custom decorator for permission and login required -
i'm following "django unleashed book" , i've created custom decorator. struggle little understanding parts of code. lack of understanding may result lack of python knowledge. here code:
def require_authenticated_permission(permission): def decorator(cls): if (not isinstance(cls, type) or not issubclass(cls, view)): raise improperlyconfigured( "require_authenticated_permission" " must applied subclasses " "of view class.") check_auth = method_decorator(login_required) check_perm = method_decorator( permission_required( permission, raise_exception=true)) cls.dispatch = check_auth(check_perm(cls.dispatch)) return cls return decorator i have 1 problem understanding code. decorator takes gcbv, uses dispatch function , stores gcbv in "permission" object argument "require_authenticated_permission". takes arguments gcbv (e. g. detailview) , stores in "cls" object. since there 1 cls object , not *args, expect decorator work 1 argument such "class thisisaview(detailview)" works views "class thisisaview(getobjectmixin, detailview)" ... mean stores both arguments in cls object?
inside decorator, cls class thisisaview, not parent class detailview. therefore doesn't matter whether defined class as:
class thisisaview(detailview): ... or
class thisisaview(mymixin, detailview): ... you "the decorator stores gcbv in "permission" object argument "require_authenticated_permission". that's doesn't sound quite right. decorator taking gcbv's dispatch method, , wrapping permission_required decorator , login_required decorator. replaces cbv's dispatch method newly wrapped method.
Comments
Post a Comment