def decorate_method(self, fn, *args, **kwargs):
"""Decorate a view method.
This is pretty simple - just attach a Gate as a `_gate` property
to the function and then wrap it in the method dispatcher.
"""
@functools.wraps(fn)
def dispatcher(*args, **kwargs):
largs = list(args)
if isinstance(largs[0], BaseModelAdmin):
func = functools.partial(fn, largs.pop(0))
else:
func = fn
request = largs.pop(0)
return (self.gate.allow_or_deny(request) or
func(request, *largs, **kwargs))
dispatcher._gate = self.gate
return dispatcher
评论列表
文章目录