<s> import datetime <EOL> from logging import getLogger <EOL> from pyramid . events import subscriber <EOL> from pyramid . events import NewResponse <EOL> from pyramid . response import FileResponse <EOL> from sqlalchemy . orm . exc import DetachedInstanceError <EOL> from kotti import get_settings <EOL> from kotti . security import get_user <EOL> CACHE_POLICY_HEADER = '<STR_LIT>' <EOL> logger = getLogger ( __name__ ) <EOL> def set_max_age ( response , delta , cache_ctrl = None ) : <EOL> """<STR_LIT>""" <EOL> if cache_ctrl is None : <EOL> cache_ctrl = { } <EOL> seconds = delta . seconds + delta . days * <NUM_LIT> * <NUM_LIT> * <NUM_LIT> <EOL> if seconds < <NUM_LIT:0> : <EOL> seconds = <NUM_LIT:0> <EOL> now = datetime . datetime . utcnow ( ) <EOL> cache_ctrl . setdefault ( '<STR_LIT>' , seconds ) <EOL> existing = response . headers . get ( '<STR_LIT>' ) <EOL> if existing : <EOL> for e in [ e . strip ( ) for e in existing . split ( '<STR_LIT:U+002C>' ) ] : <EOL> kv = e . split ( '<STR_LIT:=>' ) <EOL> if len ( kv ) == <NUM_LIT:2> : <EOL> cache_ctrl . setdefault ( kv [ <NUM_LIT:0> ] , kv [ <NUM_LIT:1> ] ) <EOL> else : <EOL> cache_ctrl . setdefault ( kv [ <NUM_LIT:0> ] ) <EOL> cache_control_header = [ ] <EOL> for key , value in sorted ( cache_ctrl . items ( ) ) : <EOL> if value is None : <EOL> cache_control_header . append ( key ) <EOL> else : <EOL> cache_control_header . append ( '<STR_LIT>' . format ( key , value ) ) <EOL> cache_control_header = '<STR_LIT:U+002C>' . join ( cache_control_header ) <EOL> response . headers [ '<STR_LIT>' ] = cache_control_header <EOL> response . headers [ '<STR_LIT>' ] = ( now + delta ) . strftime ( <EOL> "<STR_LIT>" ) <EOL> caching_policies = { <EOL> '<STR_LIT>' : <EOL> lambda response : set_max_age ( response , datetime . timedelta ( days = - <NUM_LIT:1> ) , <EOL> cache_ctrl = { '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> '<STR_LIT>' : <EOL> lambda response : set_max_age ( response , datetime . timedelta ( hours = <NUM_LIT:4> ) ) , <EOL> '<STR_LIT>' : <EOL> lambda response : set_max_age ( response , datetime . timedelta ( days = <NUM_LIT:32> ) , <EOL> cache_ctrl = { '<STR_LIT>' : None } ) , <EOL> '<STR_LIT>' : <EOL> lambda response : set_max_age ( response , datetime . timedelta ( days = - <NUM_LIT:1> ) ) , <EOL> } <EOL> def _safe_get_user ( request ) : <EOL> try : <EOL> return get_user ( request ) <EOL> except DetachedInstanceError : <EOL> return not None <EOL> def default_caching_policy_chooser ( context , request , response ) : <EOL> if request . method != '<STR_LIT:GET>' or response . status_int != <NUM_LIT:200> : <EOL> return None <EOL> elif isinstance ( response , FileResponse ) : <EOL> return '<STR_LIT>' <EOL> elif _safe_get_user ( request ) is not None : <EOL> return '<STR_LIT>' <EOL> elif response . headers [ '<STR_LIT>' ] . startswith ( '<STR_LIT>' ) : <EOL> return '<STR_LIT>' <EOL> else : <EOL> return '<STR_LIT>' <EOL> def caching_policy_chooser ( context , request , response ) : <EOL> return get_settings ( ) [ '<STR_LIT>' ] [ <NUM_LIT:0> ] ( <EOL> context , request , response ) <EOL> @ subscriber ( NewResponse ) <EOL> def set_cache_headers ( event ) : <EOL> request , response = event . request , event . response <EOL> if not hasattr ( event . request , '<STR_LIT>' ) : <EOL> return <EOL> context = event . request . context <EOL> caching_policy = response . headers . get ( CACHE_POLICY_HEADER ) <EOL> if caching_policy is None : <EOL> try : <EOL> caching_policy = caching_policy_chooser ( context , request , response ) <EOL> except : <EOL> logger . exception ( "<STR_LIT>" . format ( <EOL> caching_policy_chooser ) ) <EOL> if caching_policy is not None : <EOL> response . headers [ CACHE_POLICY_HEADER ] = caching_policy <EOL> if caching_policy : <EOL> caching_policies [ caching_policy ] ( response ) <EOL> def includeme ( config ) : <EOL> """<STR_LIT>""" <EOL> config . scan ( __name__ ) </s>
