Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _load_old_middleware(self):
middleware_classes = self.middleware_classes or []
for mw_class in middleware_classes:
mw_class, mw_types = self._unpack_middleware(mw_class)
mw_instance = mw_class()
if self._should_add_middleware(mw_instance, mw_types, MiddlewareType.PROCESS_REQUEST):
self._request_middleware.append(mw_instance.process_request)
if self._should_add_middleware(mw_instance, mw_types, MiddlewareType.PROCESS_VIEW):
self._view_middleware.append(mw_instance.process_view)
if self._should_add_middleware(mw_instance, mw_types, MiddlewareType.PROCESS_TEMPLATE_RESPONSE):
self._template_response_middleware.insert(0, mw_instance.process_template_response)
if self._should_add_middleware(mw_instance, mw_types, MiddlewareType.PROCESS_RESPONSE):
self._response_middleware.insert(0, mw_instance.process_response)
if self._should_add_middleware(mw_instance, mw_types, MiddlewareType.PROCESS_EXCEPTION):
self._exception_middleware.insert(0, mw_instance.process_exception)
def _load_new_middleware(self):
from django.core.handlers.exception import convert_exception_to_response
handler = convert_exception_to_response(self._get_response)
middleware_classes = reversed(self.middleware or [])
for mw_class in middleware_classes:
mw_class, mw_types = self._unpack_middleware(mw_class)
mw_instance = mw_class(handler)
if self._should_add_middleware(mw_instance, mw_types, MiddlewareType.PROCESS_VIEW):
self._view_middleware.insert(0, mw_instance.process_view)
if self._should_add_middleware(mw_instance, mw_types, MiddlewareType.PROCESS_TEMPLATE_RESPONSE):
self._template_response_middleware.append(mw_instance.process_template_response)
if self._should_add_middleware(mw_instance, mw_types, MiddlewareType.PROCESS_EXCEPTION):
self._exception_middleware.append(mw_instance.process_exception)
handler = convert_exception_to_response(mw_instance)
self._middleware_chain = handler