Skip to content

Commit

Permalink
Update EasyAuditMiddleware to support async context
Browse files Browse the repository at this point in the history
Replaced standard threading with 'asgiref.local' in EasyAuditMiddleware. Also, made EasyAuditMiddleware extend Django's MiddlewareMixin to automatically handle sync and async execution modes.
Github issue: soynatan#291
  • Loading branch information
Kamil Niski committed Apr 20, 2024
1 parent 70e192b commit 446c26a
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions easyaudit/middleware/easyaudit.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# makes easy-audit thread-safe
from asgiref.local import Local
import contextlib
from threading import local
from django.utils.deprecation import MiddlewareMixin


class MockRequest:
Expand All @@ -10,7 +11,7 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)


_thread_locals = local()
_thread_locals = Local()


def get_current_request():
Expand All @@ -37,12 +38,9 @@ def clear_request():
del _thread_locals.request


class EasyAuditMiddleware:
class EasyAuditMiddleware(MiddlewareMixin):
"""Makes request available to this app signals."""

def __init__(self, get_response=None):
self.get_response = get_response

def __call__(self, request):
_thread_locals.request = (
request # seems redundant w/process_request, but keeping in for now.
Expand Down

0 comments on commit 446c26a

Please sign in to comment.