<s> from __future__ import unicode_literals <EOL> import logging <EOL> import sys <EOL> import types <EOL> import warnings <EOL> from django import http <EOL> from django . conf import settings <EOL> from django . core import signals <EOL> from django . core . exceptions import ( <EOL> MiddlewareNotUsed , PermissionDenied , SuspiciousOperation , <EOL> ) <EOL> from django . db import connections , transaction <EOL> from django . http . multipartparser import MultiPartParserError <EOL> from django . urls import get_resolver , set_urlconf <EOL> from django . utils import six <EOL> from django . utils . deprecation import RemovedInDjango20Warning <EOL> from django . utils . encoding import force_text <EOL> from django . utils . module_loading import import_string <EOL> from django . views import debug <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> class BaseHandler ( object ) : <EOL> def __init__ ( self ) : <EOL> self . _request_middleware = None <EOL> self . _view_middleware = None <EOL> self . _template_response_middleware = None <EOL> self . _response_middleware = None <EOL> self . _exception_middleware = None <EOL> def load_middleware ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _view_middleware = [ ] <EOL> self . _template_response_middleware = [ ] <EOL> self . _response_middleware = [ ] <EOL> self . _exception_middleware = [ ] <EOL> request_middleware = [ ] <EOL> for middleware_path in settings . MIDDLEWARE_CLASSES : <EOL> mw_class = import_string ( middleware_path ) <EOL> try : <EOL> mw_instance = mw_class ( ) <EOL> except MiddlewareNotUsed as exc : <EOL> if settings . DEBUG : <EOL> if six . text_type ( exc ) : <EOL> logger . debug ( '<STR_LIT>' , middleware_path , exc ) <EOL> else : <EOL> logger . debug ( '<STR_LIT>' , middleware_path ) <EOL> continue <EOL> if hasattr ( mw_instance , '<STR_LIT>' ) : <EOL> request_middleware . append ( mw_instance . process_request ) <EOL> if hasattr ( mw_instance , '<STR_LIT>' ) : <EOL> self . _view_middleware . append ( mw_instance . process_view ) <EOL> if hasattr ( mw_instance , '<STR_LIT>' ) : <EOL> self . _template_response_middleware . insert ( <NUM_LIT:0> , mw_instance . process_template_response ) <EOL> if hasattr ( mw_instance , '<STR_LIT>' ) : <EOL> self . _response_middleware . insert ( <NUM_LIT:0> , mw_instance . process_response ) <EOL> if hasattr ( mw_instance , '<STR_LIT>' ) : <EOL> self . _exception_middleware . insert ( <NUM_LIT:0> , mw_instance . process_exception ) <EOL> self . _request_middleware = request_middleware <EOL> def make_view_atomic ( self , view ) : <EOL> non_atomic_requests = getattr ( view , '<STR_LIT>' , set ( ) ) <EOL> for db in connections . all ( ) : <EOL> if db . settings_dict [ '<STR_LIT>' ] and db . alias not in non_atomic_requests : <EOL> view = transaction . atomic ( using = db . alias ) ( view ) <EOL> return view <EOL> def get_exception_response ( self , request , resolver , status_code , exception ) : <EOL> try : <EOL> callback , param_dict = resolver . resolve_error_handler ( status_code ) <EOL> try : <EOL> response = callback ( request , ** dict ( param_dict , exception = exception ) ) <EOL> except TypeError : <EOL> warnings . warn ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> RemovedInDjango20Warning , stacklevel = <NUM_LIT:2> <EOL> ) <EOL> response = callback ( request , ** param_dict ) <EOL> except Exception : <EOL> signals . got_request_exception . send ( sender = self . __class__ , request = request ) <EOL> response = self . handle_uncaught_exception ( request , resolver , sys . exc_info ( ) ) <EOL> return response <EOL> def get_response ( self , request ) : <EOL> "<STR_LIT>" <EOL> urlconf = settings . ROOT_URLCONF <EOL> set_urlconf ( urlconf ) <EOL> resolver = get_resolver ( urlconf ) <EOL> response_is_rendered = False <EOL> try : <EOL> response = None <EOL> for middleware_method in self . _request_middleware : <EOL> response = middleware_method ( request ) <EOL> if response : <EOL> break <EOL> if response is None : <EOL> if hasattr ( request , '<STR_LIT>' ) : <EOL> urlconf = request . urlconf <EOL> set_urlconf ( urlconf ) <EOL> resolver = get_resolver ( urlconf ) <EOL> resolver_match = resolver . resolve ( request . path_info ) <EOL> callback , callback_args , callback_kwargs = resolver_match <EOL> request . resolver_match = resolver_match <EOL> for middleware_method in self . _view_middleware : <EOL> response = middleware_method ( request , callback , callback_args , callback_kwargs ) <EOL> if response : <EOL> break <EOL> if response is None : <EOL> wrapped_callback = self . make_view_atomic ( callback ) <EOL> try : <EOL> response = wrapped_callback ( request , * callback_args , ** callback_kwargs ) <EOL> except Exception as e : <EOL> response = self . process_exception_by_middleware ( e , request ) <EOL> if response is None : <EOL> if isinstance ( callback , types . FunctionType ) : <EOL> view_name = callback . __name__ <EOL> else : <EOL> view_name = callback . __class__ . __name__ + '<STR_LIT>' <EOL> raise ValueError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( callback . __module__ , view_name ) <EOL> ) <EOL> if hasattr ( response , '<STR_LIT>' ) and callable ( response . render ) : <EOL> for middleware_method in self . _template_response_middleware : <EOL> response = middleware_method ( request , response ) <EOL> if response is None : <EOL> raise ValueError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( middleware_method . __self__ . __class__ . __name__ ) <EOL> ) <EOL> try : <EOL> response = response . render ( ) <EOL> except Exception as e : <EOL> response = self . process_exception_by_middleware ( e , request ) <EOL> response_is_rendered = True <EOL> except http . Http404 as exc : <EOL> if settings . DEBUG : <EOL> response = debug . technical_404_response ( request , exc ) <EOL> else : <EOL> response = self . get_exception_response ( request , resolver , <NUM_LIT> , exc ) <EOL> except PermissionDenied as exc : <EOL> logger . warning ( <EOL> '<STR_LIT>' , request . path , <EOL> extra = { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : request } , <EOL> ) <EOL> response = self . get_exception_response ( request , resolver , <NUM_LIT> , exc ) <EOL> except MultiPartParserError as exc : <EOL> logger . warning ( <EOL> '<STR_LIT>' , request . path , <EOL> extra = { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : request } , <EOL> ) <EOL> response = self . get_exception_response ( request , resolver , <NUM_LIT> , exc ) <EOL> except SuspiciousOperation as exc : <EOL> security_logger = logging . getLogger ( '<STR_LIT>' % exc . __class__ . __name__ ) <EOL> security_logger . error ( <EOL> force_text ( exc ) , <EOL> extra = { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : request } , <EOL> ) <EOL> if settings . DEBUG : <EOL> return debug . technical_500_response ( request , * sys . exc_info ( ) , status_code = <NUM_LIT> ) <EOL> response = self . get_exception_response ( request , resolver , <NUM_LIT> , exc ) <EOL> except SystemExit : <EOL> raise <EOL> except Exception : <EOL> signals . got_request_exception . send ( sender = self . __class__ , request = request ) <EOL> response = self . handle_uncaught_exception ( request , resolver , sys . exc_info ( ) ) <EOL> try : <EOL> for middleware_method in self . _response_middleware : <EOL> response = middleware_method ( request , response ) <EOL> if response is None : <EOL> raise ValueError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( middleware_method . __self__ . __class__ . __name__ ) ) <EOL> except Exception : <EOL> signals . got_request_exception . send ( sender = self . __class__ , request = request ) <EOL> response = self . handle_uncaught_exception ( request , resolver , sys . exc_info ( ) ) <EOL> response . _closable_objects . append ( request ) <EOL> if not response_is_rendered and callable ( getattr ( response , '<STR_LIT>' , None ) ) : <EOL> response = response . render ( ) <EOL> if response . status_code == <NUM_LIT> : <EOL> logger . warning ( <EOL> '<STR_LIT>' , request . path , <EOL> extra = { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : request } , <EOL> ) <EOL> return response <EOL> def process_exception_by_middleware ( self , exception , request ) : <EOL> """<STR_LIT>""" <EOL> for middleware_method in self . _exception_middleware : <EOL> response = middleware_method ( request , exception ) <EOL> if response : <EOL> return response <EOL> raise <EOL> def handle_uncaught_exception ( self , request , resolver , exc_info ) : <EOL> """<STR_LIT>""" <EOL> if settings . DEBUG_PROPAGATE_EXCEPTIONS : <EOL> raise <EOL> logger . error ( <EOL> '<STR_LIT>' , request . path , <EOL> exc_info = exc_info , <EOL> extra = { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : request } , <EOL> ) <EOL> if settings . DEBUG : <EOL> return debug . technical_500_response ( request , * exc_info ) <EOL> if resolver . urlconf_module is None : <EOL> six . reraise ( * exc_info ) <EOL> callback , param_dict = resolver . resolve_error_handler ( <NUM_LIT> ) <EOL> return callback ( request , ** param_dict ) </s>
