Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Yasuhisa Yamazaki committed Jun 18, 2014
1 parent c44ca39 commit 4ca5521
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
17 changes: 10 additions & 7 deletions django_mobile/cache/__init__.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
from functools import wraps
from django.views.decorators.cache import cache_page as _cache_page
from django.utils.decorators import decorator_from_middleware
from django_mobile.cache.middleware import CacheFlavourMiddleware

from django_mobile.cache.middleware import CacheFlavourMiddleware, CacheFlavourRequestMiddleware, \
CacheFlavourResponseMiddleware

__all__ = ('cache_page', 'vary_on_flavour')

__all__ = ('cache_page', 'vary_on_flavour')

vary_on_flavour = decorator_from_middleware(CacheFlavourMiddleware)
vary_on_flavour_request = decorator_from_middleware(CacheFlavourRequestMiddleware)
vary_on_flavour_response = decorator_from_middleware(CacheFlavourResponseMiddleware)


def cache_page(*args, **kwargs):
'''
Same as django's ``cache_page`` decorator, but wraps the view into
``vary_on_flavour`` decorator before. Makes it possible to serve multiple
flavours without getting into trouble with django's caching that doesn't
``vary_on_flavour_request`` and ``vary_on_flavour_response`` decorators.
Makes it possible to serve multiple flavours without getting into trouble with django's caching that doesn't
know about flavours.
'''
decorator = _cache_page(*args, **kwargs)

def flavoured_decorator(func):
return decorator(vary_on_flavour(func))
return vary_on_flavour_request(_cache_page(*args, **kwargs)(vary_on_flavour_response(func)))

return flavoured_decorator
8 changes: 7 additions & 1 deletion django_mobile/cache/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
from django.utils.cache import patch_vary_headers


class CacheFlavourMiddleware(object):
class CacheFlavourRequestMiddleware(object):
def process_request(self, request):
_set_request_header(request, get_flavour(request))


class CacheFlavourResponseMiddleware(object):
def process_response(self, request, response):
patch_vary_headers(response, ['X-Flavour'])
return response


class CacheFlavourMiddleware(CacheFlavourRequestMiddleware, CacheFlavourResponseMiddleware):
pass

0 comments on commit 4ca5521

Please sign in to comment.