Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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 authored October 08, 2005

Showing 1 changed file with 22 additions and 0 deletions. Show diff stats Hide diff stats

  1. 22  django/utils/decorators.py
22  django/utils/decorators.py
... ...
@@ -0,0 +1,22 @@
  1
+"Functions that help with dynamically creating decorators for views."
  2
+
  3
+def decorator_from_middleware(middleware_class):
  4
+    """
  5
+    Given a middleware class (not an instance), returns a view decorator. This
  6
+    lets you use middleware functionality on a per-view basis.
  7
+    """
  8
+    def _decorator_from_middleware(view_func, *args, **kwargs):
  9
+        middleware = middleware_class(*args, **kwargs)
  10
+        def _wrapped_view(request, *args, **kwargs):
  11
+            if hasattr(middleware, 'process_request'):
  12
+                result = middleware.process_request(request)
  13
+                if result is not None:
  14
+                    return result
  15
+            response = view_func(request, *args, **kwargs)
  16
+            if hasattr(middleware, 'process_response'):
  17
+                result = middleware.process_response(request, response)
  18
+                if result is not None:
  19
+                    return result
  20
+            return response
  21
+        return _wrapped_view
  22
+    return _decorator_from_middleware

0 notes on commit 71564b4

Please sign in to comment.
Something went wrong with that request. Please try again.