from django.utils.decorators import method_decorator
Convert the provided decorator to one that can be applied to a view
class (ie. automatically decorates dispatch)
# We're going to be applying a regular decorator to a method, so the first
# step is to convert it to a method decorator.
method_dec = method_decorator(orig_dec)
# This is the decorator we're actually going to return. Since we're
# returning a class decorator, it takes a single argument, the class
# that it's decorating. It therefore returns this as well.
# We're about to change what cls.dispatch refers to, so we need to
# keep a reference to the original dispatch around so that we can
# call it later.
orig_dispatch = cls.dispatch
def _dispatch(self, *args, **kwargs):
# Right - decorate the original dispatch method using our new,
# method-ised version of the decorator we were passed in to start
decorated = method_dec(orig_dispatch)
# Finally, we can call the decorated dispatch() method.
return decorated(self, *args, **kwargs)
# Replace the original dispatch with our new dispatch function. We
# kept a reference to the old dispatch method earlier, in a closure.
cls.dispatch = _dispatch
from foo.bar import view_decorator
from django.contrib.auth.decorators import login_required