From 76d443802d41e3820efaa56068d1791f4ffda30a Mon Sep 17 00:00:00 2001 From: Gadhi Rodriguez Date: Tue, 27 Aug 2024 06:45:17 -0600 Subject: [PATCH] Add ASGI support (#127) This commit updates the middleware to handle applications using ASGI Type: feature Co-authored-by: Gadhi Rodriguez --- pghistory/middleware.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/pghistory/middleware.py b/pghistory/middleware.py index 2c9ca26..11624c5 100644 --- a/pghistory/middleware.py +++ b/pghistory/middleware.py @@ -1,3 +1,4 @@ +from django.core.handlers.asgi import ASGIRequest as DjangoASGIRequest from django.core.handlers.wsgi import WSGIRequest as DjangoWSGIRequest from django.db import connection @@ -5,7 +6,7 @@ from pghistory import config -class WSGIRequest(DjangoWSGIRequest): +class DjangoRequest: """ Although Django's auth middleware sets the user in middleware, apps like django-rest-framework set the user in the view layer. @@ -28,6 +29,14 @@ def __setattr__(self, attr, value): return super().__setattr__(attr, value) +class WSGIRequest(DjangoRequest, DjangoWSGIRequest): + pass + + +class ASGIRequest(DjangoRequest, DjangoASGIRequest): + pass + + def HistoryMiddleware(get_response): """ Annotates the user/url in the pghistory context. @@ -43,6 +52,8 @@ def middleware(request): with pghistory.context(user=user, url=request.path): if isinstance(request, DjangoWSGIRequest): # pragma: no branch request.__class__ = WSGIRequest + elif isinstance(request, DjangoASGIRequest): # pragma: no branch + request.__class__ = ASGIRequest return get_response(request) else: