Skip to content

Instantly share code, notes, and snippets.

@jacobian
Created September 15, 2011 16:42
Show Gist options
  • Save jacobian/1219750 to your computer and use it in GitHub Desktop.
Save jacobian/1219750 to your computer and use it in GitHub Desktop.
import inspect
REQUEST = object()
def super_dec(obj):
if inspect.isclass(obj):
_original_dispatch = obj.dispatch
def _wrapped_dispatch(self, request, *args, **kwargs):
print "calling wrapped CBV-style dispatch"
assert request is REQUEST
return _original_dispatch(self, request, *args, **kwargs)
obj.dispatch = _wrapped_dispatch
return obj
def _inner(*args, **kwargs):
if args[0] is REQUEST:
print "calling wrapped function view"
return obj(*args, **kwargs)
else:
print "calling wrapped method"
assert args[1] is REQUEST
return obj(*args, **kwargs)
return _inner
@super_dec
def function_view(request):
assert request is REQUEST
@super_dec
def function_view_nonstandard_names(req):
assert req is REQUEST
class ClassViewStyleOne(object):
@super_dec
def view_method(self, request):
assert request is REQUEST
@super_dec
def view_method_weird_names(s, req):
assert req is REQUEST
@super_dec
class ClassViewStyleTwo(object):
def dispatch(self, request):
assert request is REQUEST
function_view(REQUEST)
function_view_nonstandard_names(REQUEST)
ClassViewStyleOne().view_method(REQUEST)
ClassViewStyleTwo().dispatch(REQUEST)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment