Skip to content

Commit

Permalink
Remove unused middleware stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
davegaeddert committed Feb 2, 2024
1 parent 65ac6d9 commit a73918b
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 115 deletions.
6 changes: 0 additions & 6 deletions bolt/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,6 @@ class ViewDoesNotExist(Exception):
pass


class MiddlewareNotUsed(Exception):
"""This middleware is not used in this server configuration"""

pass


class ImproperlyConfigured(Exception):
"""Bolt is somehow improperly configured"""

Expand Down
70 changes: 4 additions & 66 deletions bolt/internal/handlers/base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import types

from bolt.exceptions import ImproperlyConfigured, MiddlewareNotUsed
from bolt.exceptions import ImproperlyConfigured
from bolt.logs import log_response
from bolt.runtime import settings
from bolt.signals import request_finished
Expand All @@ -15,7 +15,6 @@

class BaseHandler:
_view_middleware = None
_exception_middleware = None
_middleware_chain = None

def load_middleware(self):
Expand All @@ -25,29 +24,12 @@ def load_middleware(self):
Must be called after the environment is fixed (see __call__ in subclasses).
"""
self._view_middleware = []
self._exception_middleware = []

get_response = self._get_response
handler = convert_exception_to_response(get_response)
for middleware_path in reversed(settings.MIDDLEWARE):
middleware = import_string(middleware_path)
try:
# Adapt handler, if needed.
adapted_handler = self.adapt_method_mode(
handler,
debug=settings.DEBUG,
name="middleware %s" % middleware_path,
)
mw_instance = middleware(adapted_handler)
except MiddlewareNotUsed as exc:
if settings.DEBUG:
if str(exc):
logger.debug("MiddlewareNotUsed(%r): %s", middleware_path, exc)
else:
logger.debug("MiddlewareNotUsed: %r", middleware_path)
continue
else:
handler = adapted_handler
mw_instance = middleware(handler)

if mw_instance is None:
raise ImproperlyConfigured(
Expand All @@ -57,42 +39,15 @@ def load_middleware(self):
if hasattr(mw_instance, "process_view"):
self._view_middleware.insert(
0,
self.adapt_method_mode(mw_instance.process_view),
)
if hasattr(mw_instance, "process_exception"):
# The exception-handling stack is still always synchronous for
# now, so adapt that way.
self._exception_middleware.append(
self.adapt_method_mode(False, mw_instance.process_exception),
mw_instance.process_view,
)

handler = convert_exception_to_response(mw_instance)

# Adapt the top of the stack, if needed.
handler = self.adapt_method_mode(handler)
# We only assign to this when initialization is complete as it is used
# as a flag for initialization being complete.
self._middleware_chain = handler

def adapt_method_mode(
self,
method,
debug=False,
name=None,
):
"""
Adapt a method to be in the correct "mode":
- If is_async is False:
- Synchronous methods are left alone
- Asynchronous methods are wrapped with async_to_sync
- If is_async is True:
- Synchronous methods are wrapped with sync_to_async()
- Asynchronous methods are left alone
"""
if debug and not name:
name = name or "method %s()" % method.__qualname__
return method

def get_response(self, request):
"""Return an HttpResponse object for the given HttpRequest."""
# Setup default url resolver for this thread
Expand Down Expand Up @@ -128,13 +83,7 @@ def _get_response(self, request):

if response is None:
wrapped_callback = self.make_view_atomic(callback)

try:
response = wrapped_callback(request, *callback_args, **callback_kwargs)
except Exception as e:
response = self.process_exception_by_middleware(e, request)
if response is None:
raise
response = wrapped_callback(request, *callback_args, **callback_kwargs)

# Complain if the view returned None (a common error).
self.check_response(response, callback)
Expand Down Expand Up @@ -189,17 +138,6 @@ def make_view_atomic(self, view):
view = transaction.atomic(using=alias)(view)
return view

def process_exception_by_middleware(self, exception, request):
"""
Pass the exception to the exception middleware. If no middleware
return a response for this exception, return None.
"""
for middleware_method in self._exception_middleware:
response = middleware_method(request, exception)
if response:
return response
return None


def reset_urlconf(sender, **kwargs):
"""Reset the URLconf after each request is finished."""
Expand Down
43 changes: 0 additions & 43 deletions bolt/utils/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,46 +88,3 @@ def _dec(obj):
obj = decorator if hasattr(decorator, "__name__") else decorator.__class__
_dec.__name__ = "method_decorator(%s)" % obj.__name__
return _dec


def decorator_from_middleware(middleware_class):
"""
Given a middleware class (not an instance), return a view decorator. This
lets you use middleware functionality on a per-view basis. The middleware
is created with no params passed.
"""
return make_middleware_decorator(middleware_class)()


def make_middleware_decorator(middleware_class):
def _make_decorator(*m_args, **m_kwargs):
def _decorator(view_func):
middleware = middleware_class(view_func, *m_args, **m_kwargs)

@wraps(view_func)
def _wrapper_view(request, *args, **kwargs):
if hasattr(middleware, "process_request"):
result = middleware.process_request(request)
if result is not None:
return result
if hasattr(middleware, "process_view"):
result = middleware.process_view(request, view_func, args, kwargs)
if result is not None:
return result
try:
response = view_func(request, *args, **kwargs)
except Exception as e:
if hasattr(middleware, "process_exception"):
result = middleware.process_exception(request, e)
if result is not None:
return result
raise
if hasattr(middleware, "process_response"):
return middleware.process_response(request, response)
return response

return _wrapper_view

return _decorator

return _make_decorator

0 comments on commit a73918b

Please sign in to comment.