Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added django.utils.decorators, from Hugo's #580 patch. Refs #580.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@807 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 71564b4349422d70af41cd3ea89d4667c9bf5086 1 parent a933432
Adrian Holovaty adrianholovaty authored
Showing with 22 additions and 0 deletions.
  1. +22 −0 django/utils/decorators.py
22 django/utils/decorators.py
View
@@ -0,0 +1,22 @@
+"Functions that help with dynamically creating decorators for views."
+
+def decorator_from_middleware(middleware_class):
+ """
+ Given a middleware class (not an instance), returns a view decorator. This
+ lets you use middleware functionality on a per-view basis.
+ """
+ def _decorator_from_middleware(view_func, *args, **kwargs):
+ middleware = middleware_class(*args, **kwargs)
+ def _wrapped_view(request, *args, **kwargs):
+ if hasattr(middleware, 'process_request'):
+ result = middleware.process_request(request)
+ if result is not None:
+ return result
+ response = view_func(request, *args, **kwargs)
+ if hasattr(middleware, 'process_response'):
+ result = middleware.process_response(request, response)
+ if result is not None:
+ return result
+ return response
+ return _wrapped_view
+ return _decorator_from_middleware
Please sign in to comment.
Something went wrong with that request. Please try again.