<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> import re <EOL> from django . conf import settings <EOL> from django . core . mail import mail_managers <EOL> from django . http import HttpResponseRedirect <EOL> from django . utils . encoding import force_text <EOL> class AjaxRedirectMiddleware ( object ) : <EOL> """<STR_LIT>""" <EOL> def process_response ( self , request , response ) : <EOL> if request . is_ajax ( ) : <EOL> if request . REQUEST . get ( '<STR_LIT>' ) : <EOL> return response <EOL> if type ( response ) == HttpResponseRedirect : <EOL> response . status_code = <NUM_LIT> <EOL> return response <EOL> class ErrorMiddleware ( object ) : <EOL> """<STR_LIT>""" <EOL> def process_exception ( self , request , exception ) : <EOL> """<STR_LIT>""" <EOL> if request . user and hasattr ( request . user , '<STR_LIT:email>' ) : <EOL> request . META [ '<STR_LIT>' ] = request . user . email <EOL> class SSLRedirect : <EOL> """<STR_LIT>""" <EOL> def process_request ( self , request ) : <EOL> no_ssl_urls = getattr ( settings , '<STR_LIT>' , [ ] ) <EOL> urls = tuple ( [ re . compile ( url ) for url in no_ssl_urls ] ) <EOL> secure = False <EOL> for url in urls : <EOL> if not url . match ( request . path ) : <EOL> secure = True <EOL> break <EOL> if not secure == self . _is_secure ( request ) : <EOL> return self . _redirect ( request , secure ) <EOL> def _is_secure ( self , request ) : <EOL> if request . is_secure ( ) : <EOL> return True <EOL> if '<STR_LIT>' in request . META : <EOL> return request . META [ '<STR_LIT>' ] == '<STR_LIT>' <EOL> return False <EOL> def _redirect ( self , request , secure ) : <EOL> protocol = secure and "<STR_LIT>" or "<STR_LIT:http>" <EOL> if secure : <EOL> host = getattr ( settings , '<STR_LIT>' , request . get_host ( ) ) <EOL> else : <EOL> host = getattr ( settings , '<STR_LIT>' , request . get_host ( ) ) <EOL> newurl = "<STR_LIT>" % ( protocol , host , request . get_full_path ( ) ) <EOL> if settings . DEBUG and request . method == '<STR_LIT:POST>' : <EOL> raise Exception ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return HttpResponseRedirect ( newurl ) <EOL> class CustomBrokenLinkEmailsMiddleware ( object ) : <EOL> """<STR_LIT>""" <EOL> def process_response ( self , request , response ) : <EOL> """<STR_LIT>""" <EOL> if response . status_code == <NUM_LIT> and not settings . DEBUG : <EOL> domain = request . get_host ( ) <EOL> path = request . get_full_path ( ) <EOL> referer = force_text ( <EOL> request . META . get ( '<STR_LIT>' , '<STR_LIT>' ) , errors = '<STR_LIT:replace>' ) <EOL> if not self . is_ignorable_request ( request , path , domain , referer ) : <EOL> ua = request . META . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> ip = request . META . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> user = None <EOL> if request . user and hasattr ( request . user , '<STR_LIT:email>' ) : <EOL> user = request . user . email <EOL> content = ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) % ( referer , path , ua , ip , user ) <EOL> if self . is_internal_request ( domain , referer ) : <EOL> internal = '<STR_LIT>' <EOL> else : <EOL> internal = '<STR_LIT>' <EOL> mail_managers ( <EOL> "<STR_LIT>" % ( <EOL> internal , <EOL> domain <EOL> ) , <EOL> content , <EOL> fail_silently = True ) <EOL> return response <EOL> def is_internal_request ( self , domain , referer ) : <EOL> """<STR_LIT>""" <EOL> return bool ( re . match ( "<STR_LIT>" % re . escape ( domain ) , referer ) ) <EOL> def is_ignorable_request ( self , request , uri , domain , referer ) : <EOL> """<STR_LIT>""" <EOL> if ( not referer or ( not self . is_internal_request ( <EOL> domain , referer ) and '<STR_LIT:?>' in referer ) ) : <EOL> return True <EOL> return any ( <EOL> pattern . search ( uri ) for pattern in settings . IGNORABLE_404_URLS ) </s>
