Skip to content

Instantly share code, notes, and snippets.

@abetkin
Created May 18, 2022 10:57
Show Gist options
  • Save abetkin/70c47a4905e7aefb262c1734f6879091 to your computer and use it in GitHub Desktop.
Save abetkin/70c47a4905e7aefb262c1734f6879091 to your computer and use it in GitHub Desktop.
"""
This patch is needed due to this BUG: https://code.djangoproject.com/ticket/33716
"""
from django.conf import settings
from django.core.handlers.asgi import ASGIRequest
from django.utils.deprecation import MiddlewareMixin
from django.utils.module_loading import import_string
#TODO on __init__
def is_next_middleware_async_capable(mw):
path = f'{mw.__class__.__module__}.{mw.__class__.__name__}'
next_index = settings.MIDDLEWARE.index(path) + 1
mw_class = import_string(settings.MIDDLEWARE[next_index])
return mw_class.async_capable
def call_mw(mw, request, _call_mw=MiddlewareMixin.__call__):
if isinstance(request, ASGIRequest) and is_next_middleware_async_capable(mw):
return mw.__acall__(request)
return _call_mw(mw, request)
MiddlewareMixin.__call__ = call_mw
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment