Author: lukeplant
Date: 2011-05-23 09:08:36 -0700 (Mon, 23 May 2011)
New Revision: 16271

Modified:
   django/trunk/tests/regressiontests/decorators/tests.py
Log:
Refactoring of 'fully_decorated' for clarity and removal of duplication.

Also allows re-use of 'full_decorator' if we need it.

Modified: django/trunk/tests/regressiontests/decorators/tests.py
===================================================================
--- django/trunk/tests/regressiontests/decorators/tests.py      2011-05-23 
13:23:00 UTC (rev 16270)
+++ django/trunk/tests/regressiontests/decorators/tests.py      2011-05-23 
16:08:36 UTC (rev 16271)
@@ -16,36 +16,51 @@
     return HttpResponse('<html><body>dummy</body></html>')
 fully_decorated.anything = "Expected __dict__"
 
-# django.views.decorators.http
-fully_decorated = require_http_methods(["GET"])(fully_decorated)
-fully_decorated = require_GET(fully_decorated)
-fully_decorated = require_POST(fully_decorated)
-fully_decorated = require_safe(fully_decorated)
 
-# django.views.decorators.vary
-fully_decorated = vary_on_headers('Accept-language')(fully_decorated)
-fully_decorated = vary_on_cookie(fully_decorated)
+def compose(*functions):
+    # compose(f, g)(*args, **kwargs) == f(g(*args, **kwargs))
+    functions = list(reversed(functions))
+    def _inner(*args, **kwargs):
+        result = functions[0](*args, **kwargs)
+        for f in functions[1:]:
+            result = f(result)
+        return result
+    return _inner
 
-# django.views.decorators.cache
-fully_decorated = cache_page(60*15)(fully_decorated)
-fully_decorated = cache_control(private=True)(fully_decorated)
-fully_decorated = never_cache(fully_decorated)
 
-# django.contrib.auth.decorators
-# Apply user_passes_test twice to check #9474
-fully_decorated = user_passes_test(lambda u:True)(fully_decorated)
-fully_decorated = login_required(fully_decorated)
-fully_decorated = permission_required('change_world')(fully_decorated)
+full_decorator = compose(
+    # django.views.decorators.http
+    require_http_methods(["GET"]),
+    require_GET,
+    require_POST,
+    require_safe,
 
-# django.contrib.admin.views.decorators
-fully_decorated = staff_member_required(fully_decorated)
+    # django.views.decorators.vary
+    vary_on_headers('Accept-language'),
+    vary_on_cookie,
 
-# django.utils.functional
-fully_decorated = memoize(fully_decorated, {}, 1)
-fully_decorated = allow_lazy(fully_decorated)
-fully_decorated = lazy(fully_decorated)
+    # django.views.decorators.cache
+    cache_page(60*15),
+    cache_control(private=True),
+    never_cache,
 
+    # django.contrib.auth.decorators
+    # Apply user_passes_test twice to check #9474
+    user_passes_test(lambda u:True),
+    login_required,
+    permission_required('change_world'),
 
+    # django.contrib.admin.views.decorators
+    staff_member_required,
+
+    # django.utils.functional
+    lambda f: memoize(f, {}, 1),
+    allow_lazy,
+    lazy,
+)
+
+fully_decorated = full_decorator(fully_decorated)
+
 class DecoratorsTest(TestCase):
 
     def test_attributes(self):

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-updates@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to