|
| 1 | +from functools import wraps |
| 2 | + |
1 | 3 | from django.conf.urls import patterns, url
|
2 | 4 | from django.utils.translation import ugettext_lazy as _
|
3 |
| -from django.utils.decorators import decorator_from_middleware_with_args |
4 |
| -from django.middleware.cache import CacheMiddleware |
| 5 | +from django.views.decorators.cache import cache_page |
5 | 6 |
|
6 | 7 | from .views import OrganisationSearchView, OrganisationDetail
|
7 | 8 |
|
8 | 9 | CACHE_TIME = 15 * 60
|
9 | 10 |
|
10 | 11 |
|
11 |
| -def cache_page_anonymous(*args, **kwargs): |
12 |
| - """ |
13 |
| - Decorator to cache Django views only for anonymous users. |
14 |
| - Use just like the decorator cache_page: |
15 |
| -
|
16 |
| - @cache_page_anonymous(60 * 30) # cache for 30 mins |
17 |
| - def your_view_here(request): |
18 |
| - ... |
19 |
| - """ |
20 |
| - key_prefix = kwargs.pop('key_prefix', None) |
21 |
| - return decorator_from_middleware_with_args(CacheMiddleware)( |
22 |
| - cache_timeout=args[0], |
23 |
| - key_prefix=key_prefix, |
24 |
| - cache_anonymous_only=True) |
25 |
| - |
26 |
| - |
27 | 12 | def c(view):
|
28 |
| - return cache_page_anonymous(CACHE_TIME)(view) |
| 13 | + @wraps(view) |
| 14 | + def cache_page_anonymous(request, *args, **kwargs): |
| 15 | + if request.user.is_authenticated(): |
| 16 | + return view(request, *args, **kwargs) |
| 17 | + return cache_page(CACHE_TIME)(view)(request, *args, **kwargs) |
| 18 | + return cache_page_anonymous |
29 | 19 |
|
30 | 20 |
|
31 | 21 | urlpatterns = patterns('',
|
|
0 commit comments