<s> import time <EOL> import re <EOL> import logging <EOL> log = logging . getLogger ( '<STR_LIT>' ) <EOL> from django import template <EOL> from django . middleware . cache import CacheMiddleware as DjangoCacheMiddleware <EOL> from django . core . cache import cache <EOL> from django . utils . cache import get_cache_key , add_never_cache_headers , learn_cache_key <EOL> from django . conf import settings <EOL> from ella . core . conf import core_settings <EOL> class DoubleRenderMiddleware ( object ) : <EOL> def _get_excluded_urls ( self ) : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> return self . _excluded_urls <EOL> if core_settings . DOUBLE_RENDER_EXCLUDE_URLS is None : <EOL> self . _excluded_urls = None <EOL> return None <EOL> self . _excluded_urls = ( ) <EOL> for url in core_settings . DOUBLE_RENDER_EXCLUDE_URLS : <EOL> self . _excluded_urls += ( re . compile ( url ) , ) <EOL> return self . _excluded_urls <EOL> def process_response ( self , request , response ) : <EOL> if response . status_code != <NUM_LIT:200> or not response [ '<STR_LIT:Content-Type>' ] . startswith ( '<STR_LIT:text>' ) or not core_settings . DOUBLE_RENDER : <EOL> return response <EOL> if self . _get_excluded_urls ( ) is not None : <EOL> for pattern in self . _get_excluded_urls ( ) : <EOL> if pattern . match ( request . path ) : <EOL> return response <EOL> try : <EOL> c = template . RequestContext ( request , { '<STR_LIT>' : True } ) <EOL> t = template . Template ( response . content ) <EOL> response . content = t . render ( c ) <EOL> response [ '<STR_LIT>' ] = len ( response . content ) <EOL> except Exception , e : <EOL> log . warning ( '<STR_LIT>' , unicode ( e ) . encode ( '<STR_LIT:utf8>' ) ) <EOL> return response <EOL> class CacheMiddleware ( DjangoCacheMiddleware ) : <EOL> def process_request ( self , request ) : <EOL> resp = super ( CacheMiddleware , self ) . process_request ( request ) <EOL> if resp is None : <EOL> request . _cache_middleware_key = get_cache_key ( request , self . key_prefix ) <EOL> return resp <EOL> def process_response ( self , request , response ) : <EOL> resp = super ( CacheMiddleware , self ) . process_response ( request , response ) <EOL> add_never_cache_headers ( resp ) <EOL> return resp <EOL> class UpdateCacheMiddleware ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . cache_timeout = settings . CACHE_MIDDLEWARE_SECONDS <EOL> self . key_prefix = settings . CACHE_MIDDLEWARE_KEY_PREFIX <EOL> self . cache_anonymous_only = getattr ( settings , '<STR_LIT>' , False ) <EOL> def process_response ( self , request , response ) : <EOL> """<STR_LIT>""" <EOL> add_never_cache_headers ( response ) <EOL> if not hasattr ( request , '<STR_LIT>' ) or not request . _cache_update_cache : <EOL> return response <EOL> if request . method != '<STR_LIT:GET>' : <EOL> return response <EOL> if not response . status_code == <NUM_LIT:200> : <EOL> return response <EOL> if request . _cache_middleware_key : <EOL> cache_key = request . _cache_middleware_key <EOL> else : <EOL> cache_key = learn_cache_key ( request , response , self . cache_timeout , self . key_prefix ) <EOL> cache . set ( cache_key , ( time . time ( ) , response ) , self . cache_timeout ) <EOL> return response <EOL> class FetchFromCacheMiddleware ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . cache_expire_timeout = settings . CACHE_MIDDLEWARE_SECONDS <EOL> self . key_prefix = settings . CACHE_MIDDLEWARE_KEY_PREFIX <EOL> self . cache_refresh_timeout = getattr ( settings , '<STR_LIT>' , self . cache_expire_timeout / <NUM_LIT:2> ) <EOL> self . timeout = getattr ( settings , '<STR_LIT>' , <NUM_LIT:10> ) <EOL> self . cache_anonymous_only = getattr ( settings , '<STR_LIT>' , False ) <EOL> def process_request ( self , request ) : <EOL> """<STR_LIT>""" <EOL> if self . cache_anonymous_only : <EOL> assert hasattr ( request , '<STR_LIT:user>' ) , "<STR_LIT>" <EOL> if not request . method in ( '<STR_LIT:GET>' , '<STR_LIT>' ) or request . GET : <EOL> request . _cache_update_cache = False <EOL> return None <EOL> if self . cache_anonymous_only and request . user . is_authenticated ( ) : <EOL> request . _cache_update_cache = False <EOL> return None <EOL> cache_key = get_cache_key ( request , self . key_prefix ) <EOL> request . _cache_middleware_key = cache_key <EOL> if cache_key is None : <EOL> request . _cache_update_cache = True <EOL> return None <EOL> response = cache . get ( cache_key , None ) <EOL> if response is None : <EOL> request . _cache_update_cache = True <EOL> return None <EOL> orig_time , response = response <EOL> if orig_time and ( ( time . time ( ) - orig_time ) > self . cache_refresh_timeout ) : <EOL> request . _cache_update_cache = True <EOL> cache . set ( cache_key , ( None , response ) , self . timeout ) <EOL> return None <EOL> request . _cache_update_cache = False <EOL> return response </s>
