Ticket #12019: cache_page_default_timeout.diff

File cache_page_default_timeout.diff, 1.8 KB (added by j4mie, 5 years ago)

Slightly different approach at patch, includes test

  • django/views/decorators/cache.py

     
    3333    #   my_view = cache_page(123, key_prefix="foo")(my_view)
    3434    # and possibly this way (?):
    3535    #   my_view = cache_page(123, my_view)
     36    # and also this way (timeout defaults to CACHE_MIDDLEWARE_SECONDS)
     37    #   my_view = cache_page(my_view)
    3638
    3739    # We also add some asserts to give better error messages in case people are
    3840    # using other ways to call cache_page that no longer work.
     
    4648            return decorator_from_middleware_with_args(CacheMiddleware)(cache_timeout=args[0], key_prefix=key_prefix)(args[1])
    4749        else:
    4850            assert False, "cache_page must be passed either a single argument (timeout) or a view function and a timeout"
     51    elif callable(args[0]):
     52        return decorator_from_middleware_with_args(CacheMiddleware)(key_prefix=key_prefix)(args[0])
    4953    else:
    5054        return decorator_from_middleware_with_args(CacheMiddleware)(cache_timeout=args[0], key_prefix=key_prefix)
    5155
  • tests/regressiontests/decorators/tests.py

     
    112112        self.assertEqual(my_view_cached(HttpRequest()), "response")
    113113        my_view_cached2 = cache_page(my_view, 123, key_prefix="test")
    114114        self.assertEqual(my_view_cached2(HttpRequest()), "response")
     115        my_view_cached3 = cache_page(my_view)
     116        self.assertEqual(my_view_cached3(HttpRequest()), "response")
    115117
    116118
    117119# For testing method_decorator, a decorator that assumes a single argument.
Back to Top