~ubuntu-branches/ubuntu/quantal/python-django/quantal-security

« back to all changes in this revision

Viewing changes to django/views/decorators/cache.py

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lamb
  • Date: 2010-05-21 07:52:55 UTC
  • mfrom: (1.3.6 upstream)
  • mto: This revision was merged to the branch mainline in revision 28.
  • Revision ID: james.westby@ubuntu.com-20100521075255-ii78v1dyfmyu3uzx
Tags: upstream-1.2
ImportĀ upstreamĀ versionĀ 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
try:
15
15
    from functools import wraps
16
16
except ImportError:
17
 
    from django.utils.functional import wraps  # Python 2.3, 2.4 fallback.
 
17
    from django.utils.functional import wraps  # Python 2.4 fallback.
18
18
 
19
 
from django.utils.decorators import decorator_from_middleware
 
19
from django.utils.decorators import decorator_from_middleware_with_args, available_attrs
20
20
from django.utils.cache import patch_cache_control, add_never_cache_headers
21
21
from django.middleware.cache import CacheMiddleware
22
22
 
23
 
cache_page = decorator_from_middleware(CacheMiddleware)
 
23
 
 
24
def cache_page(*args, **kwargs):
 
25
    # We need backwards compatibility with code which spells it this way:
 
26
    #   def my_view(): pass
 
27
    #   my_view = cache_page(my_view, 123)
 
28
    # and this way:
 
29
    #   my_view = cache_page(123)(my_view)
 
30
    # and this:
 
31
    #   my_view = cache_page(my_view, 123, key_prefix="foo")
 
32
    # and this:
 
33
    #   my_view = cache_page(123, key_prefix="foo")(my_view)
 
34
    # and possibly this way (?):
 
35
    #   my_view = cache_page(123, my_view)
 
36
 
 
37
    # We also add some asserts to give better error messages in case people are
 
38
    # using other ways to call cache_page that no longer work.
 
39
    key_prefix = kwargs.pop('key_prefix', None)
 
40
    assert not kwargs, "The only keyword argument accepted is key_prefix"
 
41
    if len(args) > 1:
 
42
        assert len(args) == 2, "cache_page accepts at most 2 arguments"
 
43
        if callable(args[0]):
 
44
            return decorator_from_middleware_with_args(CacheMiddleware)(cache_timeout=args[1], key_prefix=key_prefix)(args[0])
 
45
        elif callable(args[1]):
 
46
            return decorator_from_middleware_with_args(CacheMiddleware)(cache_timeout=args[0], key_prefix=key_prefix)(args[1])
 
47
        else:
 
48
            assert False, "cache_page must be passed either a single argument (timeout) or a view function and a timeout"
 
49
    else:
 
50
        return decorator_from_middleware_with_args(CacheMiddleware)(cache_timeout=args[0], key_prefix=key_prefix)
 
51
 
24
52
 
25
53
def cache_control(**kwargs):
26
 
 
27
54
    def _cache_controller(viewfunc):
28
 
 
29
55
        def _cache_controlled(request, *args, **kw):
30
56
            response = viewfunc(request, *args, **kw)
31
57
            patch_cache_control(response, **kwargs)
32
58
            return response
33
 
 
34
 
        return wraps(viewfunc)(_cache_controlled)
35
 
 
 
59
        return wraps(viewfunc, assigned=available_attrs(viewfunc))(_cache_controlled)
36
60
    return _cache_controller
37
61
 
 
62
 
38
63
def never_cache(view_func):
39
64
    """
40
65
    Decorator that adds headers to a response so that it will
44
69
        response = view_func(request, *args, **kwargs)
45
70
        add_never_cache_headers(response)
46
71
        return response
47
 
    return wraps(view_func)(_wrapped_view_func)
 
72
    return wraps(view_func, assigned=available_attrs(view_func))(_wrapped_view_func)