<s> """<STR_LIT>""" <EOL> import logging <EOL> from google . appengine . runtime import apiproxy_errors <EOL> from google . appengine . runtime import DeadlineExceededError <EOL> from django . conf import settings <EOL> from django . http import Http404 , HttpResponse , HttpResponsePermanentRedirect <EOL> from django . template import Context , loader <EOL> from codereview import auth_utils <EOL> from codereview import models <EOL> class AddHSTSHeaderMiddleware ( object ) : <EOL> """<STR_LIT>""" <EOL> def process_response ( self , request , response ) : <EOL> if request . is_secure ( ) : <EOL> response [ '<STR_LIT>' ] = ( <EOL> '<STR_LIT>' % settings . HSTS_MAX_AGE ) <EOL> return response <EOL> class AddUserToRequestMiddleware ( object ) : <EOL> """<STR_LIT>""" <EOL> def process_request ( self , request ) : <EOL> request . user = auth_utils . get_current_user ( ) <EOL> request . user_is_admin = auth_utils . is_current_user_admin ( ) <EOL> account = None <EOL> if request . user is not None : <EOL> account = models . Account . get_account_for_user ( request . user ) <EOL> models . Account . current_user_account = account <EOL> class PropagateExceptionMiddleware ( object ) : <EOL> """<STR_LIT>""" <EOL> def _text_requested ( self , request ) : <EOL> """<STR_LIT>""" <EOL> media_ranges = request . META . get ( '<STR_LIT>' , '<STR_LIT>' ) . split ( '<STR_LIT:U+002C>' ) <EOL> return len ( media_ranges ) == <NUM_LIT:1> and media_ranges [ <NUM_LIT:0> ] == '<STR_LIT>' <EOL> def process_exception ( self , request , exception ) : <EOL> if settings . DEBUG or isinstance ( exception , Http404 ) : <EOL> return None <EOL> if isinstance ( exception , apiproxy_errors . CapabilityDisabledError ) : <EOL> msg = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> status = <NUM_LIT> <EOL> elif isinstance ( exception , ( DeadlineExceededError , MemoryError ) ) : <EOL> msg = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> status = <NUM_LIT> <EOL> else : <EOL> msg = '<STR_LIT>' <EOL> status = <NUM_LIT> <EOL> logging . exception ( '<STR_LIT>' % exception . __class__ . __name__ ) <EOL> technical = '<STR_LIT>' % ( exception , exception . __class__ . __name__ ) <EOL> if self . _text_requested ( request ) : <EOL> content = '<STR_LIT>' % ( msg , technical ) <EOL> content_type = '<STR_LIT>' <EOL> else : <EOL> tpl = loader . get_template ( '<STR_LIT>' ) <EOL> ctx = Context ( { '<STR_LIT>' : msg , '<STR_LIT>' : technical } ) <EOL> content = tpl . render ( ctx ) <EOL> content_type = '<STR_LIT>' <EOL> return HttpResponse ( content , status = status , content_type = content_type ) <EOL> class RedirectToHTTPSMiddleware ( object ) : <EOL> """<STR_LIT>""" <EOL> def process_request ( self , request ) : <EOL> is_cron = request . META . get ( '<STR_LIT>' , '<STR_LIT>' ) == '<STR_LIT:true>' <EOL> if settings . DEBUG or request . method == '<STR_LIT:POST>' or is_cron : <EOL> return <EOL> if not request . is_secure ( ) : <EOL> host = request . get_host ( ) . split ( '<STR_LIT::>' ) [ <NUM_LIT:0> ] <EOL> return HttpResponsePermanentRedirect ( <EOL> '<STR_LIT>' % ( host , request . get_full_path ( ) ) ) </s>
